背景:
尝试构建一个神经网络,该网络将包含13个输入节点,8个隐藏节点和1个输出节点
正在阅读的数据
我有这么远的代码
NeuralNetwork.java
public class NeuralNetwork {
//Setup an array of Node to store values from a .data file
Node[] dataSet;
//Declare a double[][] array, and randomize the weights of the double array in constructor
protected double[] weights;
//We set a double field named eta equal to 0.05.
protected double eta = 0.05;
private final String comma = ",";
private final String qMarks = "?";
private List<InputNode> input;
//We initialize a constructor which only takes a parameter int n.
public NeuralNetwork(File f){
List<InputNode> network = new ArrayList<InputNode>();
this.input = network;
try {
@SuppressWarnings("resource")
Scanner inFile = new Scanner(f);
//While there is another line in inFile.
while (inFile.hasNextLine()){
//Store that line into String line
String line = inFile.nextLine();
//Parition values separated by a comma
String[] columns = line.split(comma);
/*System.out.println(Arrays.toString(columns)); //Test code to see length of each column
* code works and prints out row
* */
//create a double array of size columns
InputNode[] rows = new InputNode[columns.length];
for (int i = 0; i < columns.length; i++){
//For each row...
if (!columns[i].equals(qMarks)){
//If the values in each row do not equal "?"
//Set rows[i] to the values in column[i]
rows[i] = new InputNode(Double.parseDouble(columns[i]));
}
else {
rows[i] = new InputNode(0);
}
}
input.add(rows);
}
System.out.println(input.size());
//System.out.println(input.size()); //Test code to see how many rows in .data file
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Create Hidden Layer Network
for (int i = 0; i < input.size(); i++){
}
}
Node.java
public class Node {
private double number;
public Node(double value){
this.number = value;
}
public double output(){
return number;
}
}
InputNode.java
public class InputNode extends Node {
//Declare a double variable to represent the holding value for InputNode
private double value;
public InputNode(double value) {
super(value);
// TODO Auto-generated constructor stub
}
//Create method to initialize input nodes
public void set(double tempValue){
this.value = tempValue;
}
public double get(Node s){
return s.output();
}
//Override method from Node class
//This method will grab the sum of all input node values.
public double output(){
return value;
}
}
HiddenLayer.java
public class HiddenLayer extends Node {
protected List<InputNode> nodes = new ArrayList<InputNode>();
public HiddenLayer(double value) {
super(value);
// TODO Auto-generated constructor stub
}
//Some activation functions which can be called upon.
class ActivationFunction {
//Sigmoid activation function
public double sigmoid(double x) {
return (1.0 / (1 + Math.pow(Math.E, -x)));
}
public double deriveSigmoid(double d){
return d * (1.0 - d);
}
// Hyperbolic Tan Activation Function
public double hyperTanFunction(double x) {
return (Math.pow(Math.E, x) - Math.pow(Math.E, -x)) / (Math.pow(Math.E, x) + Math.pow(Math.E, -x));
}
public double deriveHyperTanFunction(double d){
return (1.0 - Math.pow(hyperTanFunction(d), 2.0));
}
}
//Output method for the HiddenNode class which will sum up all the input nodes for
//a specific hidden node
public double output(){
/*Here we will be implementing the following function
* Sigma(x[i] * weights[i]) = net
* Pass net into the hyberbolic tangent function to get an output between -1 to 1
* We will pass net into the activation function in the train method of Neural Network
*/
//Setup a double sum variable to represent net
double net = 0;
//Setup for loop to loop over input nodes array for a hidden node
for (int i = 0; i < nodes.size(); i++){
net += nodes.get(i).output();
}
return net;
}
}
期望结果
我希望我的NeuralNetwork(File f)构造函数将每行数据的每个数字划分为单独的输入节点
例如:
对于第1行:[28,1,2,130,132,0,2,185,0,0,?,?,?,0]
您获得的输入节点为:
InputNode[1] = 28
InputNode[2] = 1
InputNode[3] = 2
.....
这仅适用于每一行,该方法应遍历每一行。此外,我一直在努力弄清楚如何设置HiddenLayer对象。
对于每个HiddenNode,网络应该总结所有输入节点值(在每一行中)并乘以隐藏节点的权重,然后将其输入sigmoid函数
答案 0 :(得分:1)
此代码将循环输入列表,然后循环数组并打印输出。
for(int i=0;i < input.size(); i++) {
System.out.println("For Row " + i + " of file:");
InputNode[] = input.get(i);
for (int j=0;j < InputNode.length; j++ ) {
System.out.println(" InputNode[ " + j + "] = " + InputNode[j].output());
}
}
您可以在HiddenLayer中使用相同的逻辑并进行处理。