0

我正在努力证明基本的神经网络结果,但到目前为止还没有。我在 encog 中做一个前馈 xor 问题并导出最终的权重和计算的输出。

为了证明我只有一个 Excel 表,我在其中输入了权重,然后是 I1*W1+I2*W2 | I1*W3+I2*W4 到隐藏层,然后每个都激活 sigmoid,然后 H1*W5+H2*W6 然后再次 sigmoid 输出。

所以没有偏差,只是一个基本的 2x2x1,但是我插入权重后得到的输出值与我使用 encog 收到的预期输出值相差甚远。

我有 8 个来自 encog 的输出集可供测试,但到目前为止,我还没有得出相同的结论。任何帮助,将不胜感激。

如果有任何帮助,下面是一个示例输出。谢谢,以色列

输出权重

61.11812639080170, -70.09419692460420, 2.58264325902522, 2.59015713019213, 1.16050691499417, 1.16295830927117

输出值

0.01111771776254, 0.96929877340644, 0.96926035361899, 0.04443376315742

在 excel 中,这是我用于 sigmoid 函数的内容:=1/(1+EXP(-1*(C3))),不知道更多是否有帮助,因为它只是 sigmoid 之外的加法和乘法。

这是 Form1.cs:

using Encog.Engine.Network.Activation;
using Encog.ML.Data.Basic;
using Encog.Neural.Networks;
using Encog.Neural.Networks.Layers;
using Encog.Neural.Networks.Training.Propagation.Resilient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Encog_Visual
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            double[][] XOR_Input = 
            {
                new[] {0.0,0.0},
                new[] {1.0,0.0},
                new[] {0.0,1.0},
                new[] {1.0,1.0}
            };

            double[][] XOR_Ideal =
            {
                new[] {0.0},
                new[] {1.0},
                new[] {1.0},
                new[] {0.0}
            };

            var trainingSet = new BasicMLDataSet(XOR_Input, XOR_Ideal);

            BasicNetwork network = CreateNetwork();

            var train = new ResilientPropagation(network, trainingSet);

            int epoch = 0;
            do
            {          
                train.Iteration();
                epoch++;
                string result0 = String.Format("Iteration No :{0}, Error: {1}", epoch, train.Error);
                textBox1.AppendText(result0 + Environment.NewLine);
            } while (train.Error > 0.001);


            foreach (var item in trainingSet)
            {
                var output = network.Compute(item.Input);
                string result1 = String.Format("Input : {0}, {1} Ideal : {2} Actual : {3}", item.Input[0], item.Input[1], item.Ideal[0], output[0]);
                textBox1.AppendText(result1 + Environment.NewLine + network.DumpWeights() + Environment.NewLine);
            }        


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private static BasicNetwork CreateNetwork()
        {
            var network = new BasicNetwork();
            network.AddLayer(new BasicLayer(null, false, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 2));
            network.AddLayer(new BasicLayer(new ActivationSigmoid(), false, 1));
            network.Structure.FinalizeStructure();
            network.Reset();
            return network;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }


    }


}
4

2 回答 2

2

我确实偶然发现了这一点,然后到了这里,我认为还有一个答案需要详细说明:

是的,确实,要确定每个权重属于哪个层需要时间,因为在Encog 框架中似乎使用不同的标准对层进行编号。

例如,如果您编写类似以下的代码,那么networkName您想调用的任何内容都在哪里(例如:“XOR_one”)。然后,您可以main public Form1()在网络训练循环之后通过添加一行来调用此函数:saveNetwork("XOR_one");然后...

public DirectoryInfo dataDirRoot;
public FileInfo dataFileRoot;
public StreamWriter fileWriteSW;

public bool saveNetwork(string networkName)
{
    try
    {
        // File data initialized
        if (dataDirRoot == null) dataDirRoot = new DirectoryInfo(Application.StartupPath + "\\Data");
        if (!dataDirRoot.Exists) dataDirRoot.Create();

        dataFileRoot = new FileInfo(dataDirRoot + "\\" + networkName + ".weights.txt");
        fileWriteSW = new StreamWriter(dataFileRoot.FullName, false, Encoding.Default);

        // (A) Write down weights from left to right layers, meaning input first and output last.
        // ...within each layer, weights are ordered up-down, always, in all three methods.
        for (int j = 0; j < network.LayerCount-1; j++)
        {
            for (int l = 0; l < network.GetLayerNeuronCount(j + 1); l++)
            {
                for (int k = 0; k < network.GetLayerTotalNeuronCount(j); k++)
                {
                    fileWriteSW.Write(network.GetWeight(j, k, l).ToString("r") + ", ");
                }
                fileWriteSW.Write("\r\n"); 
            }
        }
        fileWriteSW.Write("\r\n\r\n");

        // (B) Write down weights from left to right layers, output first, input last
        double[] auxDouble = new double[network.EncodedArrayLength()];  
        network.EncodeToArray(auxDouble);

        for (int j = 0; j < network.EncodedArrayLength(); j++)
        {
            fileWriteSW.Write(auxDouble[j] + "\r\n");
        }
        fileWriteSW.Flush();
        fileWriteSW.Close();

        // (C) Write down network structure
        // ...you will find that "weights" in the same order as with "DumpWeights()"
        dataFileRoot = new FileInfo(dataDirRoot + networkName + ".encog.txt");
        Encog.Persist.EncogDirectoryPersistence.SaveObject(dataFileRoot, network);
    }
    catch (Exception e)
    {
        MessageBox.Show("Error: " + e.Message);
        return false;
    }
    return true;
}

