-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
#20250917Launch
#https://github.com/oreilly-japan/deep-learning-from-scratch/tree/master
import numpy as np
import sys,os
#from function sigmoid, softmax, cross_entropy
sys.path.append(os.pardir)
from common.functions import numerical_gradient
from common.functions import cross_entropy_error
from common.gradient import numerical_gradient
def sigmoid(x):
return 1/ (1+np.exp(-x))
def softmax(x):
return max(x,0,dtype=float)
def cross_entropy_error(y,t): #pytorchで代用できるかもしれない。
return -sumtnp.log(y)
class ThreeLayerNet:
def init(self, input_size, hidden_size1, hidden_size2, output_size, weight_init_std=0.01):
self.params = {}
# 1層目: Input → Hidden1
self.params["W1"] = weight_init_std * np.random.randn(input_size, hidden_size1) #weight_init_stdで初期化できる!
self.params["b1"] = np.zeros(hidden_size1) #np.zeros()で全部0に書き換え!
# 2層目: Hidden1 → Hidden2
self.params["W2"] = weight_init_std * np.random.randn(hidden_size1, hidden_size2)
self.params["b2"] = np.zeros(hidden_size2)
# 3層目: Hidden2 → Output
self.params["W3"] = weight_init_std * np.random.randn(hidden_size2, output_size)
self.params["b3"] = np.zeros(output_size)
def predict(self, x0):
W1, W2, W3 = self.params["W1"], self.params["W2"], self.params["W3"]
b1, b2, b3 = self.params["b1"], self.params["b2"], self.params["b3"]
a1 = np.dot(x0, W1) + b1
x1 = sigmoid(a1)
a2 = np.dot(x1, W2) + b2
x2 = sigmoid(a2)
a3 = np.dot(x2, W3) + b3
y = softmax(a3)
return y
def loss(self, x, t):
y = self.predict(x)
return cross_entropy_error(y, t)
def accuracy(self, x, t):
y = self.predict(x)
y = np.argmax(y, axis=1)
t = np.argmax(t, axis=1)
return np.sum(y == t) / float(x.shape[0])
def numerical_gradient(self, x, t):
loss_W = lambda W: self.loss(x, t)
grads = {}
for key in ["W1", "b1", "W2", "b2", "W3", "b3"]:
grads[key] = numerical_gradient(loss_W, self.params[key])