Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow:如何有效地训练每层都需要馈电的网络?_Python_Tensorflow - Fatal编程技术网

Python TensorFlow:如何有效地训练每层都需要馈电的网络?

Python TensorFlow:如何有效地训练每层都需要馈电的网络?,python,tensorflow,Python,Tensorflow,通常,标准网络实现的唯一馈送是输入/标签。您将输入和标签/真相作为占位符,并将具体数据传递给feed\u dict以进行训练循环 我的模型由多个网络组成,就像一个网络,其中每一层本身就是一个网络。模型中的每个层网络接受两个输入:一个数据输入和一个用于图卷积的邻接列表 如果唯一需要的输入是数据输入,我就不会在使用占位符时遇到问题,因为不需要在模型的层网络之间进行馈送。但是,邻接列表是由numpy和sklearn函数创建的,这些函数需要真正的数据输入(如numpy数组),而不是占位符张量 我的网络看

通常,标准网络实现的唯一馈送是输入/标签。您将输入和标签/真相作为占位符,并将具体数据传递给
feed\u dict
以进行训练循环

我的模型由多个网络组成,就像一个网络,其中每一层本身就是一个网络。模型中的每个层网络接受两个输入:一个数据输入和一个用于图卷积的邻接列表

如果唯一需要的输入是数据输入,我就不会在使用占位符时遇到问题,因为不需要在模型的层网络之间进行馈送。但是,邻接列表是由
numpy
sklearn
函数创建的,这些函数需要真正的数据输入(如numpy数组),而不是占位符张量

我的网络看起来像这样:

# <make X* and alist* inputs placeholders>

# forwad through the layer-networks
H1 = network_fwd(X0, alist0, var_scope=SCOPE.format(0)) # params_0/*
H2 = network_fwd(H1, alist1, var_scope=SCOPE.format(1)) # params_1/*
H3 = network_fwd(H2, alist2, var_scope=SCOPE.format(2))
# ...
H10 = network_fwd(H9, alist9, var_scope=SCOPE.format(9))

# optimize on sum network error
error = loss_fn(H1, X1)
error += loss_fn(H2, X2)
error += loss_fn(H3, X3)
#...
error += loss_fn(H10, X10)

train = tf.train.AdamOptimizer(learning_rate).minimize(error)

# train
for step in range(num_steps):
    x_batch = numpy_ops.next_minibatch(X_train, mb_size) # list of data inputs

    # fill placeholders for true inputs X0, X1, ..., X10
    fdict = {placeholder_name: x for placeholder_name, x in zip(pnames, x_batch)}

    # layer-network predictions and adj lists
    adj_list0 = numpy_ops.get_adjacency_list(x_batch[0], K)
    fdict[alist0] = adj_list0
    #=====================================
    h1 = sess.run(H1, feed_dict=fdict)
    fdict[alist1] = numpy_ops.get_adjacency_list(h1, K)
    #=====================================
    h2 = sess.run(H2, feed_dict=fdict)
    fdict[alist2] = numpy_ops.get_adjacency_list(h2, K)
    # ...

    # and finally the actual training pass
    train.run(feed_dict=fdict)
#
#forwad通过分层网络
H1=网络(X0,alist0,var_scope=scope.format(0))#参数_0/*
H2=网络fwd(H1,alist1,var_scope=scope.format(1))#参数_1/*
H3=网络(H2,alist2,var\u scope=scope.format(2))
# ...
H10=网络转发(H9,alist9,var\u scope=scope.format(9))
#网络误差和的优化
错误=损失_fn(H1,X1)
误差+=损耗_fn(H2,X2)
错误+=损失_fn(H3,X3)
#...
误差+=损耗(H10,X10)
列车=tf.train.AdamOptimizer(学习率)。最小化(错误)
#训练
对于步进范围(num_步数):
x_批量=numpy_操作。下一个_小批量(x_系列,mb大小)#数据输入列表
#为真实输入X0、X1、…、X10填充占位符
fdict={placeholder_name:x表示placeholder_name,x在zip中(pnames,x_batch)}
#图层网络预测和调整列表
adj_list0=numpy_ops.get_adjacence_list(x_batch[0],K)
fdict[alist0]=调整列表0
#=====================================
h1=sess.run(h1,进给量=fdict)
fdict[alist1]=numpy_ops.get_adjancy_list(h1,K)
#=====================================
h2=sess.run(h2,进料量=fdict)
fdict[alist2]=numpy_ops.get_adjancy_list(h2,K)
# ...
#最后是实际的训练通行证
列车运行(进给量=fdict)
在我的示例代码中,我已经用硬编码行将事情明确化了,所以我不需要关于这方面的清理技巧。问题是每个
网络fwd
需要为
列表
馈电,因此每个后续层网络都会重新计算每个先前预测/输出,以获得先前层输出上的下一层邻接列表

