C#Math.Net矩阵维度必须一致
我试图将python代码翻译成神经网络 在C#中。我使用Math.Net数值计算矩阵,这是我到目前为止用C编写的代码#C#Math.Net矩阵维度必须一致,c#,python,artificial-intelligence,C#,Python,Artificial Intelligence,我试图将python代码翻译成神经网络 在C#中。我使用Math.Net数值计算矩阵,这是我到目前为止用C编写的代码# 使用系统; 使用MathNet.Numerics.LinearAlgebra; 使用MathNet.Numerics; 使用MathNet.Numerics.LinearAlgebra.Double; 名称空间神经网络 { 班级计划 { 静态void Main(字符串[]参数) { NeuralNetwork NN=新的NeuralNetwork(); WriteLine(“随
使用系统;
使用MathNet.Numerics.LinearAlgebra;
使用MathNet.Numerics;
使用MathNet.Numerics.LinearAlgebra.Double;
名称空间神经网络
{
班级计划
{
静态void Main(字符串[]参数)
{
NeuralNetwork NN=新的NeuralNetwork();
WriteLine(“随机起始突触权重:”);
控制台写入线(NN.SynapticWeights);
矩阵TrainingSetInput=DenseMatrix.OfArray(新的双[,]{{0,0,1},{1,1,1},{1,0,1},{0,1,1});
矩阵TrainingSetOutput=DenseMatrix.OfArray(新的双[,]{{0,1,1,0}).Transpose();
NN.列车(列车设定输出,列车设定输出,10000);
WriteLine(“训练后新的突触重量:”);
控制台写入线(NN.SynapticWeights);
WriteLine(“考虑到新的情况{1,0,0}->?:”;
WriteLine(NN.Think(DenseMatrix.OfArray(新的double[,]{{1,0,0}}));
Console.ReadLine();
}
}
类神经网络
{
私有矩阵_;
公共神经网络()
{
_SynapticWeights=2*Matrix.Build.Random(3,1)-1;
}
专用矩阵Sigmoid(矩阵输入)
{
返回1/(1+矩阵Exp(-Input));
}
专用矩阵SigmoidePrivative(矩阵输入)
{
返回输入*(1-输入);//此处出现新异常
}
公共矩阵思考(矩阵输入)
{
返回Sigmoid((输入*_SynapticWeights));//此处异常(已解决)
}
公共无效序列(矩阵训练输入、矩阵训练输出、整数训练迭代)
{
对于(int Index=0;Index
当我执行它时,它在第53行显示一个异常(代码中该行有一条注释)。它说:
矩阵尺寸必须一致:op1为4x3,op2为3x1
是我把它复制错了还是MAth.Net库有问题
提前感谢:据我所知,问题不在库代码中。您正试图使用
op_dotplulary
函数执行元素矩阵乘法(第53行)。在这种情况下,从错误消息中可以明显看出,由于矩阵的大小不同,矩阵无法相乘:第一个矩阵是4x3,第二个矩阵是3x1
我可以建议查看这些矩阵的大小:TrainingSetInput
和\u SynapticWeights
(查看函数Train
,您正在调用并在其中使用错误大小的矩阵进行思考)。检查它们是否正确生成。如果你真的需要元素矩阵乘法或者普通的乘法。
如果需要有关矩阵乘法的更多信息,可以使用以下链接:
元素方面:
通常:
虽然Python代码真正写的是“点”,但它显然意味着简单的矩阵*向量乘法,而不是点积。因此,尝试一个(我找到的矩阵类定义了一个操作符*
),我尝试了*操作符,但仍然不起作用。另一件事可能是行和列的解释不同。虽然它会破坏后面的数学,但如果交换参数的顺序突然“修复”了点或*,您可以快速测试。如果它能工作,那么与原始Python代码所期望的相比,一切都会被改变。一个矩阵是3x1(SynapticWeights),另一个是4x3(TrainingSetInput)。显然,我不知道如何正确地将它们相乘。如果您只是想尝试返回Sigmoid(Input*\u SynapticsWeights)
现在(根据下面的讨论)我需要点模应用来让神经网络工作。矩阵以不同的大小正确生成,我想我能做的不多。您正在生成4x3TrainingSetInput
矩阵,将其传递到名为Train
的函数中,在for
循环中,您正在调用Think
函数,并再次将TrainingSetInput
矩阵传递到那里。在该函数中,4x3矩阵与3x1矩阵相乘\u SynapticWeights
。不幸的是,这在数学上是不可能的。对于点乘,你真的需要有大小相等的矩阵。很抱歉,如果我错过了一些明显的东西,我可能是错的。谢谢你的解释。你检查过python版本是否在相同的输入上工作了吗?
using System;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics;
using MathNet.Numerics.LinearAlgebra.Double;
namespace NeuralNetwork
{
class Program
{
static void Main(string[] args)
{
NeuralNetwork NN = new NeuralNetwork();
Console.WriteLine("Random starting synaptic weights: ");
Console.WriteLine(NN.SynapticWeights);
Matrix<double> TrainingSetInput = DenseMatrix.OfArray(new double[,] { { 0, 0, 1 }, { 1, 1, 1 }, { 1, 0, 1 }, { 0, 1, 1 } });
Matrix<double> TrainingSetOutput = DenseMatrix.OfArray(new double[,] { { 0, 1, 1, 0 } }).Transpose();
NN.Train(TrainingSetInput, TrainingSetOutput, 10000);
Console.WriteLine("New synaptic weights after training: ");
Console.WriteLine(NN.SynapticWeights);
Console.WriteLine("Considering new situation {1, 0, 0} -> ?: ");
Console.WriteLine(NN.Think(DenseMatrix.OfArray(new double[,] { { 1, 0, 0 } })));
Console.ReadLine();
}
}
class NeuralNetwork
{
private Matrix<double> _SynapticWeights;
public NeuralNetwork()
{
_SynapticWeights = 2 * Matrix<double>.Build.Random(3, 1) - 1;
}
private Matrix<double> Sigmoid(Matrix<double> Input)
{
return 1 / (1 + Matrix<double>.Exp(-Input));
}
private Matrix<double> SigmoidDerivative(Matrix<double> Input)
{
return Input * (1 - Input); //NEW Exception here
}
public Matrix<double> Think(Matrix<double> Input)
{
return Sigmoid((Input * _SynapticWeights)); //Exception here (Solved)
}
public void Train(Matrix<double> TrainingInput, Matrix<double> TrainingOutput, int TrainingIterations)
{
for (int Index = 0; Index < TrainingIterations; Index++)
{
Matrix<double> Output = Think(TrainingInput);
Matrix<double> Error = TrainingOutput - Output;
Matrix<double> Adjustment = Matrix<double>.op_DotMultiply(TrainingInput.Transpose(), Error * SigmoidDerivative(Output));
_SynapticWeights += Adjustment;
}
}
public Matrix<double> SynapticWeights { get { return _SynapticWeights; } set { _SynapticWeights = value; } }
}
}