def forward(self, input): # self._saved_for_backward(input) output, input_with_pad = conv2d_forward(input, self.W, self.b, self.kernel_size, self.pad) self._saved_for_backward(input_with_pad) return output
import numpy as np import functions n = 1 c_in = 1 c_out = 1 h_in = 3 w_in = 3 pad = 0 k = 2 input = np.arange(n * c_in * h_in * w_in).reshape(n, c_in, h_in, w_in) W = np.arange(c_out * c_in * k * k).reshape(c_out, c_in, k, k) b = np.zeros(shape=(c_out)) print "input:\n", input print "W:\n", W print "b:\n", b output = functions.conv2d_forward(input, W, b, k, pad) print "output:\n", output """ print functions.conv2d_backward(input, np.log(output), W, b, k, pad) """
[2., 2., 2., 2., 1.5, 1.5], [2., 2., 1.75, 1.75, 1.5, 1.5], [2., 2., 1.75, 1.75, 1.5, 1.5], [0., 0., 1.5, 1.5, 0.75, 0.75], [0., 0., 1.5, 1.5, 0.75, 0.75]]]]) return pool_inp, pool_out, grad_pool_out, grad_pool_inp inp, w, b, grad_out = get_fake_data() out, grad_inp, grad_w, grad_b = get_conv_answer() pool_inp, pool_out, grad_pool_out, grad_pool_inp = get_pool_data_answer() try: test_conv_out = conv2d_forward(inp, w, b, 2, 0) except: print( '[FAILED] conv2d_forward: bug in codes, can not run for inp.shape = (4, 3, 6, 6), w.shape = (4, 3, 2, 2), ker_size = 2, pad = 0' ) else: if test_conv_out.shape != out.shape: print('[ERROR] conv2d_forward: output shape is not correct') else: diff = test_conv_out - out if abs(diff).max() > 1e-5: print('[ERROR] conv2d_forward: output value is not correct') else: print('[PASS] conv2d_forward: all correct') flag = 1
def forward(self, input): self._saved_for_backward(input) output = conv2d_forward(input, self.W, self.b, self.kernel_size, self.pad) self.output_data = output return output