具有动态形状TensorFlow的变量 [英] Variables with dynamic shape TensorFlow

查看:155
本文介绍了具有动态形状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))}))

这篇关于具有动态形状TensorFlow的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