Skip to content

Instantly share code, notes, and snippets.

@NikhilAshodariya
Created November 5, 2016 17:58
Show Gist options
  • Select an option

  • Save NikhilAshodariya/5a6c1381b55f3b6ed960d9160f2fae19 to your computer and use it in GitHub Desktop.

Select an option

Save NikhilAshodariya/5a6c1381b55f3b6ed960d9160f2fae19 to your computer and use it in GitHub Desktop.
This is the implementation of perceptron learning rule in Artificial Neural Network
package perceptron;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Perceptron
{
int number_of_neurons;
int number_of_input;
float learning_constant;
int activation_function = 2;
float weight[];
/*
x1 x2 x3
[1 1 0
[-2 -0.5 1]
*/
float input_vector[][];
float desired_output[];
public Perceptron(int number_of_neurons, int number_of_input, float learning_constant)
{
this.number_of_neurons = number_of_neurons;
this.number_of_input = number_of_input;
this.learning_constant = learning_constant;
weight = new float[this.number_of_neurons];
input_vector = new float[this.number_of_neurons][this.number_of_input];
desired_output = new float[this.number_of_input];
}
public void take_Input() throws IOException
{
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Data for all");
take_Weight_Input();
take_Data_Input();
take_Desired_Output();
}
private void take_Weight_Input() throws IOException
{
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < number_of_neurons; i++)
{
System.out.println("Enter weight of " + (i + 1) + "st neuron");
weight[i] = Float.parseFloat(kin.readLine());
}
}
private void take_Desired_Output() throws IOException
{
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < number_of_input; i++)
{
System.out.println("Enter desired weight of " + (i + 1) + "st input");
desired_output[i] = Float.parseFloat(kin.readLine());
}
}
private void take_Data_Input() throws IOException
{
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < number_of_input; i++)
{
System.out.println("Enter data for " + (i + 1) + " input");
for (int j = 0; j < number_of_neurons; j++)
{
System.out.println("Enter " + (i + 1) + " data");
input_vector[j][i] = Float.parseFloat(kin.readLine());
}
}
}
private float activation_Function(float net)
{
switch (activation_function)
{
case 2:
if (net > 0)
{
return 1;
}
else
{
return -1;
}
}
return -100000;
}
private int changeInWeight(float change_in_weight[])
{
int temp = 0;
for (int i = 0; i < change_in_weight.length; i++)
{
if (change_in_weight[i] != 0)
{
temp = 1;
break;
}
}
if (temp == 1)
{
return 1;
}
else
{
return 0;
}
}
public void train()
{
float net[] = new float[number_of_neurons];
float activation;
float desired_minus_activation;
float change_in_weight[] = new float[number_of_neurons];
for (; true;)
{
for (int i = 0; i < number_of_input; i++)
{
float temp = 0;
for (int j = 0; j < number_of_neurons; j++)
{
temp += weight[j] * input_vector[j][i];
}
System.out.println("net = " + temp);
net[i] = temp;
activation = activation_Function(net[i]);
System.out.println("activation function value = " + activation);
desired_minus_activation = desired_output[i] - activation;
temp = (float) (learning_constant * desired_minus_activation * 1.0);//reusing temp
System.out.println("temp = " + temp);
for (int j = 0; j < change_in_weight.length; j++)
{
change_in_weight[j] = temp * input_vector[j][i];
}
System.out.println("change in weight = " + Arrays.toString(change_in_weight));
for (int j = 0; j < weight.length; j++)
{
weight[j] = weight[j] + change_in_weight[j];
}
System.out.println("Changed weight = " + Arrays.toString(weight));
}
if (changeInWeight(change_in_weight) == 0)// 0 means weight are stabalize
{
break;
}
}
}
}
class PerceptronProgram
{
public static void main(String[] args) throws IOException
{
// i/p weights, learning rate, target vector, type of learning
int number_of_neurons;
int number_of_input;
float learning_constant;
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter number of neurons ");
number_of_neurons = Integer.parseInt(kin.readLine());
System.out.println("enter number of input ");
number_of_input = Integer.parseInt(kin.readLine());
System.out.println("enter value of learning constant ");
learning_constant = Float.parseFloat(kin.readLine());
Perceptron perc = new Perceptron(number_of_neurons, number_of_input, learning_constant);
perc.take_Input();
perc.train();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment