将参数馈送到tensorflow中的占位符

时间:2015-12-17 06:44:36

标签: python tensorflow

我正在尝试进入tensorflow,设置网络,然后向其提供数据。出于某种原因,我最终得到错误消息ValueError: setting an array element with a sequence。我做了一个我想要做的最小例子:

import tensorflow as tf
K = 10

lchild = tf.placeholder(tf.float32, shape=(K))
rchild = tf.placeholder(tf.float32, shape=(K))
parent = tf.nn.tanh(tf.add(lchild, rchild))

input = [ tf.Variable(tf.random_normal([K])),
          tf.Variable(tf.random_normal([K])) ]

with tf.Session() as sess :
    print(sess.run([parent], feed_dict={ lchild: input[0], rchild: input[1] }))

基本上,我正在建立一个带占位符的网络和一系列我想要学习的输入嵌入,然后我尝试运行网络,将输入嵌入到其中。从我通过搜索错误消息可以看出,我的feed_dict可能有问题,但我看不出任何明显的不匹配。维数。

那么,我错过了什么,或者我是如何完全倒退的呢?

编辑:我编辑了上述内容,以澄清输入表示需要学习的嵌入。我想问题可以更明确地提出:是否可以使用占位符作为参数?

1 个答案:

答案 0 :(得分:7)

输入应该是numpy数组。

因此,只需编写tf.Variable(tf.random_normal([K]))而不是np.random.randn(K),一切都应按预期工作。

编辑(我的回答后问题澄清了):

可以使用占位符作为参数,但方式略有不同。例如:

lchild = tf.placeholder(tf.float32, shape=(K))
rchild = tf.placeholder(tf.float32, shape=(K))
parent = tf.nn.tanh(tf.add(lchild, rchild))
loss = <some loss that depends on the parent tensor or lchild/rchild>
# Compute gradients with respect to the input variables
grads = tf.gradients(loss, [lchild, rchild])

inputs = [np.random.randn(K), np.random.randn(K)]
for i in range(<number of iterations>):
    np_grads = sess.run(grads, feed_dict={lchild:inputs[0], rchild:inputs[1])
    inputs[0] -= 0.1 * np_grads[0]
    inputs[1] -= 0.1 * np_grads[1]

然而,这不是最好或最简单的方法。它的主要问题是,在每次迭代时,您都需要将numpy数组复制进出会话(可能在不同的设备上运行,如GPU)。

占位符通常用于提供模型外部的数据(如文本或图像)。使用tensorflow实用程序解决它的方法如下:

lchild = tf.Variable(tf.random_normal([K])
rchild = tf.Variable(tf.random_normal([K])
parent = tf.nn.tanh(tf.add(lchild, rchild))
loss = <some loss that depends on the parent tensor or lchild/rchild>
train_op = tf.train.GradientDescentOptimizer(loss).minimize(0.1)

for i in range(<number of iterations>):
    sess.run(train_op)

# Retrieve the weights back to numpy:
np_lchild = sess.run(lchild)