感知器故障(Malfunctioning perceptron)

我是机器学习的新手,在进入多层网络之前一直在尝试基本的感知器。

我遇到的问题是下面的代码。 我有一个训练数据生成器,它使用一组权重来生成真值表。

我遇到的问题是感知器能够在使用设置'A'而不是设置'B'生成训练数据时解决/确定权重集。 当给定使用集合'B'生成的训练数据时,它会在无限循环中继续尝试确定权重(这是本地最小问题吗?)

我不明白为什么会发生这种情况。 任何帮助或建议表示赞赏。

提前致谢。

// Calling function 
public static void TestPerceptron ()
    {
        // Problem: 
        // When training data is generated using the 'A' set of weights, the perceptron is able to determine the correct weights based on the given training data.
        // When training data is generated using the 'B' set of weights, the perceptron never completes training and is stuck in an infinite loop

        double[] weights = new double[] {
            //3,2,2,3 // A
            3,2,1,3,1 // B
        };

        double bias = 0.0; 

        var trainingData = PerceptronHelper.GenerateDataSetUsingWeights (weights, bias);
        var perceptron = new Perceptron ();
        perceptron.Train (trainingData, null, null);

        //perceptron.Train (trainingData, weights, bias); 
    }


public class Perceptron
{
    private static Random r = new Random ();
    protected double _bias = r.NextDouble();
    protected double[] _weights;

    protected virtual double ComputeOutput(double[] weights, double[] inputs, double bias)
    {
        var total = 0.0;

        for (var index = 0; index < inputs.Length-1; index++) 
        {
            total += weights [index] * inputs [index];
        }

        return total + (1 * bias);
    }

    protected virtual void SetWeights(ref double[] weights, double[] inputs, double error, double learningRate, ref double bias)
    {
        for (var index = 0; index < inputs.Length-1; index++) 
        {
            weights[index] = weights [index] + (learningRate * error * inputs [index]);
        }

        bias += learningRate * error * 1;
    }

    public virtual void Train(double[][] trainingData, double[] idealWeights, double? idealBias)
    {
        var learningRate = 1.0;
        var totalError = 1.0;
        var targetError = 0.0;
        var epochs = 0.0;
        var bias = _bias;
        var weights = new double[trainingData[0].Length-1];

        if (idealBias.HasValue)
            bias = idealBias.Value;

        if (idealWeights != null)
            weights = idealWeights;

        while (totalError > targetError) 
        {
            totalError = 0.0;

            for (var index = 0; index < trainingData.Length; index++) 
            {
                var inputs = trainingData [index];

                // get target
                var target = inputs [inputs.Length - 1];

                // compute output
                var computed = ComputeOutput (weights, inputs, bias);

                // pass computed through activation
                var output = PerceptronHelper.Activation (computed);

                // determine error 
                var error = (target - output);

                // adjust weights
                SetWeights (ref weights, inputs, error, learningRate, ref bias);

                totalError += Math.Abs(error);

                var weightsMsg = "Weights: ";

                foreach(var weight in weights)
                {
                    weightsMsg += weight + "|";
                }

                Console.WriteLine (String.Format ("error: {0} weights: {1} bias: {2}", totalError, weightsMsg, bias));
            }

            epochs++;
        }

        _bias = bias;
        _weights = weights;
    }

    public void Predict(double[] inputs)
    {
        var sum = 0.0;

        for (var index = 0; index < inputs.Length; index++) 
        {
            sum += inputs [index] * _weights [index] + 1 * _bias;

            Console.WriteLine (String.Format("input: {0} weight: {1} bias: {2}", inputs[index], _weights[index], _bias));
        }

        var output = PerceptronHelper.Activation (sum);
        Console.WriteLine ("Output:{0}", output);
    }
}


