def _np_replication_pad2d_grad(src, dest): (dx_height, dx_width) = (input.shape[h_idx], input.shape[w_idx]) (dy_height, dy_width) = (output.shape[h_idx], output.shape[w_idx]) numpy_src = np.ones(src.shape, np.int32) numpy_dest = np.zeros(dest.shape, np.int32) array_src = FlattenArray(numpy_src) array_dest = FlattenArray(numpy_dest) src_num = src.shape[c_idx] * src.shape[h_idx] * src.shape[w_idx] dest_num = dest.shape[c_idx] * dest.shape[h_idx] * dest.shape[w_idx] elements_num = src.shape[0] * src_num for iter_n in range(elements_num): coords = Index2Coordinate(iter_n, src.shape) (n, c, i, j) = (coords[0], coords[c_idx], coords[h_idx], coords[w_idx]) ip_x = ip_y = 0 if j < pad_left: ip_x = pad_left elif j >= pad_left and j < dx_width + pad_left: ip_x = j else: ip_x = dx_width + pad_left - 1 if i < pad_top: ip_y = pad_top elif i >= pad_top and i < dx_height + pad_top: ip_y = i else: ip_y = dx_height + pad_top - 1 ip_x = ip_x - pad_left ip_y = ip_y - pad_top src_index = n * src_num + c * dy_width * dy_height + i * dy_width + j dest_index = (n * dest_num + c * dx_width * dx_height + ip_y * dx_width + ip_x) array_dest[dest_index] += array_src[src_index] numpy_dest = Array2Numpy(array_dest, dest.shape) return numpy_dest
def _np_zero_pad2d_grad(src, dest, padding): (c_idx, h_idx, w_idx) = (1, 2, 3) pad_left = padding[0] pad_right = padding[1] pad_top = padding[2] pad_bottom = padding[3] (dx_height, dx_width) = (dest.shape[h_idx], dest.shape[w_idx]) (dy_height, dy_width) = (src.shape[h_idx], src.shape[w_idx]) numpy_src = np.ones(src.shape, np.int32) numpy_dest = np.zeros(dest.shape, np.int32) array_src = FlattenArray(numpy_src) array_dest = FlattenArray(numpy_dest) src_num = src.shape[c_idx] * src.shape[h_idx] * src.shape[w_idx] dest_num = dest.shape[c_idx] * dest.shape[h_idx] * dest.shape[w_idx] elements_num = src.shape[0] * src_num for iter_n in range(elements_num): coords = Index2Coordinate(iter_n, src.shape) (n, c, i, j) = (coords[0], coords[c_idx], coords[h_idx], coords[w_idx]) ip_x = ip_y = 0 if (j >= pad_left and j < dx_width + pad_left and (i >= pad_top) and (i < dx_height + pad_top)): ip_x = j - pad_left ip_y = i - pad_top src_index = n * src_num + c * dy_width * dy_height + i * dy_width + j dest_index = (n * dest_num + c * dx_width * dx_height + ip_y * dx_width + ip_x) array_dest[dest_index] += array_src[src_index] numpy_dest = Array2Numpy(array_dest, dest.shape) return numpy_dest
def _np_constant_pad2d_grad(src, dest): dx_height, dx_width = input.shape[h_idx], input.shape[w_idx] dy_height, dy_width = output.shape[h_idx], output.shape[w_idx] numpy_src = np.ones(src.shape, np.int32) numpy_dest = np.zeros(dest.shape, np.int32) array_src = FlattenArray(numpy_src) array_dest = FlattenArray(numpy_dest) src_num = src.shape[c_idx] * src.shape[h_idx] * src.shape[w_idx] dest_num = dest.shape[c_idx] * dest.shape[h_idx] * dest.shape[w_idx] elements_num = src.shape[0] * src_num for iter_n in range(elements_num): coords = Index2Coordinate(iter_n, src.shape) n, c, i, j = coords[0], coords[c_idx], coords[h_idx], coords[w_idx] ip_x = ip_y = 0 if (j >= pad_left and j < (dx_width + pad_left) and i >= pad_top and i < (dx_height + pad_top)): ip_x = j - pad_left ip_y = i - pad_top src_index = n * src_num + c * dy_width * dy_height + i * dy_width + j dest_index = (n * dest_num + c * dx_width * dx_height + ip_y * dx_width + ip_x) array_dest[dest_index] += array_src[src_index] numpy_dest = Array2Numpy(array_dest, dest.shape) return numpy_dest
def _np_replication_pad2d_grad(src, dest, padding): c_idx, h_idx, w_idx = 1, 2, 3 pad_left = padding[0] pad_right = padding[1] pad_top = padding[2] pad_bottom = padding[3] dx_height, dx_width = dest.shape[h_idx], dest.shape[w_idx] dy_height, dy_width = src.shape[h_idx], src.shape[w_idx] numpy_src = np.ones(src.shape, np.int32) numpy_dest = np.zeros(dest.shape, np.int32) array_src = FlattenArray(numpy_src) array_dest = FlattenArray(numpy_dest) src_num = src.shape[c_idx] * src.shape[h_idx] * src.shape[w_idx] dest_num = dest.shape[c_idx] * dest.shape[h_idx] * dest.shape[w_idx] elements_num = src.shape[0] * src_num for iter_n in range(elements_num): coords = Index2Coordinate(iter_n, src.shape) n, c, i, j = coords[0], coords[c_idx], coords[h_idx], coords[w_idx] ip_x = ip_y = 0 if j < pad_left: ip_x = pad_left elif j >= pad_left and j < (dx_width + pad_left): ip_x = j else: ip_x = dx_width + pad_left - 1 if i < pad_top: ip_y = pad_top elif i >= pad_top and i < (dx_height + pad_top): ip_y = i else: ip_y = dx_height + pad_top - 1 ip_x = ip_x - pad_left ip_y = ip_y - pad_top src_index = n * src_num + c * dy_width * dy_height + i * dy_width + j dest_index = n * dest_num + c * dx_width * dx_height + ip_y * dx_width + ip_x array_dest[dest_index] += array_src[src_index] numpy_dest = Array2Numpy(array_dest, dest.shape) return numpy_dest