Description
1 Perceptron (50 points)
File name: perceptron.py
Implement a function in python:
perceptron_train(X,Y)
that takes training data as input and outputs the weights w, and the bias b of the perceptron. Your function
should handle any real-valued features, with feature vectors in any dimension, and binary labels. Write-Up:
describe your implementation concisely.
Implement a function in python:
perceptron_test(X_test, Y_test, w, b)
that takes testing data, the perceptron weights and bias as input and returns the accuracy on the testing
data. Write-Up: describe your implementation concisely.
You should get the following output for the perceptron when you run the test script.
Pe rcep t r on : 1. 0 1
Pe rcep t r on : 0. 4 2 8 5 7 1 4 2 8 5 7 1 4 2 8 5 5 2
2 Gradient Descent (50 points)
Implement a function in python: gradient descent(∇f, xinit, η) that takes the gradient of a function f as
input, the starting xinit and the learning rate η. The output of gradient descent is the value of x that
minimizes ∇f. ∇f will be in the form of a function, so that you can calculate the gradient at a particular
point. That is ∇f is a function with one input x and it outputs the value of the gradient at that point. If
we are working with 1D variables, then x= (x1). If x is 2D then x= (x1, x2) and so on. x should be a 1D
numpy array. Write-Up: describe your implementation concisely.
If you test your function using f(x) = x
2
, xinit = 5, and η = 0.1. ∇f = 2x. You should get a final x
of 4.56719262e − 05 or something very small like that. It should take 52 steps. Note, I am not checking for
1
gradient equal to 0. I am checking that the magnitude of the gradient is less than some very small value,
0.0001.
For testing purposes: I will test your program with various ∇f.
You should get the following output for the gradient descent when you run the test script.
G r adien t Descent : [ 4. 5 6 7 1 9 2 6 2 e −05] 1
G r adien t Descent : [ 2. 1 6 4 3 3 1 8 6 e−55 5. 7 7 0 1 6 9 7 6 e −03] 2
2