public static class PerceptronHelper
{
    // generate training data based on given weights - the number of inputs = number of weights 
    public static double[][] GenerateDataSetUsingWeights(double[] idealWeights, double bias)
    {
        var weights = idealWeights;
        var inputs = new double[weights.Length];
        var numInputCombinations = Math.Pow(2,inputs.Length); 
        var trainData = new double[(int)numInputCombinations][];
        int inputValue = 0;

        // generate training data
        for (var index = 0; index < numInputCombinations; index++) 
        {
            var sum = 0.0;  

            // last item in array is expected output
            var trainDataLine = new double[weights.Length+1];

            var binary = Convert.ToString (inputValue, 2);
            binary = binary.PadLeft (weights.Length, '0');

            // create training data line
            for (var wIndex = 0; wIndex < weights.Length; wIndex++) 
            {
                inputs [wIndex] = double.Parse(binary[wIndex].ToString());
                trainDataLine [wIndex] = inputs [wIndex];
                sum += inputs [wIndex] * weights [wIndex];
            }

            sum += (1 * bias);

            var output = Activation (sum);

            // store the expected result in the last item of the array
            trainDataLine [weights.Length] = output;

            // add the line to the data
            trainData[index] = trainDataLine;

            inputValue++;
        }

        return trainData;
    }

    public static double Activation (double sum) 
    {
        Console.WriteLine (String.Format("evaluating :{0}", sum));
        return Math.Abs(sum) >= 5 ? 1 : 0;

    }
}

输出样本:

https://onedrive.live.com/redir?resid=8a0ad995f81066db!176&authkey=!AGgY8Iy4g_8lpv4&ithint=file%2crtf


I am a newbie to machine learning and have been experimenting with basic perceptrons before moving on to multilayer networks.

The problem I have is with the code below. I have a training data generator which uses a set of weights to generate a truth table.

The problem I have is the perceptron is able to solve/determine the set of weights when the training data was generated with set 'A' but not with set 'B'. When given training data that was generated with set 'B', it continues in an infinite loop trying to determine the weights (is this a local minimum issue?)

I do not understand exactly why this is happening. Any help or advice is appreciated.

Thanks in advance.

// Calling function 
public static void TestPerceptron ()
    {
        // Problem: 
        // When training data is generated using the 'A' set of weights, the perceptron is able to determine the correct weights based on the given training data.
        // When training data is generated using the 'B' set of weights, the perceptron never completes training and is stuck in an infinite loop

        double[] weights = new double[] {
            //3,2,2,3 // A
            3,2,1,3,1 // B
        };

        double bias = 0.0; 

        var trainingData = PerceptronHelper.GenerateDataSetUsingWeights (weights, bias);
        var perceptron = new Perceptron ();
        perceptron.Train (trainingData, null, null);

        //perceptron.Train (trainingData, weights, bias); 
    }


public class Perceptron
{
    private static Random r = new Random ();
    protected double _bias = r.NextDouble();
    protected double[] _weights;

    protected virtual double ComputeOutput(double[] weights, double[] inputs, double bias)
    {
        var total = 0.0;

        for (var index = 0; index < inputs.Length-1; index++) 
        {
            total += weights [index] * inputs [index];
        }

        return total + (1 * bias);
    }

    protected virtual void SetWeights(ref double[] weights, double[] inputs, double error, double learningRate, ref double bias)
    {
        for (var index = 0; index < inputs.Length-1; index++) 
        {
            weights[index] = weights [index] + (learningRate * error * inputs [index]);
        }

        bias += learningRate * error * 1;
    }

    public virtual void Train(double[][] trainingData, double[] idealWeights, double? idealBias)
    {
        var learningRate = 1.0;
        var totalError = 1.0;
        var targetError = 0.0;
        var epochs = 0.0;
        var bias = _bias;
        var weights = new double[trainingData[0].Length-1];

        if (idealBias.HasValue)
            bias = idealBias.Value;

        if (idealWeights != null)
            weights = idealWeights;

        while (totalError > targetError) 
        {
            totalError = 0.0;

            for (var index = 0; index < trainingData.Length; index++) 
            {
                var inputs = trainingData [index];

                // get target
                var target = inputs [inputs.Length - 1];

                // compute output
                var computed = ComputeOutput (weights, inputs, bias);

                // pass computed through activation
                var output = PerceptronHelper.Activation (computed);

                // determine error 
                var error = (target - output);

                // adjust weights
                SetWeights (ref weights, inputs, error, learningRate, ref bias);

                totalError += Math.Abs(error);

                var weightsMsg = "Weights: ";

                foreach(var weight in weights)
                {
                    weightsMsg += weight + "|";
                }

                Console.WriteLine (String.Format ("error: {0} weights: {1} bias: {2}", totalError, weightsMsg, bias));
            }

            epochs++;
        }

        _bias = bias;
        _weights = weights;
    }

