python - 如何将随机 TensorFlow 张量与标量相乘?
问题描述
我正在做:
with sess.as_default():
inputs = tf.random.uniform(
shape=[10],
minval=-1,
maxval=1,
dtype=tf.dtypes.float32
)
outputs = inputs * 2
return inputs, outputs
但是,这给了我不正确的价值观:
[-0.35871983 0.38419914 0.4842844 0.73863363 0.9198251 0.872777
-0.02426648 -0.23505187 0.12343764 0.98620296]
[-1.5875583 -0.20895815 -0.38869858 -0.22130203 0.00478601 0.06755686
-1.2828503 0.13379574 0.91710424 1.2863417 ]
第二个张量应该是第一个张量的 2 倍。
如果这很重要,我正在使用 Tensorflow 1.15.0
解决方案
您的代码不能以其当前形式运行。看起来您给出了函数的一部分,但没有给出def
语句。无论如何,这是 1.15.0 中的一个工作示例
import tensorflow as tf
inputs = tf.random.uniform(
shape=[10],
minval=-1,
maxval=1,
dtype=tf.dtypes.float32
)
outputs = inputs * 2
with tf.Session() as s:
out = s.run([inputs, outputs])
结果out
是
[array([ 0.39246178, 0.77169394, 0.05202556, 0.979944 , -0.9819634 ,
-0.56705594, 0.64549136, 0.59383535, -0.5887065 , 0.90850115],
dtype=float32),
array([ 0.78492355, 1.5433879 , 0.10405111, 1.959888 , -1.9639268 ,
-1.1341119 , 1.2909827 , 1.1876707 , -1.177413 , 1.8170023 ],
dtype=float32)]
张量流 2.x 方式:
您使用的是哪个版本的张量流?使用 tensorflow 2.1.0,结果翻了一番。虽然我没有明确使用会话(tensorflow 2.x 取消了它)。
import tensorflow as tf
def func():
inputs = tf.random.uniform(
shape=[10],
minval=-1,
maxval=1,
dtype=tf.dtypes.float32
)
outputs = inputs * 2
return inputs, outputs
func()
输出是
(<tf.Tensor: shape=(10,), dtype=float32, numpy=
array([-0.02820992, 0.50165296, -0.8978882 , 0.28159666, 0.00776339,
0.8920951 , 0.89258003, -0.25228214, -0.25257993, -0.32589626],
dtype=float32)>,
<tf.Tensor: shape=(10,), dtype=float32, numpy=
array([-0.05641985, 1.0033059 , -1.7957764 , 0.5631933 , 0.01552677,
1.7841902 , 1.7851601 , -0.5045643 , -0.50515985, -0.6517925 ],
dtype=float32)>)
推荐阅读
- javascript - 如何在 jQuery 中触发和处理表单提交?
- google-apps-script - 从自定义菜单在客户端浏览器中打开不同的电子表格
- android - 您如何获取本地时间并且在时间更改时不更新它?
- elasticsearch - 如何在字段上进行敏感和不敏感的大小写搜索
- python - 为什么熊猫布尔过滤会导致浮点数?
- java - 在 Robolectric 上运行第一次测试时出现问题
- powershell-3.0 - 如何检查Powershell中是否存在文件?
- node.js - 如何使用 Node.js 在 MS Bot Framework v4 中将对话数据记录到本地存储中
- c# - 尝试解析 JSON 时出现 JsonToken.StartArray 错误
- ruby-on-rails - 如何在 ruby 2.3.3 rails 5.2.0 中手动设置 css 和 js?