tensordot 的源码解读
生活随笔
收集整理的這篇文章主要介紹了
tensordot 的源码解读
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://console.cloud.google.com/bigquery?project=heidao-market&p=heidao-market&d=mafia1_dmd&t=exchange_info&page=table
我一直以為,談?wù)摂?shù)學(xué)計(jì)算只能用數(shù)學(xué)語言,就是用數(shù)學(xué)公式一步步推導(dǎo)
這個(gè)世界是,可知的,簡潔的
復(fù)雜的是現(xiàn)象,不是本質(zhì)
在線性代數(shù)中,介紹的矩陣乘法只有一種,可以退出,在程序中,不論多么高維度的矩陣乘法運(yùn)算,其本質(zhì)也是二維矩陣運(yùn)算
import numpy as np np.random.seed(10) size_A=size=(3,4,2) A = np.random.randint(5, size=size_A) size_B=(4,2,3) B = np.random.randint(12, size=size_B) axes1=[[1,2], [0,1]]# axes1=[[2], [1]] C=np.tensordot(A, B, axes1) print(C) [[ 76 100 76][ 71 39 36][112 94 94]] def transe_shape(arr,axes,ff,size):nda=arr.ndimnotin = [k for k in range(nda) if k not in list(axes)]def mul(a):d=1for p in a :d=d*size[p]return daxes=list(axes)axm=mul(axes)nom=mul(notin)if ff:newaxes = notin + axesreturn newaxes,notin,(nom,axm)else:newaxes =axes+ notin return newaxes,notin,(axm,nom)axes_A,notin_A,shape_fA=transe_shape(A,axes1[0],1,size_A) # A1=A.transpose([1, 2, 0]).reshape((3*1,2))A1=A.transpose(axes_A).reshape(shape_fA)axes_B,notin_B,shape_fB=transe_shape(B,axes1[1],0,size_B) # B1=B.transpose( [1, 0, 2]).reshape((2,3*2)) B1=B.transpose( axes_B).reshape(shape_fB)#矩陣相乘 C1=A1@B1out_shape=[size_A[k] for k in notin_A ] +[size_B[k] for k in notin_B ] # print(C1.reshape((1,3,3,2))==C) print(C1.reshape(out_shape)==C) [[ True True True][ True True True][ True True True]]總結(jié)
以上是生活随笔為你收集整理的tensordot 的源码解读的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: keras 自定义层 2
- 下一篇: ubuntu 输入法问题:显示繁体