今天在定义一个内积运算的时候,发现了一个模糊的问题,两个变量,都是ndarray数组,一个维度是(200,10),一个维度是(10,),两者求内积后得到的新数组的维度是(200,),该如何理解?
一、数据的维度
max_degree = 10 # 多项式的最大阶数
n_train, n_test = 100, 100 # 训练和测试数据集的大小
true_w = np.zeros(max_degree)
true_w[0 : 4] = np.array([5, 1.2, -3.4, 5.6])
features = np.random.normal(size = (n_train + n_test, 1))
np.random.shuffle(features)
poly_features = np.power(features, np.arange(max_degree).reshape(1, -1))
for i in range(max_degree):
    poly_features[:, i] /= math.gamma(i + 1)
labels = np.dot(poly_features, true_w)
labels += np.random.normal(scale = 0.1, size = labels.shape)
true_w, features, poly_features, labels = [torch.tensor(x, dtype = torch.float32) for x in [true_w, features, poly_features, labels]]
 
我们看一下true_w, features, poly_features, labels四个变量的维度分别为多少:
首先是true_w,维度为(10,1)

 再然后是features,维度为(200,1):

再然后是poly_features,维度为(200,10):

最后是labels,维度为(200,):

二、问题的理解
labels = np.dot(poly_features, true_w)
 
我们可以知道,labels是由poly_features和true_w做内积得到的。
形状为(10,)的数组是一维数组。它的结构应该与此类似:a=[1,2,3,4,5]。
我们举一个例子:
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([1, 2, 3, 4, 5, 6, 7, 8]).reshape((-1, 4))
c = np.dot(b, a)
print(c)
 
得到的结果为:
(PyTorch) D:\Code Project>D:/Anaconda/envs/PyTorch/python.exe "d:/Code Project/demo.py"
[30 70]
 
数据类型为ndarray数组。
那么30和70分别是怎么计算的?
不难理解:
 
     
      
       
        
         30
        
        
         =
        
        
         1
        
        
         ∗
        
        
         1
        
        
         +
        
        
         2
        
        
         ∗
        
        
         2
        
        
         +
        
        
         3
        
        
         ∗
        
        
         3
        
        
         +
        
        
         4
        
        
         ∗
        
        
         4
        
        
         =
        
        
         1
        
        
         +
        
        
         4
        
        
         +
        
        
         9
        
        
         +
        
        
         16
        
        
         =
        
        
         30
        
       
       
         30 = 1*1+2*2+3*3+4*4 = 1+4+9+16=30 
       
      
     30=1∗1+2∗2+3∗3+4∗4=1+4+9+16=30
 
     
      
       
        
         70
        
        
         =
        
        
         1
        
        
         ∗
        
        
         5
        
        
         +
        
        
         2
        
        
         ∗
        
        
         6
        
        
         +
        
        
         3
        
        
         ∗
        
        
         7
        
        
         +
        
        
         4
        
        
         ∗
        
        
         8
        
        
         =
        
        
         5
        
        
         +
        
        
         12
        
        
         +
        
        
         21
        
        
         +
        
        
         32
        
        
         =
        
        
         38
        
        
         +
        
        
         32
        
        
         =
        
        
         70
        
       
       
         70 = 1*5+2*6+3*7+4*8=5+12+21+32=38+32=70 
       
      
     70=1∗5+2∗6+3∗7+4∗8=5+12+21+32=38+32=70
 内积后为一维数组。
同理可得,每一行做内积,共计200行,得到200个数据,内积后为一维数组。


