重要提示:在不偏向隐藏层的情况下训练 XOR 网络确实很困难,因此我展示的结果比您的示例多两个权重。这可以通过更改代码中的一行来实现:

...network.AddLayer(new BasicLayer(null, false, 2));

network.AddLayer(new BasicLayer(null, true, 2));

...为了给隐藏层一个权重输入。隐藏层中的神经元将具有三个权重。一个来自神经元输入 1,另一个来自神经元输入 2,第三个来自偏置神经元(在输入层中被列为“第三个神经元”,其值固定为 1.0)。

所以:这里的棘手之处在于将哪一层命名为“Layer 0”。

在情况(A)中,第 0 层是输入层,左起第一个,权重从第一个隐藏层(因为输入没有权重)、神经元 0 到 1 转储,然后是输出层、神经元 0。

但在 (B) 和 (C) 以及 "DumpWeights()" 的情况下,第 0 层是从右数第一个,表示输出层,权重从右到左层转储,并在每一层内从上到下转储。

总是,在每一层中,权重按顺序转储,神经元 0 到 n,并且在每个神经元中,权重来自从左层到最后一个神经元的上层神经元或偏置(如果它存在于左层)。

输出权重结果是这样的:

Case (A)
-3.61545321823196, -2.7522256580709645, 3.509680820551957, 
-7.2744584719809806, -6.05682131778526, 7.6850785784618676, 
-35.025902985103983, 31.763309640942925, 

Case (B) 
-35.025902985104
31.7633096409429
-3.61545321823196
-2.75222565807096
3.50968082055196
-7.27445847198098
-6.05682131778526
7.68507857846187

那就来看看吧:

**Output Layer** (being it called 0 or N... you decide, I prefer N)
**Neuron 0** (the only one there)
weight 2,0,0 = -35.025902985104 (where 2 is layer, 0 is first neuron in hidden layer and 0 is output neuron)
weight 2,1,0 = 31.7633096409429

**Hidden Layer** (I choose 1)
**Neuron 0** (first one)
weight 1,0,0 = -3.61545321823196 (where 1 is layer, 0 is first neuron in input layer and 0 is this neuron)
weight 1,1,0 = -2.75222565807096
weight 1,2,0 = 3.50968082055196
**Neuron 1** (last one)
weight 1,0,1 = -7.27445847198098 
weight 1,1,1 = -6.05682131778526
weight 1,2,1 = 7.68507857846187 (where 1 is layer, 2 is bias in input layer and 1 is this neuron)

请注意:您在问题中的示例是DumpWeights(): 61.11812639080170, -70.09419692460420, 2.58264325902522, 2.59015713019213, 1.16050691499417, 1.16295830927117

它对应于案例(B),仅以逗号分隔。前两个数字属于输出神经元,后者属于,第三和第四属于第一个神经元,隐藏层,第五和第六个属于第二个神经元,隐藏层。

我在这里包含使用您的数据的 Excel 示例的 CSV:

,,=+A2,2.58264325902522,,,,,,,
0,,=+A4,2.59015713019213,=C1*D1+C2*D2,=1/(1+EXP(-1*(E2))),,,,,
,,=+A2,1.16050691499417,,,,=+F2,61.1181263908017,,
0,,=+A4,1.16295830927117,=C3*D3+C4*D4,=1/(1+EXP(-1*(E4))),,=+F4,-70.0941969246042,=H3*I3+H4*I4,=1/(1+EXP(-1*(J4)))
,,,,,,,,,,
,,=+A7,2.58264325902522,,,,,,,
1,,=+A9,2.59015713019213,=C6*D6+C7*D7,=1/(1+EXP(-1*(E7))),,,,,
,,=+A7,1.16050691499417,,,,=+F7,61.1181263908017,,
0,,=+A9,1.16295830927117,=C8*D8+C9*D9,=1/(1+EXP(-1*(E9))),,=+F9,-70.0941969246042,=H8*I8+H9*I9,=1/(1+EXP(-1*(J9)))
,,,,,,,,,,
,,=+A12,2.58264325902522,,,,,,,
0,,=+A14,2.59015713019213,=C11*D11+C12*D12,=1/(1+EXP(-1*(E12))),,,,,
,,=+A12,1.16050691499417,,,,=+F12,61.1181263908017,,
1,,=+A14,1.16295830927117,=C13*D13+C14*D14,=1/(1+EXP(-1*(E14))),,=+F14,-70.0941969246042,=H13*I13+H14*I14,=1/(1+EXP(-1*(J14)))
,,,,,,,,,,
,,=+A17,2.58264325902522,,,,,,,
1,,=+A19,2.59015713019213,=C16*D16+C17*D17,=1/(1+EXP(-1*(E17))),,,,,
,,=+A17,1.16050691499417,,,,=+F17,61.1181263908017,,
1,,=+A19,1.16295830927117,=C18*D18+C19*D19,=1/(1+EXP(-1*(E19))),,=+F19,-70.0941969246042,=H18*I18+H19*I19,=1/(1+EXP(-1*(J19)))
,,,,,,,,,,
DumpWeights() = ,,,,,,,,,,
"61.11812639080170, -70.09419692460420, 2.58264325902522, 2.59015713019213, 1.16050691499417, 1.16295830927117",,,,,,,,,,

应该这样做:)

(记录一下,我用的是Encog v3.2.0)

于 2014-05-25T15:27:30.923 回答
0

以防将来有人偶然发现这一点。

encog输出的权重是layerN Neuron:0,1,n,bias,下到layer0 neuron:0,1,n,bias

找出正确的输出以反馈给函数,我能够根据给定的输出正确地证明它。

于 2013-11-19T21:12:31.620 回答