Neural networks is an AI approach which simulates how the br
Neural networks is an AI approach which simulates how the brain works. Each neuron in the network has a fixed number of inputs. Each input is assigned a weight. When \"activated\" it sums up the result of multiplying each input by its weight. If this sum is above a threshold, it returns 1. If the sum is equal to or smaller than the threshold it returns -1.
ArtificialNeuron(double[] weightings)
Set the weights instance variable equal to weightings. Set inputs equal to a new array of type double with the same length as weightings. Finally set the value of the entry at index 0 in inputs equal to 1.
setInput(int index, double newValue)
Set the entry in inputs at index index equal to newValue.
activate(double threshold)
Write a loop that will can go over each index in inputs. For each entry in inputs, multiple its value by the value of the entry in weights at the same index. Use sum to add up all of these results. Once the loop completes, if sumthe is greater than threshold, return 1. Otherwise, return -1. Complete the three methods in the code:
package artneuron;
/**
* Class that defines the functions for a single neuron in an artificial neural network (e.g., an AI algorithm). Since
* the number of inputs can never change, it is faster (and more logical) for this class to use array than an ArrayList.
*/
public class ArtificialNeuron {
/** Input values for this neuron. Store these input values directly. */
private double[] inputs;
private double[] weights;
/**
* Creates a new artificial neuron. This will save the provided weights array and create a new inputs array so that
* the input array has the same length as the weights array. Following common practice, the initial value in the
* inputs array will be set to 1.
*
* @param weightings Array of weightings to be used by this neuron
*/
public ArtificialNeuron(double[] weightings) {
}
/**
* Assign the input at the index stored in {@code index} to have the value {@code newValue}
* @param index Index of the input whose value will be updated
* @param newValue New value for the input being updated.
*/
public void setInput(int index, double newValue) {
}
/**
* Calculates the \"output\" of this artificial neuron. This is done by calculating the weighted sum of the inputs. This
* is done by looping through each entry in the input array. At each index, multiply the values of the input and
* weighting array at that index and add the result into the sum. Once this sum is completed, compared that sum to
* {@code threshold}. If the sum is greater then return 1. If the sum is smaller or equal then return -1.
*
* @param threshold Threshold value over which this neuron should \"fire\".
* @return 1 if the neuron fires and -1 if it does not.
*/
public int activate(double threshold) {
double sum = 0.0;
}
}
Solution
/**
* Class that defines the functions for a single neuron in an artificial neural network (e.g., an AI algorithm). Since
* the number of inputs can never change, it is faster (and more logical) for this class to use array than an ArrayList.
*/
public class ArtificialNeuron {
/** Input values for this neuron. Store these input values directly. */
private double[] inputs;
private double[] weights;
/**
* Creates a new artificial neuron. This will save the provided weights array and create a new inputs array so that
* the input array has the same length as the weights array. Following common practice, the initial value in the
* inputs array will be set to 1.
*
* @param weightings Array of weightings to be used by this neuron
*/
public ArtificialNeuron(double[] weightings) {
this.weights = weightings;
this.inputs = new double[weightings.length];
for (int i = 0; i < this.inputs.length; i++)
this.inputs[i] = 1.0;
}
/**
* Assign the input at the index stored in {@code index} to have the value {@code newValue}
* @param index Index of the input whose value will be updated
* @param newValue New value for the input being updated.
*/
public void setInput(int index, double newValue) {
this.inputs[index] = newValue;
}
/**
* Calculates the \"output\" of this artificial neuron. This is done by calculating the weighted sum of the inputs. This
* is done by looping through each entry in the input array. At each index, multiply the values of the input and
* weighting array at that index and add the result into the sum. Once this sum is completed, compared that sum to
* {@code threshold}. If the sum is greater then return 1. If the sum is smaller or equal then return -1.
*
* @param threshold Threshold value over which this neuron should \"fire\".
* @return 1 if the neuron fires and -1 if it does not.
*/
public int activate(double threshold) {
double sum = 0.0;
for (int i = 0; i < this.inputs.length; i++) {
sum += this.inputs[i] * this.weights[i];
}
if (Double.compare(sum,1.0) > 1)
return 1;
else
return -1;
}
}


