tf.shape() 在张量流中得到错误的形状 [英] tf.shape() get wrong shape in tensorflow

查看:43
本文介绍了tf.shape() 在张量流中得到错误的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义一个这样的张量:

I define a tensor like this:

x = tf.get_variable("x", [100])

但是当我尝试打印张量的形状时:

But when I try to print shape of tensor :

print(tf.shape(x))

我得到Tensor("Shape:0", shape=(1,), dtype=int32),为什么输出的结果不应该是shape=(100)

I get Tensor("Shape:0", shape=(1,), dtype=int32), why the result of output should not be shape=(100)

推荐答案

tf.shape(input, name=None) 返回表示输入形状的一维整数张量.

tf.shape(input, name=None) returns a 1-D integer tensor representing the shape of input.

您正在寻找: x.get_shape() 返回 x 变量的 TensorShape.

You're looking for: x.get_shape() that returns the TensorShape of the x variable.

更新:由于这个答案,我写了一篇文章来阐明 Tensorflow 中的动态/静态形状:https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

Update: I wrote an article to clarify the dynamic/static shapes in Tensorflow because of this answer: https://pgaleone.eu/tensorflow/2018/07/28/understanding-tensorflow-tensors-shape-static-dynamic/

这篇关于tf.shape() 在张量流中得到错误的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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