元素明智的乘法两个列表的tf.tensor在tensorflow中

发布于 2025-02-10 07:22:58 字数 342 浏览 0 评论 0原文

在张量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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居人 2025-02-17 07:22:58

这称为外乘积两个张量。通过利用TensorFlow的 broadcasting 规则:

import numpy as np
import tensorflow as tf

t = tf.constant([[0, 1],[2, 3]]) 
a = np.array([0, 1, 2])

# (2,2) x (3,1,1) produces the desired shape of (3,2,2)
result = t * a.reshape((-1, 1, 1))
# Alternatively: result = t * a[:, np.newaxis, np.newaxis]

print(result)

结果很容易计算。

<tf.Tensor: shape=(3, 2, 2), dtype=int32, numpy=
array([[[0, 0],
        [0, 0]],

       [[0, 1],
        [2, 3]],

       [[0, 2],
        [4, 6]]], dtype=int32)>

This is called the outer product of two tensors. It's easy to compute by taking advantage of Tensorflow's broadcasting rules:

import numpy as np
import tensorflow as tf

t = tf.constant([[0, 1],[2, 3]]) 
a = np.array([0, 1, 2])

# (2,2) x (3,1,1) produces the desired shape of (3,2,2)
result = t * a.reshape((-1, 1, 1))
# Alternatively: result = t * a[:, np.newaxis, np.newaxis]

print(result)

results in

<tf.Tensor: shape=(3, 2, 2), dtype=int32, numpy=
array([[[0, 0],
        [0, 0]],

       [[0, 1],
        [2, 3]],

       [[0, 2],
        [4, 6]]], dtype=int32)>
若能看破又如何 2025-02-17 07:22:58

tensorflow ,我们有 tf.tensordot,并且可以按照以下内容使用:

>>> a = tf.reshape(tf.range(4), (2,2))
>>> b = tf.range(3)
>>> tf.tensordot(b,a, axes=0)
<tf.Tensor: shape=(3, 2, 2), dtype=int32, numpy=
array([[[0, 0],
        [0, 0]],

       [[0, 1],
        [2, 3]],

       [[0, 2],
        [4, 6]]], dtype=int32)>

In , we have tf.tensordot and can use this like below:

>>> a = tf.reshape(tf.range(4), (2,2))
>>> b = tf.range(3)
>>> tf.tensordot(b,a, axes=0)
<tf.Tensor: shape=(3, 2, 2), dtype=int32, numpy=
array([[[0, 0],
        [0, 0]],

       [[0, 1],
        [2, 3]],

       [[0, 2],
        [4, 6]]], dtype=int32)>
百变从容 2025-02-17 07:22:58

您可以穿越数组并使用张量值执行标量乘法:

import tensorflow as tf

t = tf.constant([[0, 1],[2, 3]])
a = [0, 1, 2]

u = []
for i in a:
    u.append(t.numpy()*i)
u = tf.constant(u)
print(u)

output:

tf.Tensor(
[[[0 0]
  [0 0]]

 [[0 1]
  [2 3]]

 [[0 2]
  [4 6]]], shape=(3, 2, 2), dtype=int32)

另外,您可以使用以下方式使用列表理解以获得更多 可读 em>代码:

import tensorflow as tf

t = tf.constant([[0, 1],[2, 3]])
a = [0, 1, 2]

u = tf.constant([t.numpy()*i for i in a])
print(u)

You can traverse the array and perform a scalar multiplication with the tensor values:

import tensorflow as tf

t = tf.constant([[0, 1],[2, 3]])
a = [0, 1, 2]

u = []
for i in a:
    u.append(t.numpy()*i)
u = tf.constant(u)
print(u)

Output:

tf.Tensor(
[[[0 0]
  [0 0]]

 [[0 1]
  [2 3]]

 [[0 2]
  [4 6]]], shape=(3, 2, 2), dtype=int32)

Also, you can use list comprehension as follows to get a more readable code:

import tensorflow as tf

t = tf.constant([[0, 1],[2, 3]])
a = [0, 1, 2]

u = tf.constant([t.numpy()*i for i in a])
print(u)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文