如何使用从 TFRecords 读取的值作为 tf.set_shape 的参数? [英] How can I use values read from TFRecords as arguments to tf.set_shape?

查看:21
本文介绍了如何使用从 TFRecords 读取的值作为 tf.set_shape 的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

height = tf.cast(features['height'],tf.int32)宽度 = tf.cast(features['width'],tf.int32)image.set_shape(tf.pack([height,width,3]))

但是,我收到以下错误:

TypeError: int() 参数必须是字符串或数字,而不是张量"

有人能指出我正确的方向吗?谢谢.

解决方案

您不能通过 tf.TensorTensor.set_shape().使用从 TFRecord 读取的值作为 set_shape() 的参数的唯一方法是对其进行评估(例如使用 sess.run())并传递结果 NumPy数组到set_shape().

这种限制的原因有点微妙.Tensor.set_shape() 用于对特定 Tensor(即符号值)可以保持的所有值的形状进行断言.特别是,它用于填补形状推断留下的空白,当程序员对该数据的了解比库可以推断的更多时.但是,这些断言只能使用在图构建时可用的信息.动态变化的形状使用特殊值 Dimension(None)(对于未知维度)和 TensorShape(None)(对于未知等级的张量)表示.>

整个 TensorFlow 的意图是大多数操作应该能够在没有静态已知形状的情况下工作,以便可以使用动态变化形状的张量.但是,在使用的 图像处理操作中有一些例外用于裁剪/填充:

这三个操作要求形状是静态已知的.然而,它们只是 tf.pad()tf.slice() 利用静态形状避免在每一步都进行不必要的计算.我们一直在慢慢地从图像操作中删除这种依赖关系,但请随时提出有关此的 GitHub 问题.同时,您可以直接使用较低级别的操作来解决动态张量缺乏形状推断的问题.

I would like to do something like this:

height = tf.cast(features['height'],tf.int32)
width = tf.cast(features['width'],tf.int32)
image.set_shape(tf.pack([height,width,3]))

However, I get the following error:

TypeError: int() argument must be a string or a number, not 'Tensor'

Can someone point me in the right direction? Thank you.

解决方案

You cannot pass a tf.Tensor to Tensor.set_shape(). The only way to use a value read from a TFRecord as the argument to set_shape() is to evaluate it (e.g. using sess.run()) and pass the resulting NumPy array to set_shape().

The reason for this limitation is somewhat subtle. Tensor.set_shape() is used to make assertions about the shapes of all values that a particular Tensor (i.e. a symbolic value) can hold. In particular, it is used to fill the gaps left by shape inference, when the programmer knows more about that data than the library can infer. However, these assertions can only use information that is available at graph construction time. Dynamically-varying shapes are represented using the special values Dimension(None) (for an unknown dimension) and TensorShape(None) (for a tensor of unknown rank).

The intent throughout TensorFlow is that most operations should be able to work without statically known shapes, so that tensors of dynamically varying shape can be used. However, there are a few exceptions among the image processing ops used for cropping/padding:

These three ops require that the shape is statically known. However, they are simply convenience wrappers around tf.pad() and tf.slice() that take advantage of the static shape to avoid doing needless computation in every step. We've been slowly removing this dependency from the image ops, but feel free to raise a GitHub issue about this. In the meantime, you can use the lower-level ops directly to work around the lack of shape inference for your dynamic tensors.

这篇关于如何使用从 TFRecords 读取的值作为 tf.set_shape 的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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