【pythonでニューラルネットワーク#3】単回帰分析(ミニバッチ学習・SGD)

記事の目的

pythonで単回帰分析の勾配降下法を、ミニバッチ学習を使用して実装していきます。ここにある全てのコードは、コピペで再現することが可能です。

 

目次

  1. ミニバッチ学習の概要
  2. ライブラリとミニバッチ学習
  3. データの作成
  4. モデル
  5. モデルの学習
  6. 結果の可視化

 

1 ミニバッチ学習の概要

 

2 ライブラリとミニバッチ学習

# In[1]
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

# In[2]
data = np.arange(100)
data

# In[3]
batch_size = 20
batch_n = len(data) // batch_size
batch_index = np.arange(len(data))

for epoch in range(3):
  np.random.shuffle(batch_index)
  print(f"Epoch {epoch}") 

  for n in range(batch_n): 
    mb_index = batch_index[n*batch_size:(n+1)*batch_size]
    print(data[mb_index])

 

3 データの作成

# In[4]
x_train = np.random.normal(5, 1, 100)
t_train = 2 + 3*x_train + np.random.randn(100)
# x_train = (x_train - x_train.mean())/x_train.std()
# t_train = (t_train - t_train.mean())/t_train.std()

# In[5]
plt.scatter(x_train,t_train)}

 

4 モデル

# In[6]
def model(x ,w, b):
  y = w*x + b
  return y

# In[7]
def loss(y, t):
  L = sum((y-t)**2) / len(t)
  return L

# In[8]
def optimizer(x, y, t, w, b, lr):
  dL = 2*(y-t) / len(t)
  dw = np.dot(dL, x)
  db = dL.sum()
  w = w - lr*dw
  b = b - lr*db
  return (w,b)

 

5 モデルの学習

# In[9]
w,b = 0.5, 0.5

batch_size = 20
batch_n = len(x_train) // batch_size
batch_index = np.arange(len(x_train))

loss_all = []

for epoch in range(1, 100 + 1):

  np.random.shuffle(batch_index)

  for n in range(batch_n):
    mb_index = batch_index[n*batch_size:(n+1)*batch_size]
    y = model(x_train[mb_index], w, b)
    loss_train = loss(y, t_train[mb_index])

    w,b =optimizer(x_train[mb_index], y, t_train[mb_index], w, b, lr=1e-3)

  loss_all.append(loss_train)

  if epoch == 1 or epoch % 20 == 0:
    print(f"Epoch {epoch}, Loss {loss_train:.4f}")

 

6 結果の可視化

# In[10]
plt.plot(range(1,len(loss_all)+1), loss_all)

# In[11]
x = np.arange(2,9)
y = model(x,w,b)
plt.plot(x, y, color="black")
plt.scatter(x_train, t_train)