Professional Documents
Culture Documents
Page 1 of 7
Alamein University Neural Networks
Faculty of Computer science & Course Code: AIE231
Engineering
Page 2 of 7
Alamein University Neural Networks
Faculty of Computer science & Course Code: AIE231
Engineering
Page 3 of 7
Alamein University Neural Networks
Faculty of Computer science & Course Code: AIE231
Engineering
class SGD:
def __init__(self, lr=0.01, max_iter=1000, batch_size=32,
tol=1e-3):
# learning rate of the SGD Optimizer
self.learning_rate = lr
# maximum number of iterations for SGD Optimizer
self.max_iteration = max_iter
# mini-batch size of the data
self.batch_size = batch_size
# tolerance for convergence for the theta
self.tolerence_convergence = tol
# Initialize model parameters to None
self.theta = None
Page 4 of 7
Alamein University Neural Networks
Faculty of Computer science & Course Code: AIE231
Engineering
SGD Implementation
We will create a random dataset with 100 rows and 5 columns and we fit
our Stochastic gradient descent Class on this data. Also, We will use predict
method from SGD
# Create random dataset with 100 rows and 5 columns
X = np.random.randn(100, 5)
# create corresponding target value by adding random
# noise in the dataset
y = np.dot(X, np.array([1, 2, 3, 4, 5]))\
+ np.random.randn(100) * 0.1
# Create an instance of the SGD class
model = SGD(lr=0.01, max_iter=1000,
batch_size=32, tol=1e-3)
model.fit(X, y)
Page 5 of 7
Alamein University Neural Networks
Faculty of Computer science & Course Code: AIE231
Engineering
This cycle of taking the values and adjusting them based on different
parameters in order to reduce the loss function is called back-
propagation.
This curve consists of three distinct parts: learning rates that don't learn fast enough,
and don't take the model anywhere; an area of steepest descent, eventually leading into
an optimal or near-optimal learning rate. Past the edge of that curve you get noise, and
eventually divergence (this is where the learning rate is too large).
Page 6 of 7
Alamein University Neural Networks
Faculty of Computer science & Course Code: AIE231
Engineering
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
import numpy as np
from keras.callbacks import LearningRateScheduler
# Perform training.
clf.fit(X_train, y_train, epochs=10, batch_size=500)
clf.compile(loss='categorical_crossentropy', optimizer=SGD())
# Perform training.
clf.fit(X_train, y_train, epochs=10, batch_size=500, callbacks=[lr_sche
d])
Page 7 of 7