import numpy as np w = np.array(range(15)).reshape(3,5) b = np.array(range(5)) c = b.reshape(1,b.shape[0]) print w.shape,b.shape, c.shape #(3, 5) (5,) (1, 5) a = w -b d = w -c print a, a.shape """ [[ 0 0 0 0 0] [ 5 5 5 5 5] [10 10 10 10 10]] (3, 5) """ print d, d.shape """ [[ 0 0 0 0 0] [ 5 5 5 5 5] [10 10 10 10 10]] (3, 5) """ #b += c #ValueError: non-broadcastable output operand with shape (5,) doesn't match the broadcast shape (1,5) c += b print c, c.shape #[[0 2 4 6 8]] (1, 5)
可以看见b(5,)可以向c(1,5)或其他(n,5)传播,这个传播是单向的这个也有点意思 https://stackoverflow.com/questions/32109319/how-to-implement-the-relu-function-in-numpy
numpy库中一维矩阵的一些坑点
最新推荐文章于 2023-02-22 12:25:51 发布