我有一个占位符列表如下:
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:层dense_4的输入0与该层不兼容:它的级别未定义,但该层需要定义等级。
下面是运行信息的会话:
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)我是不是遗漏了什么?这是如何做到的呢?
发布于 2018-03-14 08:29:05
您只需要重新调整来自tf.gather的输出,正如在this答案中所解释的:
l_in = tf.layers.dense(inputs=tf.reshape(helpme, shape=[-1,5]), units=64, activation=tf.nn.relu, trainable=True)https://stackoverflow.com/questions/49271153
复制相似问题