栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

TensorFlow:生成一个随机常数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TensorFlow:生成一个随机常数

tf.constant()
运算需要花费numpy的阵列(或其它隐式转换为一个numpy的阵列),并返回一个
tf.Tensor
其值是相同的数组。它并
没有 接受
tf.Tensor
作为其参数。

另一方面,

tf.random_normal()
op
tf.Tensor
每次运行时都会根据给定的分布返回其值是随机生成的。由于它返回a
tf.Tensor
,因此不能用作的参数
tf.constant()
。这说明了
TypeError
(与的使用无关
tf.InteractiveSession
,因为它在构建图形时发生)。

我假设您希望您的图包括一个张量,该张量是(i)首次使用时随机生成的,而(ii)之后是常量。有两种方法可以做到这一点:

  1. 使用NumPy生成随机值,然后将其放入

    tf.constant()
    ,就像您在问题中所做的那样:

    some_test = tf.constant(np.random.normal(loc=0.0, scale=1.0, size=(2, 2)).astype(np.float32))
  2. (可能更快,因为它可以使用GPU生成随机数)使用TensorFlow生成随机值并将其放在

    tf.Variable

    some_test = tf.Variable(tf.random_normal([2, 2], mean=0.0, stddev=1.0, dtype=tf.float32)

    sess.run(some_test.initializer) # Must run this before using

    some_test



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号