无法在 TensorFlow 中转换部分转换的张量

编程入门 行业动态 更新时间:2024-10-24 22:28:20
本文介绍了无法在 TensorFlow 中转换部分转换的张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 03:53:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1403902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:张量   TensorFlow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!