背景:
尝试构建一个具有 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 对象。
对于每个隐藏节点,网络应将所有输入节点值(每行中)相加,并将其乘以隐藏节点的权重,然后将其输入 sigmoid 函数
最佳答案
此代码将循环遍历输入列表,然后遍历数组并打印输出。
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中使用相同的逻辑并进行处理。
关于java - 从 .data 文件读取数据并将数据值分区到节点数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890814/