自定义 class 以保存感知器数据 - 可能未正确填充
custom class to hold perceptron data- possibly not correctly populated
我有以下输入
0 0 0 0 0 0 2
0 0 0 0 1 0 2
0 0 0 1 0 0 2
0 0 0 1 1 0 2
这些是用于演示的玩具值
这些是为了给感知器提供两个输出,即选项卡后面的两个标签。
我创建了一个 class 来保存一组输入和一个标签,所以我最终设想了两个数据结构,每个神经元一个,形式如下:
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 1 0 0
0 0 0 1 1 0
和
0 0 0 0 0 2
0 0 0 0 1 2
0 0 0 1 0 2
0 0 0 1 1 2
class 看起来像这样:
class Group {
public String key;
public String[] value;
public String getKey() {
return key;
}
public String[] getValue() {
return value;
}
Group(String[] splited_inputs, String k)
{
this.key = k;
this.value = splited_inputs;
}
@Override
public String toString() {
return this.key + " " + this.value;
}
}
我通过以这种方式从文件中读取输入来填充 class:
ArrayList<Group> list = new ArrayList<>();
/**************
* READ INPUT *
**************/
BufferedReader reader = new BufferedReader(new FileReader("../PA-A-train.dat"));
String line;//new variable
while ((line = reader.readLine()) != null) //read the line and compare
{
/*********************************************************************
* GET THE LINE, SPLIT ON THE TAB FOR LABEL VS. INPUT IDENTIFICATION *
*********************************************************************/
String[] label_detector = line.split("\t"); //split
/*****************************
* GET THE INDIVIDUAL INPUTS *
*****************************/
String inputs = label_detector[label_detector.length - 2];
String[] splited_inputs = inputs.split("\s+");
splited_inputs = Arrays.stream(splited_inputs) //Remove null values
.filter(s -> (s != null && s.length() > 0))
.toArray(String[]::new);
//for this training datum, how many features does it have
int number_of_inputs = splited_inputs.length; //5
/************************************
* GET THE LABEL (2nd LAYER OUTPUT) *
************************************/
String trueLabel = label_detector[label_detector.length - 1];
//System.out.println("this is the corresponding label: " + trueLabel);
String[] splited_labels = trueLabel.split("\s+");
int number_of_labels = splited_labels.length;
list.add(new Group(splited_inputs, splited_labels[0]));
}
reader.close();
for (Group p : list)
System.out.println( "check it out: " + p.toString() );
}
但是 当我尝试打印它时,输出如下所示:
check it out: 0 [Ljava.lang.String;@87aac27
check it out: 0 [Ljava.lang.String;@3e3abc88
check it out: 0 [Ljava.lang.String;@6ce253f1
check it out: 0 [Ljava.lang.String;@53d8d10a
check it out: 0 [Ljava.lang.String;@e9e54c2
check it out: 0 [Ljava.lang.String;@65ab7765
check it out: 0 [Ljava.lang.String;@1b28cdfa
check it out: 1 [Ljava.lang.String;@eed1f14
check it out: 0 [Ljava.lang.String;@7229724f
check it out: 0 [Ljava.lang.String;@4c873330
所以它打印的是内存中的位置,而不是输入数组。
这是因为我最初只是将该位置插入内存,还是因为我没有遍历它以正确打印它?
问题出在您的 toString() 方法中:
public String toString() {
return this.key + " " + this.value;
}
this.value
是一个字符串数组 (String[]
),因此它的 toString()
方法 returns 看起来像 [Ljava.lang.String;@106d69c
。相反,您可以遍历 this.value
并将每个连接到您的结果:
public String toString() {
String result = this.key;
for (int i = 0; i < this.value.length; i++) {
result += " " + this.value[i];
}
return result;
}
更好的是,您可以使用 String.join(delimiter, elements)
:
public String toString() {
return this.key + " " + String.join(" ", this.value);
}
输出不会像 Arrays.toString()
那样有方括号。
您不能在 java 中的数组上直接调用 toString()
来打印内容。
但是,您可以使用 Arrays.toString(Object[] arr)
@Override
public String toString() {
return this.key + " " + Arrays.toString(this.value);
}
我有以下输入
0 0 0 0 0 0 2
0 0 0 0 1 0 2
0 0 0 1 0 0 2
0 0 0 1 1 0 2
这些是用于演示的玩具值
这些是为了给感知器提供两个输出,即选项卡后面的两个标签。
我创建了一个 class 来保存一组输入和一个标签,所以我最终设想了两个数据结构,每个神经元一个,形式如下:
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 1 0 0
0 0 0 1 1 0
和
0 0 0 0 0 2
0 0 0 0 1 2
0 0 0 1 0 2
0 0 0 1 1 2
class 看起来像这样:
class Group {
public String key;
public String[] value;
public String getKey() {
return key;
}
public String[] getValue() {
return value;
}
Group(String[] splited_inputs, String k)
{
this.key = k;
this.value = splited_inputs;
}
@Override
public String toString() {
return this.key + " " + this.value;
}
}
我通过以这种方式从文件中读取输入来填充 class:
ArrayList<Group> list = new ArrayList<>();
/**************
* READ INPUT *
**************/
BufferedReader reader = new BufferedReader(new FileReader("../PA-A-train.dat"));
String line;//new variable
while ((line = reader.readLine()) != null) //read the line and compare
{
/*********************************************************************
* GET THE LINE, SPLIT ON THE TAB FOR LABEL VS. INPUT IDENTIFICATION *
*********************************************************************/
String[] label_detector = line.split("\t"); //split
/*****************************
* GET THE INDIVIDUAL INPUTS *
*****************************/
String inputs = label_detector[label_detector.length - 2];
String[] splited_inputs = inputs.split("\s+");
splited_inputs = Arrays.stream(splited_inputs) //Remove null values
.filter(s -> (s != null && s.length() > 0))
.toArray(String[]::new);
//for this training datum, how many features does it have
int number_of_inputs = splited_inputs.length; //5
/************************************
* GET THE LABEL (2nd LAYER OUTPUT) *
************************************/
String trueLabel = label_detector[label_detector.length - 1];
//System.out.println("this is the corresponding label: " + trueLabel);
String[] splited_labels = trueLabel.split("\s+");
int number_of_labels = splited_labels.length;
list.add(new Group(splited_inputs, splited_labels[0]));
}
reader.close();
for (Group p : list)
System.out.println( "check it out: " + p.toString() );
}
但是 当我尝试打印它时,输出如下所示:
check it out: 0 [Ljava.lang.String;@87aac27
check it out: 0 [Ljava.lang.String;@3e3abc88
check it out: 0 [Ljava.lang.String;@6ce253f1
check it out: 0 [Ljava.lang.String;@53d8d10a
check it out: 0 [Ljava.lang.String;@e9e54c2
check it out: 0 [Ljava.lang.String;@65ab7765
check it out: 0 [Ljava.lang.String;@1b28cdfa
check it out: 1 [Ljava.lang.String;@eed1f14
check it out: 0 [Ljava.lang.String;@7229724f
check it out: 0 [Ljava.lang.String;@4c873330
所以它打印的是内存中的位置,而不是输入数组。
这是因为我最初只是将该位置插入内存,还是因为我没有遍历它以正确打印它?
问题出在您的 toString() 方法中:
public String toString() {
return this.key + " " + this.value;
}
this.value
是一个字符串数组 (String[]
),因此它的 toString()
方法 returns 看起来像 [Ljava.lang.String;@106d69c
。相反,您可以遍历 this.value
并将每个连接到您的结果:
public String toString() {
String result = this.key;
for (int i = 0; i < this.value.length; i++) {
result += " " + this.value[i];
}
return result;
}
更好的是,您可以使用 String.join(delimiter, elements)
:
public String toString() {
return this.key + " " + String.join(" ", this.value);
}
输出不会像 Arrays.toString()
那样有方括号。
您不能在 java 中的数组上直接调用 toString()
来打印内容。
但是,您可以使用 Arrays.toString(Object[] arr)
@Override
public String toString() {
return this.key + " " + Arrays.toString(this.value);
}