【发布时间】:2018-03-14 16:29:05
【问题描述】:
我有一个占位符列表如下:
input_vars = []
input_vars.append(tf.placeholder(shape=[None, 5], dtype=tf.float32, name="place0"))
input_vars.append(tf.placeholder(shape=[None, 5], dtype=tf.float32, name="place1"))
input_vars.append(tf.placeholder(shape=[None, 5], dtype=tf.float32, name="place2"))
我想根据一个int占位符访问不同的占位符,如下:
which_input = tf.placeholder(tf.int32)
在会话中调用以下内容时:
input_vars[which_input]
我收到以下错误:
TypeError:列表索引必须是整数,而不是张量
我尝试使用有效的 tf.gather,但是当我想在密集层中提供选定的占位符时,如下所示:
helpme = tf.gather(input_vars, which_input)
l_in = tf.layers.dense(inputs=helpme, units=64, activation=tf.nn.relu, trainable=True)
我收到以下错误:
ValueError: Input 0 of layer dense_4 is in compatible with the layer: its rank is undefined, but the layer requires a defined rank.
这是会话运行信息:
x = [[1,2,3,4,5]]
x.append([6,7,8,9,10])
y = [[5,4,3,2,1]]
y.append([5,3,2,1,1])
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
dictd = dict()
dictd[input_vars[0]] = x
dictd[input_vars[1]] = y
dictd[input_vars[2]] = x
dictd[which_input] = 2
print sess.run(l_in, feed_dict=dictd)
我错过了什么吗?如何做到这一点?
【问题讨论】:
-
您要解决的实际基础任务是什么?也许有一种解决方法不需要“占位符的占位符”。
-
@mikkola 我有一个计算图,它需要为相同的神经网络提供不同类型的输入(input_vars)来计算损失函数。因此需要。
-
我还是有点迷茫。你不需要同时喂它们,一次只喂一个?那么损失函数有什么样的签名呢?
-
@mikkola :损失函数有多个部分。每个部分都需要相同的神经网络来评估不同的输入并产生输出。然后使用这个产生的输出来计算损失函数。例如,
loss = loss_1 + loss_2;loss_1要求我将input_var[0]提供给 NN,获取其输出,转换为标量并添加到损失函数。同样,loss_2要求我将input_var[1]提供给 NN,获取其输出,转换为标量并添加到损失函数。所以总的来说,我应该能够在同一个计算图中选择给 NN 的输入。 -
也许您可以同时在不同的占位符中同时输入两个
input_vars,并设置您的计算图以使用同一网络处理两者?如果您将网络设置为具有两个与share the variables 相同的分支,例如权重和偏差,您应该能够做到这一点。
标签: tensorflow