Gradient Descent: The Stochastic Update
Learn to code the perceptron trick using the Stochastic Gradient Descent.
Exploratory data analysis
We have two features X1 and X2 and a label. Each feature has ten data points and the label is 0 or 1. Make a decision boundary that separates the two classes.
Letβs look at the
π Note: Remember, we can only apply the perceptron algorithm if the data is linearly separable. For this, we need to draw the points on the graph to visualize the data.
Draw the data points on the
Coding the perceptron training rule
Press + to interact
import numpy as npdef sigmoid(x): # sigmoid activation function"""The sigmoid activation function on the input x"""return 1 / (1 + np.exp(-x))def forward_propagation(input_data, weights, bias):"""Computes the forward propagation operation of a perceptron andreturns the output after applying the sigmoid activation function"""# take the dot product of input and weight and add the biasreturn sigmoid(np.dot(input_data, weights) + bias) # the perceptron equationdef calculate_error(Y, Y_predicted):"""Computes the binary cross entropy error"""# the cross entropy errorreturn - Y * np.log(Y_predicted) - (1 - Y) * np.log(1 - Y_predicted)def gradient(target, actual, X):""""Gradient of weights and bias"""dW = - (target - actual) * X # gradient of weightsdb = target - actual # gradient of biasreturn dW, dbdef update_parameters(W, b, dW, db, learning_rate):"""Updating the weights and bias value"""W = W - dW * learning_rate # update weightb = b - db * learning_rate # update learning ratereturn W, bdef train(X, Y, weights, bias, epochs, learning_rate):"""Training the perceptron using stochastic update"""sum_error = 0.0for i in range(epochs): # outer loop iterates epoch timesfor j in range(len(X)): # inner loop iterates length of X timesY_predicted = forward_propagation(X[j], weights.T, bias) # predicted labelsum_error = sum_error + calculate_error (Y[j], Y_predicted) # compute errordW, db = gradient(Y[j], Y_predicted, X[j]) # find gradientweights, bias = update_parameters(weights, bias , dW, db, learning_rate) # update parametersprint("epochs:", i, "error:", sum_error)sum_error = 0 # re-intialize sum error at the end of each epochreturn weights, bias# Initialize parameters# declaring two data pointsX = np.array([[2.78, 2.55],[1.46, 2.36],[3.39, 4.40],[1.38, 1.85],[3.06, 3.00],[7.62, 2.75],[5.33, 2.08],[6.92, 1.77],[8.67, -0.24],[7.67, 3.50]])Y = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) # actual labelweights = np.array([0.0, 0.0]) # weights of perceptronbias = 0.0 # bias valuelearning_rate = 0.1 # learning rateepochs = 10 # set epochsprint("Before training")print("weights:", weights, "bias:", bias)weights, bias = train(X, Y, weights, bias, epochs, learning_rate) # train the functionprint("\nAfter training")print("weights:", weights, "bias:", bias)# Predict valuespredicted_labels = forward_propagation(X, weights.T, bias)print("Target labels: ", Y)print("Predicted label:", (predicted_labels > 0.5) * 1)
Explanation
Initialization parameters
The table summarizes the initialized parameters:
Variables | Definition |
---|---|