|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import numpy as np from p88func import * class simpleNet: def __init__(self): self.W = np.random.randn(2,3) def predict(self,x): return np.dot(x,self.W) def loss(self,x,t): z = self.predict(x) y = softmax(z) loss = cross_entropy_error(y,t) return loss net = simpleNet() print(net.W) x = np.array([0.6,0.9]) p = net.predict(x) print(p) print(np.argmax(p)) t= np.array([0,0,1]) d = net.loss(x,t) print(d) def f(W): return net.loss(x,t) dW = numerical_gradient(f,net.W) print(dW) |
(2,3) 같은 다차원배열일 경우의 기울기를 구한다. simpleNet은 입력 값 x와 normal distribution로 초기화한 W 값(가중치) 를 행렬곱하여 (xW) 그 값을 softmax 값으로 변환하고 정답레이블(t)와…