如何在 encog XOR 函数中提供字符串作为输入和输出?
How to provide strings as inputs and outputs in encog XOR function?
我需要在 Java 中使用 XOR 函数制作 encog 程序,该函数必须将带有定义的字符串单词作为输入,但 BasicMLDataSet 只能接收双精度数。这是我使用的示例代码:
/**
* The input necessary for XOR.
*/
public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 },
{ 0.0, 1.0 }, { 1.0, 1.0 } };
/**
* The ideal data necessary for XOR.
*/
public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };
这是接收 XOR_INPUT 和 XOR_IDEAL 的 class:
MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
有什么方法可以完成字符串训练或以某种方式解析它们,然后 return 在将它们写入控制台之前将它们 return 成字符串?
我已找到解决此问题的方法。因为我只能提供 0 到 1 之间的双精度值作为输入,而且我在 encog 中没有找到任何可以自然地将字符串规范化为双精度值的函数,所以我创建了自己的函数。我从单词中的每个字母中获取 ascii 值,然后简单地将 90/asciiValue 除以得到 0 和 1 之间的值。请记住,这仅适用于小写字母。功能可以轻松升级以支持大写字母。这是函数:
//Converts every letter in string to ascii and normalizes it (90/asciiValue)
public static double[] toAscii(String s, int najveci) {
double[] ascii = new double[najveci];
try {
byte[] bytes = s.getBytes("US-ASCII");
for (int i = 0; i < bytes.length; i++) {
ascii[i] = 90.0 / bytes[i];
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ascii;
}
对于单词理想输出,我使用了类似的解决方案。我也在规范化单词中的每个字母,然后我对这些值取平均值。稍后,我将这些值反规范化以取回字符串并检查模型训练效果。
您可以查看full code here.
您可以使用 Encog 的 EncogAnalyst 和 AnalystWizard 来规范化您的数据。 @JeffHeaton(Encog 的作者)的 posting 展示了一个使用 .csv 文件的示例
这些 类 可以规范化数字和 "nominal" 数据(例如,您要使用的字符串。)您可能希望对这些字符串使用 "Equilateral" 规范化,因为这将避免神经网络的一些训练问题。
您可能还想查看这个 tutorial on Encog on PluralSight,其中有一整节是关于规范化的。
以下是 Encog 文档中的示例,展示了如何使用代码(没有 .csv 文件)规范化字段:
var fuelStats = new NormalizedField( NormalizationAction.Normalize , ”fuel”, 200, 0, −0.9, 0.9) ;
28 Obtaining Data for Encog
For the above example the range is normalized to -0.9 to 0.9. This is very similar to normalizing between -1 and 1, but less extreme. This can produce better results at times. It is also known that the acceptable range for fuel is between 0 and 200. Now that the field object has been created, it is easy to normalize the values. Here the value 100 is normalized into the variable n. double n = fuelStats .Normalize(100); To denormalize n back to the original fuel value, use the following code:
double f = fuelStats .Denormalize(n);
我需要在 Java 中使用 XOR 函数制作 encog 程序,该函数必须将带有定义的字符串单词作为输入,但 BasicMLDataSet 只能接收双精度数。这是我使用的示例代码:
/**
* The input necessary for XOR.
*/
public static double XOR_INPUT[][] = { { 0.0, 0.0 }, { 1.0, 0.0 },
{ 0.0, 1.0 }, { 1.0, 1.0 } };
/**
* The ideal data necessary for XOR.
*/
public static double XOR_IDEAL[][] = { { 0.0 }, { 1.0 }, { 1.0 }, { 0.0 } };
这是接收 XOR_INPUT 和 XOR_IDEAL 的 class:
MLDataSet trainingSet = new BasicMLDataSet(XOR_INPUT, XOR_IDEAL);
有什么方法可以完成字符串训练或以某种方式解析它们,然后 return 在将它们写入控制台之前将它们 return 成字符串?
我已找到解决此问题的方法。因为我只能提供 0 到 1 之间的双精度值作为输入,而且我在 encog 中没有找到任何可以自然地将字符串规范化为双精度值的函数,所以我创建了自己的函数。我从单词中的每个字母中获取 ascii 值,然后简单地将 90/asciiValue 除以得到 0 和 1 之间的值。请记住,这仅适用于小写字母。功能可以轻松升级以支持大写字母。这是函数:
//Converts every letter in string to ascii and normalizes it (90/asciiValue)
public static double[] toAscii(String s, int najveci) {
double[] ascii = new double[najveci];
try {
byte[] bytes = s.getBytes("US-ASCII");
for (int i = 0; i < bytes.length; i++) {
ascii[i] = 90.0 / bytes[i];
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return ascii;
}
对于单词理想输出,我使用了类似的解决方案。我也在规范化单词中的每个字母,然后我对这些值取平均值。稍后,我将这些值反规范化以取回字符串并检查模型训练效果。
您可以查看full code here.
您可以使用 Encog 的 EncogAnalyst 和 AnalystWizard 来规范化您的数据。 @JeffHeaton(Encog 的作者)的 posting 展示了一个使用 .csv 文件的示例
这些 类 可以规范化数字和 "nominal" 数据(例如,您要使用的字符串。)您可能希望对这些字符串使用 "Equilateral" 规范化,因为这将避免神经网络的一些训练问题。
您可能还想查看这个 tutorial on Encog on PluralSight,其中有一整节是关于规范化的。
以下是 Encog 文档中的示例,展示了如何使用代码(没有 .csv 文件)规范化字段:
var fuelStats = new NormalizedField( NormalizationAction.Normalize , ”fuel”, 200, 0, −0.9, 0.9) ;
28 Obtaining Data for Encog For the above example the range is normalized to -0.9 to 0.9. This is very similar to normalizing between -1 and 1, but less extreme. This can produce better results at times. It is also known that the acceptable range for fuel is between 0 and 200. Now that the field object has been created, it is easy to normalize the values. Here the value 100 is normalized into the variable n. double n = fuelStats .Normalize(100); To denormalize n back to the original fuel value, use the following code:
double f = fuelStats .Denormalize(n);