    public void Predict(double[] inputs)
    {
        var sum = 0.0;

        for (var index = 0; index < inputs.Length; index++) 
        {
            sum += inputs [index] * _weights [index] + 1 * _bias;

            Console.WriteLine (String.Format("input: {0} weight: {1} bias: {2}", inputs[index], _weights[index], _bias));
        }

        var output = PerceptronHelper.Activation (sum);
        Console.WriteLine ("Output:{0}", output);
    }
}


public static class PerceptronHelper
{
    // generate training data based on given weights - the number of inputs = number of weights 
    public static double[][] GenerateDataSetUsingWeights(double[] idealWeights, double bias)
    {
        var weights = idealWeights;
        var inputs = new double[weights.Length];
        var numInputCombinations = Math.Pow(2,inputs.Length); 
        var trainData = new double[(int)numInputCombinations][];
        int inputValue = 0;

        // generate training data
        for (var index = 0; index < numInputCombinations; index++) 
        {
            var sum = 0.0;  

            // last item in array is expected output
            var trainDataLine = new double[weights.Length+1];

            var binary = Convert.ToString (inputValue, 2);
            binary = binary.PadLeft (weights.Length, '0');

            // create training data line
            for (var wIndex = 0; wIndex < weights.Length; wIndex++) 
            {
                inputs [wIndex] = double.Parse(binary[wIndex].ToString());
                trainDataLine [wIndex] = inputs [wIndex];
                sum += inputs [wIndex] * weights [wIndex];
            }

            sum += (1 * bias);

            var output = Activation (sum);

            // store the expected result in the last item of the array
            trainDataLine [weights.Length] = output;

            // add the line to the data
            trainData[index] = trainDataLine;

            inputValue++;
        }

        return trainData;
    }

    public static double Activation (double sum) 
    {
        Console.WriteLine (String.Format("evaluating :{0}", sum));
        return Math.Abs(sum) >= 5 ? 1 : 0;

    }
}

A sample of the output:

https://onedrive.live.com/redir?resid=8a0ad995f81066db!176&authkey=!AGgY8Iy4g_8lpv4&ithint=file%2crtf


原文:https://stackoverflow.com/questions/29885530
2022-03-13 15:03

相关文章

更多

实现Hadoop中的机架感知

原理 Hadoop中声明是有机架感知的功能,能够提高hadoop的性能。平时我们使用的hadoop集群 ...

Hadoop机架感知与balancer

版本:Apache Hadoop 1.0.3 Hadoop集群节点通常会跨很多个机架,增加节点的情况时 ...

Hadoop配置机架感知(Python脚本)

昨天QQ群里提了一个Hadoop运行效率分配的问题,总结一下,写个文章。集群使用hadoop-1.0. ...

Facebook的Hadoop应用与故障转移方案

  在《 数据大爆炸 一分钟=60秒=海量数据》一文中,我们曾提到在短短的60秒内,Faceboo ...

Hadoop集群启动机架感知

Hadoop集群的机架感知功能是提高hadoop集群网络性能的重要参数,配置hadoop机架感知功能的 ...

SecondaryNamenode配置与NameNode故障恢复

*注:本文基于0.20.2配置测试,0.21以后已改成Checkpoint Node和Backup N ...

Hadoop Datanode支持磁盘故障代码hack

背景 Hadoop当中的每一个datanode上,都会保存一些HDFS中文件 的blocks,而这些b ...

Windows7系统蓝屏故障分析:蓝屏代码详解

此蓝色背景错误信息画面一般简称为“蓝屏(Blue Screen)”或“停止屏(Stop Error S ...

Windows XP/Vista/Windows 7常见蓝屏故障分析

Windows XP/Vista/Windows 7常见蓝屏故障分析 当您在运行Microsoft W ...

Hadoop集群网络性能优化:Hadoop机架感知实现及配置

背景 分布式的集群通常包含非常多的机器,由于受到机架槽位和交换机网口的限制,通常大型的分布式集群都会跨 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本