代码实现madaline训练算法的问题 [英] problem with code implementing madaline training algorithm

查看:102
本文介绍了代码实现madaline训练算法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好吗


我想在c#中实现xor函数的madaline算法


我的代码在这里:


使用System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;

名称空间xsvasdf
{
class程序
{
static int [,] input = {{1,1},{1,-1} ,{-1,1}};

static int [] target = {-1,1,1};
static double [,] weigh = {{0.05,0.1},{0.2,0.2}};
static double [] vector = {0.5,0.5};
static double [] bias = {0.3,0.15,0.5};
static bool epoch = true;
static int k;
static int input1 = 0,input2 = 0;
static double [] z_input = new double [2];
static int [] z_layer = new int [2];
static double y_input;
static int y;
static int pp = 0;

static void Main(string [] args)
{
weigh = train(weigh,bias,target);


Console.WriteLine(" b1:{0},b2:{1},b3:{2} \ n w00:{3},w10:{4}, w01:{5},w11:{6} \ n pp:{7}",偏见[0],偏差[1],偏差[2],称重[0,0],称重[1,0] ,称[0,1],称重[1,1],pp);


}


public static double [,] train(double [,] weigh,double [] bias,int [] target)
{


而(epoch)
{
epoch = false;
for(int j = 0; j< 3; j ++)
{

input1 = input [j,0];
input2 =输入[j,1];
z_input [0] = bias [0] +(input1 * weigh [0,0])+(input2 * weigh [1,0]);
z_layer [0] = f(z_input [0]);
z_input [1] = bias [1] +(input1 * weigh [0,1])+(input2 * weigh [1,1]);
z_layer [1] = f(z_input [1]);
y_input = bias [2] + z_layer [0] * vector [0] + z_layer [1] * vector [1];
y = f(y_input);
if(target [j]!= y)
{
epoch = true;
if(target [j] == -1)
{
if(z_input [0]> 0.0&& z_input [1]> 0.0)
{
bias [0] + = 0.5 *( - 1 - z_input [0]);
称重[0,0] + = 0.5 *( - 1 - z_input [0])* input1;
称重[1,0] + = 0.5 *( - 1 - z_input [0])* input2;

bias [1] + = 0.5 *( - 1 - z_input [1]);
称重[0,1] + = 0.5 *( - 1 - z_input [1])* input1;
称重[1,1] + = 0.5 *( - 1 - z_input [1])* input2;
}


否则如果(z_input [1]> 0.0&& z_input [0]< = 0)
{
bias [1] + = 0.5 *( - 1 - z_input [1]);
称重[0,1] + = 0.5 *( - 1 - z_input [1])* input1;
称重[1,1] + = 0.5 *( - 1 - z_input [1])* input2;
}
else if(z_input [0]> 0.0&& z_input [1]< = 0)
{
bias [0] + = 0.5 *( -1 - z_input [0]);
称重[0,0] + = 0.5 *( - 1 - z_input [0])* input1;
称重[1,0] + = 0.5 *( - 1 - z_input [0])* input2;
}
}

否则if(target [j] == 1)
{
if(Math.Abs​​(z_input [0])< ; Math.Abs​​(z_input [1]))
{
bias [0] = bias [0] + 0.5 *(1 - z_input [0]);
称重[0,0] =称重[0,0] + 0.5 *(1 - z_input [0])* input1;
称重[1,0] =称重[0,0] + 0.5 *(1 - z_input [0])* input2;
}
其他
{
bias [1] = bias [1] + 0.5 *(1 - z_input [1]);
称重[0,1] =称重[0,1] + 0.5 *(1 - z_input [1])* input1;
称重[1,1] =称重[0,1] + 0.5 *(1 - z_input [1])* input2;


}
}

for(int g = 0; g< 2; g ++)
for(int s = 0; s< 2; s ++)
{
bestWeghits [g,s] + =称重[g,s];
}
}
else {继续; }
}
}


返回权重;

}

public static int f(double xIn)
{
return(xIn> = 0.0)? 1:-1;
}
}
}

但它没有给我很好的回应


它首先用输入和目标训练网络,然后得到两个双极并预测响应


但它不能完美训练并给出好的重量!

帮我... /

解决方案

你好AmirReza Nazarizadeh,


感谢您在此发帖。


能否指出您的数据意味着,例如
target,vector ,偏重,z_input,z_layer等?     ;


 


< p style ="margin:0px"> 此外,我测试你的代码。我没有找到
的定义
bestWeghits
数组。我想也许你想要返回
bestWeghits 而不是
称重。


 


bestWeghits定义于您的列车方法如下。

 public static double [,] train(double [,] weigh,double [] bias,int [] target )
        {
 
            double [,] bestWeghits = new double [2,2];
            ...
            返回称重; //返回wrigh是没有意义的。它在代码的开头定义了代码中的值。
 
        }


Weigh的结果:



BestWeghits'结果:



< span style ="margin:0px; color:#666666; font-family:'Segoe UI',sans-serif; font-size:10pt"> >> 首先使用输入和目标训练网络,然后使用
获得两个双极并预测响应


您能否提供更多详情?你想得到什么?你得到错误吗?


最好的问候,


Wendy


hello there

I want to implement madaline algorithm for xor function in c#

