请问tf.matmul()和tf.multiply()在矩阵乘法上的具体区别是什么?

2026-05-21 23:363阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计556个文字,预计阅读时间需要3分钟。

请问tf.matmul()和tf.multiply()在矩阵乘法上的具体区别是什么?

我这就不多说了,大家还是直接看代码吧!flyfish

我就废话不多说了,大家还是直接看代码吧~

flyfish

# a # [[1, 2, 3], # [4, 5, 6]] a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) # b1 # [[ 7, 8], # [ 9, 10], # [11, 12]] b1 = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) #b2 #[[ 7 8 9] # [10 11 12]] b2 = tf.constant([7, 8, 9, 10, 11, 12], shape=[2, 3]) # c矩阵相乘 第一个矩阵的列数(column)等于第二个矩阵的行数(row) # [[ 58, 64], # [139, 154]] c = tf.matmul(a, b1) # d`数元素各自相乘 #[[ 7 16 27] # [40 55 72]] d = tf.multiply(a, b2) #维度必须相等 with tf.Session(): print(d.eval())

关于其他计算

b3 = tf.constant([7, 8, 9,], shape=[1, 3]) tf.multiply(a, b3) 结果是 [[ 7 16 27] [28 40 54]] b4 = tf.constant([7, 8], shape=[2, 1]) tf.multiply(a, b4) 结果是 [[ 7 14 21] [32 40 48]] b5 = tf.constant([7], shape=[1, 1]) tf.multiply(a, b5) 结果是 [[ 7 14 21] [28 35 42]]

补充知识:tensor matmul的对3维张量的处理

请问tf.matmul()和tf.multiply()在矩阵乘法上的具体区别是什么?

torch.matmul(a,b)处理的一般是a和b的最后两个维度,假设a的维度为B*F*M,b也为B*F*M, 在对a,b做相乘操作的时候,需要完成对B的维度顺序的变换,通过permute(0, 2, 1)变换为B*M*F。

通过变换后进行torch.matmul(a,b)得到结果为B*F*F,在除了最后两个维度的的之前维度上都被认为是Batch。

示例1:

>>> import torch >>> a=torch.rand((1000,5,10)) >>> b=torch.rand((1000,10,12)) >>> c=torch.matmul(a,b) >>> c.shape torch.Size([1000, 5, 12])

在处理不同维度时,会通过广播来合并除最后两个维度外的其他维度,如对于A*B*F*M与B*M*F的matmul,结果为A*B*F*F

示例2:

>>> a=torch.rand((50,1000,5,10)) >>> b=torch.rand((1000,10,12)) >>> c=torch.matmul(a,b) >>> c.shape torch.Size([50, 1000, 5, 12])

以上这篇关于tf.matmul() 和tf.multiply() 的区别说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

标签:

本文共计556个文字,预计阅读时间需要3分钟。

请问tf.matmul()和tf.multiply()在矩阵乘法上的具体区别是什么?

我这就不多说了,大家还是直接看代码吧!flyfish

我就废话不多说了,大家还是直接看代码吧~

flyfish

# a # [[1, 2, 3], # [4, 5, 6]] a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) # b1 # [[ 7, 8], # [ 9, 10], # [11, 12]] b1 = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) #b2 #[[ 7 8 9] # [10 11 12]] b2 = tf.constant([7, 8, 9, 10, 11, 12], shape=[2, 3]) # c矩阵相乘 第一个矩阵的列数(column)等于第二个矩阵的行数(row) # [[ 58, 64], # [139, 154]] c = tf.matmul(a, b1) # d`数元素各自相乘 #[[ 7 16 27] # [40 55 72]] d = tf.multiply(a, b2) #维度必须相等 with tf.Session(): print(d.eval())

关于其他计算

b3 = tf.constant([7, 8, 9,], shape=[1, 3]) tf.multiply(a, b3) 结果是 [[ 7 16 27] [28 40 54]] b4 = tf.constant([7, 8], shape=[2, 1]) tf.multiply(a, b4) 结果是 [[ 7 14 21] [32 40 48]] b5 = tf.constant([7], shape=[1, 1]) tf.multiply(a, b5) 结果是 [[ 7 14 21] [28 35 42]]

补充知识:tensor matmul的对3维张量的处理

请问tf.matmul()和tf.multiply()在矩阵乘法上的具体区别是什么?

torch.matmul(a,b)处理的一般是a和b的最后两个维度,假设a的维度为B*F*M,b也为B*F*M, 在对a,b做相乘操作的时候,需要完成对B的维度顺序的变换,通过permute(0, 2, 1)变换为B*M*F。

通过变换后进行torch.matmul(a,b)得到结果为B*F*F,在除了最后两个维度的的之前维度上都被认为是Batch。

示例1:

>>> import torch >>> a=torch.rand((1000,5,10)) >>> b=torch.rand((1000,10,12)) >>> c=torch.matmul(a,b) >>> c.shape torch.Size([1000, 5, 12])

在处理不同维度时,会通过广播来合并除最后两个维度外的其他维度,如对于A*B*F*M与B*M*F的matmul,结果为A*B*F*F

示例2:

>>> a=torch.rand((50,1000,5,10)) >>> b=torch.rand((1000,10,12)) >>> c=torch.matmul(a,b) >>> c.shape torch.Size([50, 1000, 5, 12])

以上这篇关于tf.matmul() 和tf.multiply() 的区别说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

标签: