如何训练Perceptron学习使用成本函数

时间:2019-04-04 07:23:05

标签: c# perceptron

我有一个读取数据文件的感觉,我需要训练和更新权重,以使用逻辑S形函数获得最接近曲线底部的值。我需要弄清楚如何实现成本函数,以便降低速度,直到给出最准确的值为止。

        List<string> robot = new List<string>();
        using (StreamReader sr = File.OpenText(path))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                robot.Add(line);
            }
        }

        List<float> Inputs = new List<float>();


        for (int i = 0; i < robot.Count; i++)
        {
            var text = robot[i].Split(',').ToArray();
            string V1 = text[2];
            Inputs.Add(float.Parse(V1));

        }

        float max = Inputs.Max();
        float min = Inputs.Min();

        for (int i = 0; i < Inputs[i]; i++)
        {
            Inputs[i] = ((Inputs[i] - min) / (max - min));
        }

        for (int i = -2; i < Inputs.Count - 2; i++)
        {
            Program p = new Program(3);
            float result;

            if (i == -2)
            {
                float[] point = { 0, 0, Inputs[0] };
                result = p.Feed(point);
            }
            else if (i == -1)
            {
                float[] point = { 0, Inputs[0], Inputs[1] };
                result = p.Feed(point);
            }
            else
            {
                float[] point = { Inputs[i], Inputs[i + 1], Inputs[i + 2] };
                result = p.Feed(point);
            }

            Console.WriteLine(result);
        }

    }

    Program(int n)
    {
        weights = new float[n];

        for (int i = 0; i < weights.Length; i++)
        {
            weights[i] = (float)RandomNumberBetween(-0.5, 0.5);
        }
    }

    float Feed(float[] inputs)
    {
        int bias = 1;

        float sum = 0;
        for (int i = 0; i < weights.Length; i++)
        {
            sum += inputs[i] * weights[i] + bias * (float)RandomNumberBetween(-0.5, 0.5);
        }
        return Activate(sum);
    }

    float Activate(float sum)
    {
        if (sum > 0) return 1;
        else return 0;
    }

    float Sigmoid(float sum)
    {
        return (float)(1.0 / (1.0 + Math.Pow(Math.E, -sum)));
    }

    void Train(float[] inputs, int desired)
    {
        float guess = Feed(inputs);
        float error = desired - guess;
        for (int i = 0; i < weights.Length; i++)
        {
            weights[i] += c * error * inputs[i];
        }
    }
}

我需要实现一个成本函数,该函数将通过数据更新权重,直到权重达到曲线的底部为止。

0 个答案:

没有答案