my code is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace xsvasdf
{
    class Program
    {
        static int[,] input = { { 1, 1 }, { 1, -1 }, { -1, 1 } };
        
        static int[] target = { -1, 1, 1 };
        static double[,] weigh = { { 0.05, 0.1 }, { 0.2, 0.2 } };
        static double[] vector = { 0.5, 0.5 };
        static double[] bias = { 0.3, 0.15, 0.5 };
        static bool epoch = true;
        static int k;
        static int input1 = 0, input2 = 0;
        static double[] z_input = new double[2];
        static int[] z_layer = new int[2];
        static double y_input;
        static int y;
        static int pp = 0;

        static void Main(string[] args)
        {
            weigh = train(weigh, bias, target);


            Console.WriteLine("b1:{0} , b2:{1} , b3:{2} \n w00:{3} , w10:{4} , w01:{5}, w11:{6} \n pp:{7}", bias[0], bias[1], bias[2], weigh[0, 0], weigh[1, 0], weigh[0, 1], weigh[1, 1], pp);

           
        }


        public static double[,] train(double[,] weigh, double[] bias, int[] target)
        {
     
                
            while (epoch)
            {
                epoch = false;
                for (int j = 0; j < 3; j++)
                {

                    input1 = input[j, 0];
                    input2 = input[j, 1];
                    z_input[0] = bias[0] + (input1 * weigh[0, 0]) + (input2 * weigh[1, 0]);
                    z_layer[0] = f(z_input[0]);
                    z_input[1] = bias[1] + (input1 * weigh[0, 1]) + (input2 * weigh[1, 1]);
                    z_layer[1] = f(z_input[1]);
                    y_input = bias[2] + z_layer[0] * vector[0] + z_layer[1] * vector[1];
                    y = f(y_input);
                    if (target[j] != y)
                    {
                        epoch = true;
                        if (target[j] == -1)
                        {
                            if (z_input[0] > 0.0 && z_input[1] > 0.0)
                            {
                                bias[0] += 0.5 * (-1 - z_input[0]);
                                weigh[0, 0] += 0.5 * (-1 - z_input[0]) * input1;
                                weigh[1, 0] += 0.5 * (-1 - z_input[0]) * input2;

                                bias[1] += 0.5 * (-1 - z_input[1]);
                                weigh[0, 1] += 0.5 * (-1 - z_input[1]) * input1;
                                weigh[1, 1] += 0.5 * (-1 - z_input[1]) * input2;
                            }


                            else if (z_input[1] > 0.0 && z_input[0] <= 0)
                            {
                                bias[1] += 0.5 * (-1 - z_input[1]);
                                weigh[0, 1] += 0.5 * (-1 - z_input[1]) * input1;
                                weigh[1, 1] += 0.5 * (-1 - z_input[1]) * input2;
                            }
                            else if (z_input[0] > 0.0 && z_input[1] <= 0)
                            {
                                bias[0] += 0.5 * (-1 - z_input[0]);
                                weigh[0, 0] += 0.5 * (-1 - z_input[0]) * input1;
                                weigh[1, 0] += 0.5 * (-1 - z_input[0]) * input2;
                            }
                        }

                        else if (target[j] == 1)
                        {
                            if (Math.Abs(z_input[0]) < Math.Abs(z_input[1]))
                            {
                                bias[0] = bias[0] + 0.5 * (1 - z_input[0]);
                                weigh[0, 0] = weigh[0, 0] + 0.5 * (1 - z_input[0]) * input1;
                                weigh[1, 0] = weigh[0, 0] + 0.5 * (1 - z_input[0]) * input2;
                            }
                            else
                            {
                                bias[1] = bias[1] + 0.5 * (1 - z_input[1]);
                                weigh[0, 1] = weigh[0, 1] + 0.5 * (1 - z_input[1]) * input1;
                                weigh[1, 1] = weigh[0, 1] + 0.5 * (1 - z_input[1]) * input2;

                                
                            }
                        }
                        
                        for(int g=0; g<2;g++)
                            for(int s=0; s<2; s++)
                            {
                                bestWeghits[g, s] += weigh[g, s];
                            }
                    }
                    else { continue; }
                }
            }


                    return weigh;
            
        }

        public static int f(double xIn)
        {
            return (xIn >= 0.0) ? 1 : -1;
        }
    }
}

but it does not give me the good response

it first train net with inputs and targets and then get two bipolar and predict the response

but it can not train perfectly and give good weights!
help me.../

解决方案

Hi AmirReza Nazarizadeh,

Thank you for posting here.

Could you please indicate what your data means, such as target ,vector ,bias weigh ,z_input ,z_layer and so on?  

 

Besides , I test your code. I didn’t find the definition of the bestWeghits array in your code. I guess that maybe you want to return the bestWeghits rather than the weigh.

 

The bestWeghits is defined in your train method, as follows.

public static double[,] train(double[,] weigh, double[] bias, int[] target)
        {
 
            double[,] bestWeghits = new double[2, 2];
            …
            return weigh;//It is meaningless to return wrigh. It is defined at the beginning with value in your code.
 
        }

Weigh’s result:

BestWeghits’ result:

>>it first train net with inputs and targets and then get two bipolar and predict the response

Could you provide more details? What you want to get? Do you get the error?

Best Regards,

Wendy


这篇关于代码实现madaline训练算法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