[英]Input string was not in a correct format String to double [] [] array
I know that there are more threads on this topic here, but none of them actually helped me. 我知道这里有更多关于此主题的主题,但实际上没有一个对我有帮助。
I will provide whole code: 我将提供完整的代码:
namespace ConsoleApplication1
{
public static class Load
{
public static double[][] FromFile(string path)
{
var rows = new List<double[]>();
foreach (var line in File.ReadAllLines(path))
{
// HERE IS THE ERROR
rows.Add(line.Split(new[] { ' ' }).Select(double.Parse).ToArray());
}
return rows.ToArray();
}
}
public class Program
{
static void Main( string [ ] args )
{
string cestain = @"E:\vstup.txt";
double[][] innput = Load.FromFile(cestain);
string cestaout = @"E:\vystup.txt";
double[][] ooutput = Load.FromFile(cestaout);
// c r e a t e a neural network
var network = new BasicNetwork ( ) ;
network . AddLayer (new BasicLayer ( null , true , 2) ) ;
network . AddLayer (new BasicLayer (new ActivationSigmoid ( ) , true , 3) ) ;
network . AddLayer (new BasicLayer (new ActivationSigmoid ( ) , false , 1) ) ;
network . Structure . FinalizeStructure ( ) ;
network . Reset( );
// c r e a t e t r a i n i n g data
IMLDataSet trainingSet = new BasicMLDataSet (innput , ooutput ) ;
// t r a i n the neural network
IMLTrain train = new ResilientPropagation (network , trainingSet) ;
int epoch = 1 ;
do
{
train.Iteration( ) ;
Console.WriteLine(@"Epoch #" + epoch + @" Error : " + train.Error );
epoch++;
} while ( train.Error> 0.01 ) ;
Console.ReadLine();
}
}
}
Here is what I am trying to load to double[][]
input: 这是我要加载为
double[][]
输入的内容:
166 163 180 228
166163180228
165 162 160 226
165162160226
166 163 180 228
166163180228
166 164 180 228
166164180228
171 162 111 225
171162111225
Here is what I am trying to load to double[][]
output: 这是我要加载为
double[][]
输出的内容:
1 0 0
1 0 0
0 1 0
0 1 0
1 0 0
Problem : You have the Extra EmptyLines after every Line in your text file. 问题:文本文件中的每一行之后都有多余的空行。 when Split() function encounters a new Line it returns as it is becaue there is nothing to split and
Add()
function throws exception as empty line is not a valid Double
. 当Split()函数遇到新的Line时,它会返回,因为没有拆分内容,并且
Add()
函数抛出异常,因为空行不是有效的Double
。
Solution1 : You can Use StringSplitOptions.RemoveEmptyEntries
as second argument to Split()
function to ignore the EmptyLines. 解决方案1:可以使用
StringSplitOptions.RemoveEmptyEntries
作为Split()
函数的第二个参数来忽略EmptyLines。
foreach (var line in File.ReadAllLines(path))
{
// HERE IS THE ERROR
rows.Add(line.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());
}
Solution 2: you can check wether line is Empty or not by using String.IsNullOrWhiteSpace()
解决方案2:您可以使用
String.IsNullOrWhiteSpace()
检查行是否为空
foreach (var line in File.ReadAllLines(path))
{
if(!String.IsNullOrWhiteSpace(line))
{
// HERE IS THE ERROR
rows.Add(line.Split(new[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Select(double.Parse).ToArray());
}
}
I had a similar problem not long ago. 我不久前也遇到过类似的问题。 Using
StringSplitOptions.RemoveEmptyEntries
solved it for me. 使用
StringSplitOptions.RemoveEmptyEntries
为我解决了它。 So it becomes, lines.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
这样就变成了
lines.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
It helps because you are getting empty lines, so RemoveEmptyEntries
removes those lines from the result of the Split method. 它会有所帮助,因为您将获得空行,因此
RemoveEmptyEntries
将从Split方法的结果中删除这些行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.