Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save NikhilAshodariya/a20d4330dc91f513134ae14114983fc3 to your computer and use it in GitHub Desktop.
This is the implementation of Delta learning rule in Artificial Neural Network
package deltalearningrule;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class DeltaLearningRule {
int number_of_epoch = 1;
int number_of_neurons;
int number_of_input;
float learning_constant;
int activation_function;
float weight[];
/*
x1 x2 x3
[1 1 0
[-2 -0.5 1]
*/
float input_vector[][];
float desired_output[];
public DeltaLearningRule(int number_of_neurons, int number_of_input, int number_of_epoch, float learning_constant) {
this.number_of_neurons = number_of_neurons;
this.number_of_input = number_of_input;
this.learning_constant = learning_constant;
this.number_of_epoch = number_of_epoch;
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();
while (true) {
System.out.println("Select Activation Function");
System.out.println("1. unipolar continuous");
System.out.println("2. bipolar continuous");
activation_function = Integer.parseInt(kin.readLine());
if (activation_function >= 1 && activation_function <= 2) {
break;
} else {
System.out.println("Please choose correct activation function");
}
}
}
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 1:
float t = (float) (1.0 / (1 + Math.exp(-net)));
return t;
case 2:
float t1 = (float) ((2.0 / (1 + Math.exp(-net))) - 1);
return t1;
}
return -100000;
}
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 (int k = 0; k < number_of_epoch; k++) {
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 * calculate_FDashNet(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 for first cycle = " + Arrays.toString(weight));
}
}
}
private float calculate_FDashNet(float o) {
if (activation_function == 1)//i.e. unipolar continuous
{
return (float) (o * (1 - o) * 1.0);
} else {
//bipolar continuous
return (float) ((1 - Math.pow(o, 2)) * 0.5);
}
}
}
class DeltaLearningRuleProgram {
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;
int number_of_epoch;
BufferedReader kin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of epoch");
number_of_epoch = Integer.parseInt(kin.readLine());
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());
DeltaLearningRule perc = new DeltaLearningRule(number_of_neurons, number_of_input, number_of_epoch, 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