具有动态形状TensorFlow的变量

问题描述:

我需要在TensorFlow中创建一个矩阵来存储一些值.诀窍是矩阵必须支持动态形状.

I need to create a matrix in TensorFlow to store some values. The trick is the matrix has to support dynamic shape.

我正在尝试执行与numpy中相同的操作:

I am trying to do the same I would do in numpy:

myVar = tf.Variable(tf.zeros((x,y), validate_shape=False)

其中x=(?)y=2.但这不起作用,因为零不支持``部分已知的TensorShape'',那么,我应该如何在TensorFlow中做到这一点?

where x=(?) and y=2. But this does not work because zeros does not support 'partially known TensorShape', so, How should I do this in TensorFlow?

如果您知道会话的形状,则可能会有所帮助.

If you know the shape out of the session, this could help.

import tensorflow as tf
import numpy as np

v = tf.Variable([], validate_shape=False)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(v, feed_dict={v: np.zeros((3,4))}))
    print(sess.run(v, feed_dict={v: np.zeros((2,2))}))