首页 > 解决方案 > 如何在张量流的占位符中接受张量列表?

问题描述

我想做以下事情:

out = sess.run(operation, feed_dict={x: x_value, , y: y_value})

在模型定义的某个地方,我使用了这个:

x = tf.placeholder(tf.float32, shape=(None, None, 3))
y = tf.placeholder(tf.float32, shape=(None, None, 3))
operation = some_function(x, y)

在哪里xy是图像。如何重写我的代码,以便我可以y用一个x. 我想做这样的事情:

x = tf.placeholder(tf.float32, shape=(None, None, 3))
y = []
x = []
for _y in y:
    operation = some_function(x, _y)
    z.append(operation)

现在我只是这样做:

x = tf.placeholder(tf.float32, shape=(None, None, 3))
y = tf.placeholder(tf.float32, shape=(None, None, None, 3)) #add extra dimension
z = tf.unstack(tf.map_fn(some_function, y))

但问题是当我这样做时,tf.unpack会抛出一个错误,说它不知道 y 的大小并失败。

这样做可以解决这个问题,我y用图像总数初始化 0 索引。

x = tf.placeholder(tf.float32, shape=(None, None, 3))
y = tf.placeholder(tf.float32, shape=(2, None, None, 3)) #add extra dimension
z = tf.unstack(tf.map_fn(some_function, y))

我使用以下方法评估值:

y_values = np.stack([y_value1, y_value2])
out = sess.run(operation, feed_dict={x: x_value, , y: y_values })

理想情况下,我希望它是可变的,我不必初始化占位符的值。可以说我希望它对所有不同的y值进行加权求和。每次只需 1y或 5y秒等。

标签: pythontensorflow

解决方案


推荐阅读