メモ:小ロット勾配降下パラメータ解実現


"""
       

  :                 
  :      
"""

import numpy as np

X = 2 * np.random.rand(100, 1)
Y = 4 + 3 * X + np.random.randn(100, 1)
X = np.c_[np.ones((100, 1)), X]

n_epochs = 10000
batch_size = 10
m = 100
theta = np.random.randn(2, 1)
learning_rate = 0.001

if __name__ == "__main__":

    for epoch in range(n_epochs):
        #       s
        indexs_list = np.arange(m)
        np.random.shuffle(indexs_list)

        for batch in range(int(m/batch_size)):
            index_list = indexs_list[batch*batch_size:batch*batch_size + batch_size]
            Xb = X[index_list]
            yb = Y[index_list]
            gradients = 1/batch_size * Xb.T.dot(Xb.dot(theta) - yb)
            theta -= learning_rate * gradients

    print(theta)
[[4.07610616]
 [2.97844257]]