本文介绍了代码实现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; } } } 但它没有给我很好的回应 它首先用输入和目标训练网络,然后得到两个双极并预测响应 但它不能完美训练并给出好的重量! 帮我... / 解决方案 hello thereI 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 responseit first train net with inputs and targets and then get two bipolar and predict the responsebut it can not train perfectly and give good weights!help me.../ 解决方案 这篇关于代码实现madaline训练算法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-11 00:00