tensorflow:占位符形状的循环值[无]

时间:2019-02-27 17:53:31

标签: python tensorflow

我的问题如下:如果我有一个'None'形状的占位符,如何在tensorflow中编写代码以循环'None'形状的值?例如,给定一个占位符,如果我预定义了形状,则可以编写:

[i for i in range(placeholder.shape[0].value)] 

但是当形状为'None'时如何编写代码?我尝试过

[i for i in tf.range(tf.shape(placeholder)[0])] 

它根本不起作用。我也尝试使用tf.while_loop,但仍然无法获得预期的结果。谁能帮我?非常感谢

1 个答案:

答案 0 :(得分:0)

也许您可以使用tf.scan:

import numpy as np
import tensorflow as tf

tf.InteractiveSession()
placeholder = tf.placeholder(dtype=tf.int32)  # The shape of the placeholder is unknown for now
def fn(_, x):
  y = 2 * x  # Do something with this value
  return y
shape = tf.scan(fn, tf.shape(placeholder))
feed_dict = {placeholder: np.zeros((2, 3, 4))}
print(shape.eval(feed_dict))