def corr2d_multi_in(X, K): # for x, k in zip(X, K): # print(d2l.corr2d(x, k)) return nd.add_n(*[d2l.corr2d(x, k) for x, k in zip(X, K)])
def corr2d_multi_in(X, K): # First, traverse along the 0th dimension (channel dimension) of X and K. # Then, add them together by using * to turn the result list into a # positional argument of the add_n function return nd.add_n(*[d2l.corr2d(x, k) for x, k in zip(X, K)])
def forward(self, x): return d2l.corr2d(x, self.weight.data()) + self.bias.data()
def convolution_test(): X = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) K = np.array([[0, 1], [2, 3]]) corr = d2l.corr2d(X, K) print("shape {}, value {}".format(corr.shape, corr))
K = np.array([[0, 1], [2, 3]]) corr = d2l.corr2d(X, K) print("shape {}, value {}".format(corr.shape, corr)) class Conv2D(nn.Block): def __init__(self, kernel_size, **kwargs): super().__init__(**kwargs) self.weight = self.params.get('weight', shape=kernel_size) self.bias = self.params.get('bias', shape=(1, )) def forward(self, x): return d2l.corr2d(x, self.weight.data()) + self.bias.data() def sample_black_white_image(): X = np.ones((6, 8)) X[:, 2:6] = 0 return X # kernel K = np.array([[1, -1]]) Y = d2l.corr2d(sample_black_white_image(), K) print("shape {}, value {}".format(Y.shape, Y)) # transposed Y_t = d2l.corr2d(sample_black_white_image().T, K) print("transposed : shape {}, value {}".format(Y_t.shape, Y_t))