它的效率非常低,但是我不能对邻接列表函数使用tensorflow操作。是否有任何方法可以通过模型的网络进行单次传递(每个
H*
只计算一次)

在运行时的autodiff框架(如Chainer)中,这不会是一个问题,因为您可以在正向执行期间执行邻接列表函数或任何其他非框架数据处理函数,但我真的很困惑如何在TF中执行此操作。

的建议对我来说是可行的

我如何使用tf.py_func:

# <initialize all child network variables>

# make adjacency list function interface for tf.py_func
def alist_func(h_in):
    """ Given constraints on the input to the func arg in tf.py_func,
        you may need to interface the numpy function if it's signature
        has other args or kwargs
    """
    return numpy_ops.get_adjacency_list(h_in, K)

# direct graph
data_in_shape = (11, None, num_points, D)
X_input = tf.placeholder(tf.float32, shape=data_in_shape, name='X_input')
X_pred, loss = tensor_ops.multi_func_model_fwd(X_input, var_scopes, alist_func)

# optimizer
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)

# train
for step in range(num_steps):
    x_batch = numpy_ops.next_minibatch(X_train, mb_size) # list of data inputs
    train.run(feed_dict={X_input: x_batch})



# in tensor_ops script, this is how I use tf.py_func
def meta_model_fwd(x_in, var_scopes, alist_fn, *args, **kwargs):
    alist = tf.py_func(alist_fn, [x_in[0]], tf.int32) # inp is float32, out is int32
    h = model_fwd(x_in[0], alist, var_scopes[0], *args, **kwargs)
    loss = loss_fn(h, x_in[1])
    for idx, vscope in enumerate(var_scopes[1:]):
        alist = tf.py_func(alist_fn, [h], tf.int32)
        h = model_fwd(h, alist, vscope, *args, **kwargs)
        loss += loss_fn(h, x_in[idx+1])
    return h, loss
#
#为tf.py_func生成邻接列表函数接口
定义函数(h_in):
“”“给定对tf.py_func中func参数的输入的约束,
如果numpy函数是签名函数,则可能需要对其进行接口
有其他args或kwargs
"""
返回numpy操作。获取邻接列表(h\U in,K)
#直接图
形状中的数据=(11,无,点数,D)
X_input=tf.placeholder(tf.float32,shape=data_in_shape,name='X_input')
X_pred,loss=tensor_ops.multi_func_model_fwd(X_input,var_scopes,alist_func)
#优化器
列车=tf.train.AdamOptimizer(学习率)。最小化(损失)
#训练
对于步进范围(num_步数):
x_批量=numpy_操作。下一个_小批量(x_系列,mb大小)#数据输入列表
运行(feed_dict={X_输入:X_批处理})
#在tensor_ops脚本中,这是我如何使用tf.py_func的
def meta_model_fwd(x_in,var_scopes,alist_fn,*args,**kwargs):
alist=tf.py_func(alist_fn[x_in[0]],tf.int32)#inp是float32,out是int32
h=型号(x在[0]中,列表,变量范围[0],*args,**kwargs)
损耗=损耗fn(h,x_in[1])
对于idx,枚举中的vscope(变量作用域[1:]):
alist=tf.py\u func(alist\u fn[h],tf.int32)
h=型号(h、ALIS、vscope、*args、**kwargs)
损耗+=损耗fn(h,x_in[idx+1])
返回h,损失

再次感谢米科拉

您说不能对邻接列表函数使用tensorflow ops。这是为什么?@mikkola:邻接列表函数调用
sklearn.neights
函数。这些是包含高度优化(cython)最近邻搜索和距离度量函数的高级函数。对于低维玩具数据,可能可以使用TF ops,但我经常遇到成对距离函数的内存问题,因为单个数据样本大约有32^3个点。在TF中有效地实现这些功能需要一整套支持功能,并且可能需要团队的共同努力。我只是一个凡人。你考虑过用这些函数包装吗?不,我不知道<代码> tf.Pyfunc,但是它看起来很有前途。我不熟悉tensorflow;谢谢分享!