TensorFlow-根据另一个变量的形状动态定义变量的形状

Fil*_*ixo 6 python tensorflow

假设我有一个张量,x其维数在图形初始化时未定义。

我可以使用以下方法获取形状:

x_shape = tf.shape(input=x)
Run Code Online (Sandbox Code Playgroud)

现在,如果我想根据x_shape使用中定义的值创建一个变量:

y = tf.get_variable(variable_name="y", shape=[x_shape[0], 10])
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,因为传递给参数shape的值必须为intand not Tensor。如何在不使用占位符的情况下创建这样的动态形状变量?

Ish*_*nal 0

第一个参数是变量名。

x = tf.zeros(shape=[10,20])
x_shape = x.shape
variable_name ='y'
y = tf.get_variable(variable_name, shape=[x_shape[0], x_shape[1]])
Run Code Online (Sandbox Code Playgroud)