⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 简单的神经网络训练程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace EX2
{
    class Program
    {
        static void Main(string[] args)
        {
            W w1 = new W(2, -2, 0);
            W w2 = new W(1, 3, -1);
            X x1 = new X(1, 0, 1);
            W w3 = new W(3, -2, -1);
            float s11 = w1.x * x1.x + w1.y * x1.y + w1.z * x1.z;
            float f11 = 1/((float)(1+System.Math.Exp(-s11)));
            float s12 = x1.x * w2.x + x1.y * w2.y + x1.z * w2.z;
            float f12 = 1 / ((float)(1 + System.Math.Exp(-s12)));
            X x2 = new X(f11, f12, 1);
            float s21 = w3.x * x2.x + w3.y * x2.y + w3.z * x2.z;
            float f21 = 1 / ((float)(1 + System.Math.Exp(-s21)));
            float theta21 = (0 - f21) * f21 * (1 - f21);
            float theta11 = f11 * (1 - f11) * theta21 * w3.x;
            float theta12 = f12 * (1 - f12) * theta21 * w3.y;
            w1.x = w1.x + theta11 * x1.x;
            w1.y = w1.y + theta11 * x1.y;
            w1.z = w1.z + theta11 * x1.z;
            w2.x = w2.x + theta12 * x1.x;
            w2.y = w2.y + theta12 * x1.y;
            w2.z = w2.z + theta12 * x1.z;
            w3.x = w3.x + theta21 * x2.x;
            w3.y = w3.y + theta21 * x2.y;
            w3.z = w3.z + theta21 * x2.z;
            x1.x = 0;
            x1.y = 0;
            x1.z = 1;
            s11 = w1.x * x1.x + w1.y * x1.y + w1.z * x1.z;
            f11 = 1 / ((float)(1 + System.Math.Exp(-s11)));
            s12 = x1.x * w2.x + x1.y * w2.y + x1.z * w2.z;
            f12 = 1 / ((float)(1 + System.Math.Exp(-s12)));
            x2.x = s11;
            x2.y = s12;
            x2.z = 1;
            s21 = w3.x * x2.x + w3.y * x2.y + w3.z * x2.z;
            f21 = 1 / ((float)(1 + System.Math.Exp(-s21)));
            theta21 = (1 - f21) * f21 * (1 - f21);
            theta11 = f11 * (1 - f11) * theta21 * w3.x;
            theta12 = f12 * (1 - f12) * theta21 * w3.y;
            w1.x = w1.x + theta11 * x1.x;
            w1.y = w1.y + theta11 * x1.y;
            w1.z = w1.z + theta11 * x1.z;
            w2.x = w2.x + theta12 * x1.x;
            w2.y = w2.y + theta12 * x1.y;
            w2.z = w2.z + theta12 * x1.z;
            w3.x = w3.x + theta21 * x2.x;
            w3.y = w3.y + theta21 * x2.y;
            w3.z = w3.z + theta21 * x2.z;
            x1.x = 0;
            x1.y = 1;
            x1.z = 1;
            s11 = w1.x * x1.x + w1.y * x1.y + w1.z * x1.z;
            f11 = 1 / ((float)(1 + System.Math.Exp(-s11)));
            s12 = x1.x * w2.x + x1.y * w2.y + x1.z * w2.z;
            f12 = 1 / ((float)(1 + System.Math.Exp(-s12)));
            x2.x = s11;
            x2.y = s12;
            x2.z = 1;
            s21 = w3.x * x2.x + w3.y * x2.y + w3.z * x2.z;
            f21 = 1 / ((float)(1 + System.Math.Exp(-s21)));
            theta21 = (0 - f21) * f21 * (1 - f21);
            theta11 = f11 * (1 - f11) * theta21 * w3.x;
            theta12 = f12 * (1 - f12) * theta21 * w3.y;
            w1.x = w1.x + theta11 * x1.x;
            w1.y = w1.y + theta11 * x1.y;
            w1.z = w1.z + theta11 * x1.z;
            w2.x = w2.x + theta12 * x1.x;
            w2.y = w2.y + theta12 * x1.y;
            w2.z = w2.z + theta12 * x1.z;
            w3.x = w3.x + theta21 * x2.x;
            w3.y = w3.y + theta21 * x2.y;
            w3.z = w3.z + theta21 * x2.z;

            x1.x =1;
            x1.y = 1;
            x1.z = 1;
            s11 = w1.x * x1.x + w1.y * x1.y + w1.z * x1.z;
            f11 = 1 / ((float)(1 + System.Math.Exp(-s11)));
            s12 = x1.x * w2.x + x1.y * w2.y + x1.z * w2.z;
            f12 = 1 / ((float)(1 + System.Math.Exp(-s12)));
            x2.x = s11;
            x2.y = s12;
            x2.z = 1;
            s21 = w3.x * x2.x + w3.y * x2.y + w3.z * x2.z;
            f21 = 1 / ((float)(1 + System.Math.Exp(-s21)));
            theta21 = (1 - f21) * f21 * (1 - f21);
            theta11 = f11 * (1 - f11) * theta21 * w3.x;
            theta12 = f12 * (1 - f12) * theta21 * w3.y;
            w1.x = w1.x + theta11 * x1.x;
            w1.y = w1.y + theta11 * x1.y;
            w1.z = w1.z + theta11 * x1.z;
            w2.x = w2.x + theta12 * x1.x;
            w2.y = w2.y + theta12 * x1.y;
            w2.z = w2.z + theta12 * x1.z;
            w3.x = w3.x + theta21 * x2.x;
            w3.y = w3.y + theta21 * x2.y;
            w3.z = w3.z + theta21 * x2.z;
            w1.print();
            w2.print();
            w3.print();
            Console.ReadLine();

         
        }
    }

    class W
    {
        public W(float a, float b, float c)
        {
            x = a;
            y = b;
            z = c;
        }
        public float x, y, z;
        public void print()
        {
            Console.Write(x + "\t");
            Console.Write(y + "\t");
            Console.WriteLine(z);
        }
    }

    class X
    {
        public X(float a, float b, float c)
        {
            x = a;
            y = b;
            z = c;
        }
        public float x, y, z;
        public void print()
        {
            Console.Write(x + "\t");
            Console.Write(y + "\t");
            Console.WriteLine(z);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -