Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created February 12, 2021 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/7a7d8cb41770d3aff9f5687271b5c3a4 to your computer and use it in GitHub Desktop.
Save amankharwal/7a7d8cb41770d3aff9f5687271b5c3a4 to your computer and use it in GitHub Desktop.
# Mini-batch Gradient Descent
n_iterations = 50
minibatch_size = 20
m = 100
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)
X_b = np.c_[np.ones((100, 1)), X] # add x0 = 1 to each instance
np.random.seed(42)
theta = np.random.randn(2,1) # random initialization
t0, t1 = 200, 1000
def learning_schedule(t):
return t0 / (t + t1)
t = 0
for epoch in range(n_iterations):
shuffled_indices = np.random.permutation(m)
X_b_shuffled = X_b[shuffled_indices]
y_shuffled = y[shuffled_indices]
for i in range(0, m, minibatch_size):
t += 1
xi = X_b_shuffled[i:i+minibatch_size]
yi = y_shuffled[i:i+minibatch_size]
gradients = 2/minibatch_size * xi.T.dot(xi.dot(theta) - yi)
eta = learning_schedule(t)
theta = theta - eta * gradients
print(theta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment