def test_sliding2d(): """ Input: A (padded) valid Tensor for slides. [width, height] width_idx height_idx stride Output: A sliding Tensor [K, K], K = kernel_size """ n_samples = 2 width = 4 height = 5 a = Tensor(np.random.randn(n_samples, width, height), requires_grad=True) a.print() kernel_size = 2 # Symmetric Squared stride = 1 width_idx = 0 height_idx = 1 b = slide.Sliding2D(width_idx=width_idx, height_idx=height_idx, kernel_size=kernel_size, stride=stride)(a) b.print() b.backward() print(a.grad)
def test_padding2d(): n_samples = 4 width = 2 height = 3 a = Tensor(np.random.randn(n_samples, width, height), requires_grad=True) a.print() padding = 1 b = pad.Padding2D(padding=padding)(a) b.print() b.backward() print(a.grad)
def test_mse_loss(): n_samples = 5 n_output = 4 Y = Tensor(np.random.randn(n_samples, n_output), name='Y') Y_pred = Tensor(np.random.randn(n_samples, n_output), requires_grad=True, name='Y_pred') loss_ = mse.MSELoss('loss')(Y, Y_pred) Y.print() Y_pred.print() loss_.print() loss_.backward() print(Y_pred.grad)