-
2019-07-14 13:30:19
通过tf.reshape()对高维度张量降维,验证高维度张量相乘结果
最近遇到了需要将高于2维度的张量相乘的需求,通过互联网资源查到了先用tf.reshape()降到2维再运算的骚操作。下面验证这种操作的可靠性。
#测试多维矩阵乘法。问题来自于mul-attention模型的矩阵运算 #2019-7-14编辑 import tensorflow as tf import numpy as np #定义两个三维矩阵 #k.shape = [batch_size, seq_length, embedded_size] #w.shape = [embedded_size, d_k, h_num] k = tf.Variable(tf.random_uniform([3, 4, 5])) w = tf.Variable(tf.random_uniform([5, 6, 7])) #实现k与w的相乘,目标维度为[batch_size, seq_length, d_k, h_num] #通过reshape的方式,将矩阵降到2维,实现矩阵乘法,再通过reshape还原 k_2d = tf.reshape(k, [-1, 5]) w_2d = tf.reshape(w, [5, -1]) r_2d = tf.matmul(k_2d, w_2d) r_4d = tf.reshape(r_2d, [-1, 4, 6, 7]) #运算结果 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) r_untested = sess.run(r_4d) k_3d, w_3d = sess.run([k, w]) print(np.dot(k_3d[0,:,:],w_3d[:,:,0])) #array([[0.68616796, 1.147416 , 1.2250627 , 1.0267124 , 0.5699807 , 0.65192497], [1.2962847 , 0.63438064, 1.7439795 , 1.2534602 , 0.8585079 , 0.9535629 ], [1.0780972 , 1.466816 , 1.623834 , 1.4493611 , 0.9913111 , 1.1141219 ], [0.6155605 , 1.0016347 , 0.95043844, 0.8071648 , 0.6317205 , 0.8374078 ]], dtype=float32) print(r_untested[0,:,:,0]) #array([[0.68616796, 1.147416 , 1.2250627 , 1.0267124 , 0.5699807 , 0.65192497], [1.2962846 , 0.6343807 , 1.7439795 , 1.2534602 , 0.8585079 , 0.9535629 ], [1.0780972 , 1.4668161 , 1.623834 , 1.449361 , 0.9913111 , 1.1141219 ], [0.6155604 , 1.0016347 , 0.95043844, 0.8071648 , 0.6317204 , 0.8374078 ]], dtype=float32)
最终发现结果相同,大胆的用吧
更多相关内容 -
YDOOK:Pytorch教程:tensor 张量相乘 矩阵相乘
2021-11-28 20:27:21YDOOK:Pytorch教程:tensor 张量相乘 矩阵相乘 © YDOOK Jinwei Lin, shiye.work import torch import numpy as np t = torch.tensor([[1, 2, 3], [4, 5, 6]]) print(t) # 输出转置张量 print(t.T) # 矩阵相乘...YDOOK:Pytorch教程:tensor 张量相乘 矩阵相乘
© YDOOK Jinwei Lin, shiye.work
import torch import numpy as np t = torch.tensor([[1, 2, 3], [4, 5, 6]]) print(t) # 输出转置张量 print(t.T) # 矩阵相乘 t_mul = t.matmul(t.T) print('t = ', t) print('t.T = ', t.T) print('\n2X3张量与3X2张量相乘,得到2X2张量:') print('t_mul = ', t_mul)
outcome:
tensor([[1, 2, 3], [4, 5, 6]]) tensor([[1, 4], [2, 5], [3, 6]]) t = tensor([[1, 2, 3], [4, 5, 6]]) t.T = tensor([[1, 4], [2, 5], [3, 6]]) 2X3张量与3X2张量相乘,得到2X2张量: t_mul = tensor([[14, 32], [32, 77]]) Process finished with exit code 0
-
torch.matmul() 张量相乘
2021-04-14 09:10:15如果是二维的矩阵相乘,那就跟平时咱们做的矩阵乘法一样: a = torch.tensor([[1,2], [3,4]]) a Out[31]: tensor([[1, 2], [3, 4]]) b = torch.tensor([[2,2], [3,4]]) b Out[33]: tensor([[2, 2], [3, 4]])...如果是二维的矩阵相乘,那就跟平时咱们做的矩阵乘法一样:
a = torch.tensor([[1,2], [3,4]]) a Out[31]: tensor([[1, 2], [3, 4]]) b = torch.tensor([[2,2], [3,4]]) b Out[33]: tensor([[2, 2], [3, 4]]) torch.matmul(a, b) Out[34]: tensor([[ 8, 10], [18, 22]]) torch.matmul(a, b).shape Out[35]: torch.Size([2, 2])
如果维度更高呢?前面的维度必须要相同,然后最里面的两个维度符合矩阵相乘的形状限制:i×
j
,j
×k。a = torch.tensor([[[1,2], [3,4], [5,6]],[[7,8], [9,10], [11,12]]]) a Out[37]: tensor([[[ 1, 2], [ 3, 4], [ 5, 6]], [[ 7, 8], [ 9, 10], [11, 12]]]) a.shape Out[38]: torch.Size([2, 3, 2]) b = torch.tensor([[[1,2], [3,4]],[[7,8], [9,10]]]) b Out[40]: tensor([[[ 1, 2], [ 3, 4]], [[ 7, 8], [ 9, 10]]]) b.shape Out[41]: torch.Size([2, 2, 2]) torch.matmul(a, b) Out[42]: tensor([[[ 7, 10], [ 15, 22], [ 23, 34]], [[121, 136], [153, 172], [185, 208]]]) # a 和 b 的最外面的维度都是 2,相同。 # 最里面两个维度分别是 3 × 2 和 2 × 2,那么乘完以后就是 3 × 2 torch.matmul(a, b).shape Out[43]: torch.Size([2, 3, 2])
这里举一个例子,在某一篇论文的代码中,作者使用matmul的场景。
简单地说,就是用过matmul()函数实现subject 的 lookup
假设有下面这么一个矩阵,shape为[batch_size, 1, seq_len],该矩阵的含义是,最里面的每一个 [ ] s e q l e n []_{seq_len} []seqlen表示一个句子的序列,如果元素为1,则表示该下标可以作为subject的head index。并且在每一行中,只有一个1。也就是只有一个subject的head index。
现在有另外一个矩阵,shape为[batch_size, seq_len, bert_dim]。该矩阵的含义是整个batch的text([batch_size, seq_len])经过经过bert encoder之后得到的。
根据前面说的,二者相乘,得到的shape是[batch_size, 1, bert_dim]。
比如第一行 [ 0 , 1 , 0 , . . . . 0 , 0 ] × b e r t e n c o d e 之 后 的 矩 阵 = [ 0.3 , 0.1 , . . . , 0 ] [0,1,0,....0,0]×bert encode之后的矩阵=[0.3, 0.1, ..., 0] [0,1,0,....0,0]×bertencode之后的矩阵=[0.3,0.1,...,0]
最后得到的是subject在bert encode之后的空间中look up,或者说嵌入以后的向量。
-
Python:Tensorflow中两个稀疏张量相乘
2022-03-17 21:34:27Python:Tensorflow中两个稀疏张量相乘 博主在想让两个稀疏张量进行相乘时,发现不能用tf.matmul、tf.sparse_matmul、tf.sparse_tensor_dense_matmul,看来tf内置的没有对两个SparseTensor相乘的函数,于是,我在...Python:Tensorflow中两个稀疏张量相乘
博主在想让两个稀疏张量进行相乘时,发现不能用tf.matmul、tf.sparse_matmul、tf.sparse_tensor_dense_matmul,看来tf内置的没有对两个SparseTensor相乘的函数,于是,我在网上找了相关的方法,终于功夫不负有心人。
原文请点击这里
https://www.it1352.com/1886186.html。
代码如下:
M = tf.SparseTensor(indices=ind, values=val, dense_shape=[e, e]) #计算值后的反转后的图 M2=tf.matmul(tf.sparse_tensor_to_dense(M,0.0),tf.sparse_tensor_to_dense(M,0.0),a_is_sparse=True,b_is_sparse=True)
-
pytorch有关张量类型和张量相乘时的报错问题
2021-09-29 14:49:47最近在学习pytorch,在遇到张量点乘或者叉乘的时候总是会遇到各种报错,烦不胜烦 于是就想总结一下有关张量点乘或者叉乘时报错的问题,记录笔记 首先,张量有很多种类型: t1=torch.tensor([[1,2,3],[4,5,6]],dtype=... -
两个维度不同的张量相乘
2018-04-11 10:57:29context_vector = math_ops .reduce _sum( array_ops .reshape (attn_dist, [batch_size, - 1 , 1 , 1 ]) * encoder_state, [ 1 , 2 ...相乘结果为: (batch_size, attn_length, 1, attn_size) -
tensorflow 一个矩阵与多个矩阵相乘时的计算方法(二维和三维张量相乘为例)
2019-06-12 21:47:10https://blog.csdn.net/lanlana168/article/details/81136907 tensor二维与三维如何相加 -
torch.bmm() 与 torch.matmul()==>张量的相乘运算
2021-06-26 08:55:28torch.matmul()没有强制规定维度和大小,可以用利用广播机制进行不同维度的相乘操作 当进行操作的两个tensor都是3D时,两者等同。 torch.bmm() 用法: torch.bmm(input,mat2,out=None)→ Tensor torch.bmm()... -
张量基础2(张量乘法和对称)
2021-05-30 12:09:31张量乘法和超对称张量乘法张量内积张量乘以矩阵张量克罗内克积(KronerckerproductKronercker productKronerckerproduct)张量HadamardHadamardHadamard积Khatri−RaoKhatri-...定义:张量内积——同样大小的张量相乘即张 -
matlab三维张量按页相乘
2020-10-12 19:08:09matlab三维张量按页相乘 matlab里对三维张量来说,按页加减,点乘点除都是支持的,但按页做矩阵乘法不行。 定义函数: function y = newtimes(x,y) [mx,nx,p] = size(x); [ny,py,q] = size(y); x1 = reshape... -
Torch张量对应点乘、相乘
2019-03-30 20:25:531.张量点乘, z = x.mul(y) ,维度相同 2.张量相乘, z = x.mm(y) , (m,n) x (n,p) -
张量学习(7):张量乘积
2020-11-21 10:15:58向量的外积、张量内积、张量积(直积)、Kronecker乘积、Hadamard乘积、Khatri-Rao乘积、张量乘法(张量内积和张量乘以矩阵) -
乘以两个张量
2021-03-26 12:11:58我在Tensorflow中有两个张量,它们有以下两种形状:print(tf.valid_dataset.get_shape())print(weights1.get_shape())有结果:(10000, 784)(784, 1024)但是,如果我试图将它们相乘,就像这样:tf.matmul(tf_valid_... -
pytorch——张量乘法
2019-09-02 10:32:25* : a与b做*乘法,原则是如果a与b的size不同,则以某种方式将a或b进行复制,使得复制后的a和b的size相同,然后再将a和b做element-wise(对应元素相乘)的乘法。 * 高维张量: import torch a = torch.tensor([[1,2... -
pytorch 三维以内的张量乘法
2020-12-15 14:44:20'''tensor乘以标量数值''' y=torch.mul(x,value) #或者 y=x.mul(value) '''一维张量乘以一维张量''' # x.shape=torch([m]).y....'''二维张量相乘''' # x.shape=torch([m,n]),y.shape=torch([n,d]),z.shape=torch( -
张量基础学习 (三 张量代数运算———上)
2020-07-10 14:59:55经过前面几期博客的学习,我们初步认识了张量的基本概念,一些重要的符号与指标,坐标的变换规律和相应的张量的分量转化规律之后,接下里,将持续学习张量的各种运算法则与规律! 本人励志做最详细的博客撰写,所以... -
tensorflow多维张量计算
2018-09-20 18:50:38两个三维矩阵的乘法怎样计算呢?我通过实验发现,tensorflow把前面的维度当成...首先计算shape为(2, 2, 3)乘以shape为(2, 3, 2)的张量。 import tensorflow as tf import numpy as np a = tf.constant(np.arange(1,... -
pytorch 张量的乘积和矩阵乘法
2021-02-07 17:27:17张量的乘积( tensor 维度需要一致,对应 位置 元素相乘 ) tensor = torch.ones(4,4) tensor[:,1] = 2 print('tensor:') print(tensor) # 逐个元素相乘结果 print(f"tensor.mul(tensor): \n {tensor.mul(tensor)} \... -
张量乘积的三种形式
2021-12-16 17:42:36张量乘积的三种形式 直积或张量积. A.kron(B) 本文主要分为三个部分: 1、矩阵乘积 (matmul product) 2 、哈达马积 (hadamard product) 3 、克罗内克积 (Kronecker product) ------------------------------------...