def test_append(): A = np.ones((1, INT_OVERFLOW)) B = np.ones((2, INT_OVERFLOW)) A.attach_grad() with mx.autograd.record(): C = np.append(A, B, axis=0) assert C.shape == (3, INT_OVERFLOW) assert C[2][0] == 1 C.backward() assert A.grad.shape == (1, INT_OVERFLOW) assert A[0][0] == 1
import math from mxnet import np, npx, gluon, autograd from mxnet.gluon import nn from d2l import mxnet as d2l npx.set_np() #とりあえずNo1だけについて W1 = np.array([[0.9, 0.3, 0.9], [-0.7, 0.3, -0.7]]) W2 = np.array([-0.3, -0.9, -0.7]) b1 = np.array([1]) b2 = np.array([1]) params = [W1, b1, W2, b2] for param in params: param.attach_grad() X = np.array([1, 1, 1]) X.attach_grad() y_true = np.array([1]) with autograd.record(): H = np.tanh(np.dot(W1, X)) O = np.tanh(np.dot(W2, np.append(H, np.array([1])))) L = (y_true - O)**2 L = 1 / 2 * L L.backward() print('predicted value:', O) print('updated W1', W1 - W1.grad) print('updated W2', W2 - W2.grad)
import math from mxnet import np, npx, gluon, autograd from mxnet.gluon import nn from d2l import mxnet as d2l npx.set_np() initial_w1 = np.array([[0.9, 0.3, 0.9], [-0.7, 0.3, -0.7]]) initial_w2 = np.array([-0.3, -0.9, -0.7]) true_labels = np.array([1, -1, 1, -1]) inputs = np.array([[1, 1, 1], [0, 1, 1], [0, 0, 1], [1, 0, 1]]) #No1についての予測, 以下No. n のm層目に関してh_n_m などと記述 h_1_1 = np.dot(initial_w1, inputs[0]) z_1_1 = np.append(np.tanh(h1), np.array([1])) z_1_3 = np.dot(initial_w2, z_1_1) y_hat_1 = np.tanh(z_1_3) y_hat_1 #No1についてのロス