张量流中具有未指定维度的张量 [英] Tensor with unspecified dimension in tensorflow

查看:33
本文介绍了张量流中具有未指定维度的张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩 tensorflow 时遇到了以下代码的问题:

I'm playing around with tensorflow and ran into a problem with the following code:

def _init_parameters(self, input_data, labels):

    # the input shape is (batch_size, input_size)
    input_size = tf.shape(input_data)[1]

    # labels in one-hot format have shape (batch_size, num_classes)
    num_classes = tf.shape(labels)[1]

    stddev = 1.0 / tf.cast(input_size, tf.float32)

    w_shape = tf.pack([input_size, num_classes], 'w-shape')
    normal_dist = tf.truncated_normal(w_shape, stddev=stddev, name='normaldist')
    self.w = tf.Variable(normal_dist, name='weights')

(我正在按照 this 中的建议使用 tf.pack问题,因为我遇到了同样的错误)

(I'm using tf.pack as suggested in this question, since I was getting the same error)

当我运行它时(从一个调用这个的更大的脚本),我得到这个错误:

When I run it (from a larger script that invokes this one), I get this error:

ValueError: initial_value must have a shape specified: Tensor("normaldist:0", shape=TensorShape([Dimension(None), Dimension(None)]), dtype=float32)

我尝试在交互式 shell 中复制该过程.实际上,normal_dist 的维度是未指定的,尽管提供的值确实存在:

I tried to replicate the process in the interactive shell. Indeed, the dimensions of normal_dist are unspecified, although the supplied values do exist:

In [70]: input_size.eval()
Out[70]: 4

In [71]: num_classes.eval()
Out[71]: 3

In [72]: w_shape.eval()
Out[72]: array([4, 3], dtype=int32)

In [73]: normal_dist.eval()
Out[73]: 
array([[-0.27035281, -0.223277  ,  0.14694688],
       [-0.16527176,  0.02180306,  0.00807841],
       [ 0.22624688,  0.36425814, -0.03099642],
       [ 0.25575709, -0.02765726, -0.26169327]], dtype=float32)

In [78]: normal_dist.get_shape()
Out[78]: TensorShape([Dimension(None), Dimension(None)])

这很奇怪.Tensorflow 生成向量但不能说出它的形状.我做错了什么吗?

This is weird. Tensorflow generates the vector but can't say its shape. Am I doing something wrong?

推荐答案

正如 Ishamael 所说,所有张量都有一个静态形状,这在图构建时是已知的,并且可以使用 Tensor.get_shape();和动态形状,它仅在运行时已知,可通过获取张量的值或将其传递给像 tf.shape.在许多情况下,静态和动态形状是相同的,但它们可以不同 - 静态形状可以部分定义 - 以便允许动态形状从一个步骤到下一步骤变化.

As Ishamael says, all tensors have a static shape, which is known at graph construction time and accessible using Tensor.get_shape(); and a dynamic shape, which is only known at runtime and is accessible by fetching the value of the tensor, or passing it to an operator like tf.shape. In many cases, the static and dynamic shapes are the same, but they can be different - the static shape can be partially defined - in order allow the dynamic shape to vary from one step to the next.

在你的代码中 normal_dist 有一个部分定义的静态形状,因为 w_shape 是一个计算值.(TensorFlow 有时会尝试评估这些在图构建时计算的值,但它卡在 tf.pack 处.)它推断形状 TensorShape([Dimension(None), Dimension(None)]),意思是行数和列数未知的矩阵",因为它知道w_shape是一个长度为2的向量,所以结果normal_dist一定是2-维度.

In your code normal_dist has a partially-defined static shape, because w_shape is a computed value. (TensorFlow sometimes attempts to evaluate these computed values at graph construction time, but it gets stuck at tf.pack.) It infers the shape TensorShape([Dimension(None), Dimension(None)]), which means "a matrix with an unknown number of rows and columns," because it knowns that w_shape is a vector of length 2, so the resulting normal_dist must be 2-dimensional.

您有两种选择来处理这个问题.您可以按照 Ishamael 的建议设置静态形状,但这需要您在图形构建时了解该形状.例如,以下可能有效:

You have two options to deal with this. You can set the static shape as Ishamael suggests, but this requires you to know the shape at graph construction time. For example, the following may work:

normal_dist.set_shape([input_data.get_shape()[1], labels.get_shape()[1]])

或者,您可以将 validate_shape=False 传递给 tf.Variable 构造函数.这允许您创建一个具有部分定义形状的变量,但它限制了稍后可以在图中推断出的静态形状信息的数量.

Alternatively, you can pass validate_shape=False to the tf.Variable constructor. This allows you to create a variable with a partially-defined shape, but it limits the amount of static shape information that can be inferred later on in the graph.

这篇关于张量流中具有未指定维度的张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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