元素明智的乘法两个列表的tf.tensor在tensorflow中
在张量2中,在张量和数组之间进行元素乘法的最快方法是什么?
例如,如果张量t
(类型tf.tensor)为:
[[0, 1],
[2, 3]]
并且我们有一个数组a
(of type np.array):
[0, 1, 2]
我不知道
[[[0, 0],
[0, 0]],
[[0, 1],
[2, 3]],
[[0, 2],
[4, 6]]]
:输出。
What is the fastest way to do an element-wise multiplication between a tensor and an array in Tensorflow 2?
For example, if the tensor T
(of type tf.Tensor) is:
[[0, 1],
[2, 3]]
and we have an array a
(of type np.array):
[0, 1, 2]
I wand to have:
[[[0, 0],
[0, 0]],
[[0, 1],
[2, 3]],
[[0, 2],
[4, 6]]]
as output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这称为外乘积两个张量。通过利用TensorFlow的 broadcasting 规则:
结果很容易计算。
This is called the outer product of two tensors. It's easy to compute by taking advantage of Tensorflow's broadcasting rules:
results in
在 tensorflow ,我们有
tf.tensordot
,并且可以按照以下内容使用:
In tensorflow, we have
tf.tensordot
and can use this like below:您可以穿越数组并使用张量值执行标量乘法:
output:
另外,您可以使用以下方式使用列表理解以获得更多
可读
em>代码:You can traverse the array and perform a scalar multiplication with the tensor values:
Output:
Also, you can use list comprehension as follows to get a more
readable
code: