lesson2
lesson2
def sum_squared_error(y,t):
return 0.5*np.sum((y-t)**2)
print(sqe)
0.5925
##Cross-Entrpy Error
[ ]: import numpy as np
def cross_entropy_error(y,t):
delta = 1e-7
return -np.sum(t*np.log(y+delta))
y = [1.0,0.05,0.6,0.0,0.05,0.1,0.0,0.1,0.0,0.0]
t = [0,0,1,0,0,0,0,0,0,0]
0.510825457099338
• In the example, the output correct label is 0.6 and the crossp-entropy error is 0.51
##Mini-Batch Learning - In neural network training, some training data is selected, and training
is conducted for each group of data, which is called a mini-batch
[ ]: from google.colab import drive
drive.mount('/content/drive')
1
Mounted at /content/drive
[ ]: import sys, os
sys.path.append(os.pardir)
import numpy as np
print(x_train.shape)
print(t_train.shape)
(60000, 784)
(60000,)
##Numericial Differentiation
[ ]: import numpy as np
import matplotlib.pyplot as plt
def numerical_diff(f,x):
h = 1e-4
return (f(x+h) - f(x-h)) / (2*h)
def function_1(x):
return 0.01*x**2 + 0.1*x
x = np.arange(0.0,20.0, 0.1)
y = function_1(x)
plt.xlabel("x")
plt.ylabel("f(x)")
plt.plot(x,y)
plt.show()
2
##Partial Derivative
[ ]: def function_2(x):
return x[0]**2 + x[1]**2
##Gradient
[ ]: import numpy as np
def function_2(x):
return x[0]**2 + x[1]**2
def numericial_gradient(f,x):
h = 1e-4
grad = np.zeros_like(x) #Tra ve mot mang co kich thuoc giong voi mang da cho␣
↪va bang 0
3
x[idx] = tmp_val - h
fxh2 = f(x)
[ ]: array([6., 8.])
##Gradient descent
[ ]: import numpy as np
def numericial_gradient(f,x):
h = 1e-4
grad = np.zeros_like(x) #Tra ve mot mang co kich thuoc giong voi mang da cho␣
↪va bang 0
x[idx] = tmp_val - h
fxh2 = f(x)
def function_2(x):
return x[0]**2 + x[1]**2
for i in range(step_num):
grad = numericial_gradient(f,x)
x -= lr * grad
return x
4
[ ]: array([-6.11110793e-10, 8.14814391e-10])
Mounted at /content/drive
[ ]: import sys, os
sys.path.append(os.pardir)
import numpy as np
class simpleNet:
def __init__ (self):
self.W = np.random.randn(2,3)
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)
t = np.array([0,0,1])
net.loss(x,t)
def f(W):
return net.loss(x,t)
dW = numerical_gradient(f, net.W)
5
print(dW)
class TwoLayerNet:
def __init__(self, input_size, hidden_size, output_size, weight_init_std = 0.
↪01):
self.params = {}
self.params['W1'] = weight_init_std * np.random.randn(input_size,␣
↪hidden_size)
self.params['b1'] = np.zeros(hidden_size)
self.params['W2'] = weight_init_std * np.random.randn(hidden_size,␣
↪output_size)
self.params['b2'] = np.zeros(output_size)
a1 = np.dot(x, W1) + b1
z1 = sigmoid(a1)
a2 = np.dot(z1, W2) + b2
y = softmax(a2)
return y
return cross_entropy_error(y,t)
6
return accuracy
return grads
[17]: (10,)
train_loss_list = []
inters_num = 10000
train_size = x_train.shape[0]
batch_size = 100
learning_rate = 0.1
for i in range(inters_num):
batch_mask = np.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
7
loss = network.loss(x_batch, t_batch)
train_loss_list.append(loss)
train_loss_list = []
train_acc_list = []
test_acc_list = []
inter_per_epoch = max(train_size / batch_size, 1)
inters_num = 10000
batch_size = 100
learning_rate = 0.1
if i% inter_per_epoch == 0:
train_acc = network.accuracy(x_train, t_train)
test_acc = network.accuracy(x_test, t_test)
train_acc_list.append(train_acc)
test_acc_list.append(test_acc)
print("train acc, test acc | " + str(train_acc) + " , " + str(test_acc))