|
| 1 | +import numpy as np |
| 2 | + |
| 3 | + |
| 4 | +# 基于元素的relu原生实现 |
| 5 | +def naive_relu(x): |
| 6 | + # x是一个二维张量 |
| 7 | + assert len(x.shape) == 2 |
| 8 | + |
| 9 | + x = x.copy() # 避免覆盖输入的张量 |
| 10 | + for i in range(x.shape[0]): |
| 11 | + for j in range(x.shape[1]): |
| 12 | + x[i, j] = max(x[i, j], 0) |
| 13 | + return x |
| 14 | + |
| 15 | + |
| 16 | +# 基于元素的add原生实现 |
| 17 | +def naive_add(x, y): |
| 18 | + # x和y是二维张量 |
| 19 | + assert len(x.shape) == 2 |
| 20 | + assert x.shape == y.shape |
| 21 | + |
| 22 | + x = x.copy() # 避免覆盖输入的张量 |
| 23 | + for i in range(x.shape[0]): |
| 24 | + for j in range(x.shape[1]): |
| 25 | + x[i, j] += y[i, j] |
| 26 | + return x |
| 27 | + |
| 28 | + |
| 29 | +# 只能广播最后一个维度、最后两个维度。。。 |
| 30 | +x = np.array([[[5, 78, 2, 34, 0], |
| 31 | + [6, 79, 3, 35, 1], |
| 32 | + [7, 80, 4, 36, 2]], |
| 33 | + [[5, 78, 2, 34, 0], |
| 34 | + [6, 79, 3, 35, 1], |
| 35 | + [7, 80, 4, 36, 2]], |
| 36 | + [[5, 78, 2, 34, 0], |
| 37 | + [6, 79, 3, 35, 1], |
| 38 | + [7, 80, 4, 36, 2]], |
| 39 | + [[5, 78, 2, 34, 0], |
| 40 | + [6, 79, 3, 35, 1], |
| 41 | + [7, 80, 4, 36, 2]]]) |
| 42 | +# y = np.array([1, 2, 3]) # 维度不匹配 |
| 43 | +# y = np.array([1, 2, 3, 4]) # 维度不匹配 |
| 44 | +# y = np.array([1, 2, 3, 4, 5]) |
| 45 | +# y = np.array([[1, 2, 3, 4, 5], |
| 46 | +# [2, 3, 4, 5, 6], |
| 47 | +# [1, 2, 3, 4, 5], |
| 48 | +# [1, 2, 3, 4, 5]]) # 维度不匹配 |
| 49 | +y = np.array([[1, 2, 3, 4, 5], |
| 50 | + [2, 3, 4, 5, 6], |
| 51 | + [1, 2, 3, 4, 5]]) |
| 52 | +z = x + y |
| 53 | +print(z) |
| 54 | + |
| 55 | + |
| 56 | +# 矩阵加向量的原生实现 |
| 57 | +def naive_add_matrix_and_vector(x, y): |
| 58 | + # x是一个二维张量 |
| 59 | + # y是一个向量 |
| 60 | + assert len(x.shape) == 2 |
| 61 | + assert len(y.shape) == 1 |
| 62 | + assert x.shape[1] == y.shape[0] |
| 63 | + |
| 64 | + x = x.copy() # 避免覆盖输入的张量 |
| 65 | + for i in range(x.shape[0]): |
| 66 | + for j in range(x.shape[1]): |
| 67 | + x[i, j] += y[j] |
| 68 | + return x |
| 69 | + |
| 70 | + |
| 71 | +# x是一个随机张量,大小为(64, 3, 32, 10) |
| 72 | +x = np.random.random((64, 3, 32, 10)) |
| 73 | +# y是一个随机张量,大小为(32, 10) |
| 74 | +y = np.random.random((32, 10)) |
| 75 | + |
| 76 | +# z的大小为(64, 3, 32, 10) |
| 77 | +z = np.maximum(x, y) |
| 78 | +print(z.shape) |
| 79 | + |
| 80 | + |
| 81 | +# 向量dot的原生实现 |
| 82 | +def naive_vector_dot(x, y): |
| 83 | + # x和y是向量 |
| 84 | + assert len(x.shape) == 1 |
| 85 | + assert len(y.shape) == 1 |
| 86 | + assert x.shape[0] == y.shape[0] |
| 87 | + |
| 88 | + z = 0 |
| 89 | + for i in range(x.shape[0]): |
| 90 | + z += x[i] * y[i] |
| 91 | + return z |
| 92 | + |
| 93 | + |
| 94 | +# 矩阵-向量dot的原生实现 |
| 95 | +def naive_matrix_vector_dot(x, y): |
| 96 | + # x是矩阵 |
| 97 | + # y是向量 |
| 98 | + assert len(x.shape) == 2 |
| 99 | + assert len(y.shape) == 1 |
| 100 | + assert x.shape[1] == y.shape[0] |
| 101 | + |
| 102 | + z = np.zeros(x.shape[0]) |
| 103 | + for i in range(x.shape[0]): |
| 104 | + # for j in range(x.shape[1]): |
| 105 | + # z[i] += x[i][j] * y[j] |
| 106 | + z[i] = naive_vector_dot(x[i, :], y) |
| 107 | + return z |
| 108 | + |
| 109 | + |
| 110 | +x = np.array([[1, 2, 3], |
| 111 | + [1, 2, 3]]) |
| 112 | +y = np.array([1, 2, 3]) |
| 113 | +z = naive_matrix_vector_dot(x, y) |
| 114 | +print(z) |
| 115 | + |
| 116 | + |
| 117 | +# 矩阵dot的原生实现 |
| 118 | +def naive_matrix_dot(x, y): |
| 119 | + # x和y是矩阵 |
| 120 | + assert len(x.shape) == 2 |
| 121 | + assert len(y.shape) == 2 |
| 122 | + assert x.shape[1] == y.shape[0] |
| 123 | + |
| 124 | + z = np.zeros((x.shape[0], y.shape[1])) |
| 125 | + for i in range(y.shape[1]): |
| 126 | + z[:, i] = naive_matrix_vector_dot(x, y[:, i]) |
| 127 | + return z |
| 128 | + |
| 129 | + |
| 130 | +x = np.array([[1, 2, 3], |
| 131 | + [1, 2, 3]]) |
| 132 | +y = np.array([[1, 2, 3, 4], |
| 133 | + [1, 2, 3, 4], |
| 134 | + [1, 2, 3, 4]]) |
| 135 | +z = naive_matrix_dot(x, y) |
| 136 | +print(z) |
0 commit comments