无法在 TensorFlow 中转换部分转换的张量 [英] Cannot convert a partially converted tensor in TensorFlow

查看:24
本文介绍了无法在 TensorFlow 中转换部分转换的张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TensorFlow 中有很多方法需要指定形状,例如 truncated_normal:

There are many methods in TensorFlow that requires specifying a shape, for example truncated_normal:

tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

我有一个用于输入形状 [None, 784] 的占位符,其中第一个维度是 None 因为批次大小可能会有所不同.我可以使用固定的批量大小,但它仍然与测试/验证集大小不同.

I have a placeholder for the input of shape [None, 784], where the first dimension is None because the batch size can vary. I could use a fixed batch size but it still would be different from the test/validation set size.

我无法将此占位符提供给 tf.truncated_normal,因为它需要完全指定的张量形状.让 tf.truncated_normal 接受不同张量形状的简单方法是什么?

I cannot feed this placeholder to tf.truncated_normal because it requires a fully specified tensor shape. What is a simple way to having tf.truncated_normal accept different tensor shapes?

推荐答案

您只需要将其作为单个示例输入,但要以批处理形式输入.所以这意味着为形状添加一个额外的维度,例如

You just need to feed it in as a single example but in the batched shape. So that means adding an extra dimension to the shape e.g.

batch_size = 32 # set this to the actual size of your batch
tf.truncated_normal((batch_size, 784), mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

这样它就会适合"占位符.

This way it will "fit" into the placeholder.

如果您希望 batch_size 发生变化,您还可以使用:

If you expect batch_size to change you can also use:

tf.truncated_normal(tf.shape(input_tensor), mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

input_tensor 可以是占位符,也可以是任何会添加噪声的张量.

Where input_tensor could be a placeholder or just whatever tensor is going to have this noise added to it.

这篇关于无法在 TensorFlow 中转换部分转换的张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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