2

Perceptron Training

Unsolved
Supervised

Difficulty: 7 | Problem written by ankita
Neural networks are composed of small units which are sometimes called neurons or perceptrons. These small units are supervised classifiers of their own.  

Here, our aim is to implement the perceptron training manually using NumPy only.

Given below is the perceptron criteria that is to be minimized to learn the weights:

Ep(w) =  \(\sum_{n\in M}^{}-w^{t}\Phi(x_{n})t_{n}\) 

M: misclassified points.

\(w\)Weights of the model

\(\Phi\)Feature function of x in our case it is identity function \(\Phi\)(x) = x

\(t_{n}\): True labels of training data labeled as +1 and -1 for the two classes

Input:

x: an array of training examples
y: an array of outputs corresponding to each training example (possible values are 0 and 1)
lr: the learning rate for the algorithm
iter: number of iterations the algorithm will perform

Output:
A list of updated weights after every iteration. Do not include the first W that is an array of zeros

Algorithm: 

  • Convert the given y from 0,1 to \(t_{n}\) which is +1 and -1 for the two classes
  • Calculate,  \(w^{T}x\) , then write y_p as an array of +1 for positive value at the corresponding location in the calculated vector and -1 for negative value
  • It can be seen for correct classification \(w^{T}\Phi(x_{n})t_{n}\)  is positive and negative for incorrect classification
  • For incorrectly classified points calculate derivative of Ep(w) wrt to w using the formula given below: Derivative of  Ep(w) wrt to w =  \(\sum_{n\in M}^{}-\Phi(x_{n})t_{n}\), where M is misclassified points
  • Update the weights using the formula: W = W - learning_rate*derivative of the perceptron criterion with respect to W
  • Continue the above step for the number of iterations specified in the function.

Hints:

The first element of W should be wo i.e., if Y = wX + wo then W = [wo, elements of w]
To get the above W, stack a column of ones to X at the beginning of X.

Sample Input:
<class 'list'>
X: [[0.26703489, 0.50235526], [0.18151398, 0.07526641], [0.97665297, 0.09124986]]
Y: [0, 1, 1]
iter: 5
lr: 0.1

Expected Output:
<class 'list'>
[array([[0.8 ], [0.8841833 ], [0.98334837]]), array([[0.6 ], [0.76836661], [0.96669675]]), array([[0.4 ], [0.65254991], [0.95004512]]), array([[0.2 ], [0.53673322], [0.93339349]]), array([[5.55111512e-17], [4.20916525e-01], [9.16741865e-01]])]

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Harum veritatis ducimus libero officia, libero voluptates totam ullam eius dolores autem quia adipisci suscipit, et id dolorem consequatur exercitationem ab ratione obcaecati. Ab cum similique deleniti laborum cumque sunt voluptatum placeat corporis, iste excepturi explicabo laborum recusandae. Vero ratione itaque eos veritatis natus commodi labore recusandae facere eum, quia eveniet deserunt temporibus omnis, officia rem quod temporibus cum dolorem itaque nulla recusandae commodi aliquam nesciunt. Rem quaerat amet incidunt suscipit aliquam, blanditiis autem qui architecto?

Ipsam consequatur quam illum, quasi beatae dolor deserunt repellat quibusdam officia nesciunt eius cumque, architecto est reiciendis, libero odit voluptates aliquid corporis aut quis, ad dolor eaque modi blanditiis vel ipsum porro explicabo eos repudiandae. Doloribus nemo eaque sapiente ratione amet architecto consequuntur facilis similique a perspiciatis, dolore molestias voluptate temporibus.

Eius illo totam nesciunt dicta est optio? Ab blanditiis asperiores possimus distinctio cupiditate officia quaerat ipsam veritatis, iusto quas rerum quaerat aspernatur odio.

This is a premium feature.
To access this and other such features, click on upgrade below.

Ready.

Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)