PDA

View Full Version : سوال: مشکل در کدنویسی الگوریتم



amir.nazarizadeh
سه شنبه 27 تیر 1396, 19:59 عصر
سلام
خسته نباشید دوستان
من کد زیر رو نوشتم:


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 }, { 0.5, 0.5 } };
static double[] bias = { 0.3, 0.15, 0.5 };
static bool epoch = true;
static int input1 = 0, input2 = 0;
static int k = 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);




z_input[0] = bias[0] + (-1*weigh[0, 0]) + (-1* weigh[1, 0]);
z_layer[0] = f(z_input[0]);
z_input[1] = bias[1] + (-1* weigh[0, 1]) + (-1* weigh[1, 1]);
z_layer[1] = f(z_input[1]);
y_input = bias[2] + (0.5) * (z_layer[0]) + (0.5) * (z_layer[1]);
if (y_input >= 0)
Console.WriteLine("1");
else
Console.WriteLine("-1");








Console.ReadLine();
}








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




double[,] bestWeghits = new double[2, 1];


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] * weigh[2, 0] + z_layer[1] * weigh[2, 1];
y = f(y_input);
if (target[j] != y)
{
epoch = true;
if (target[j] == -1)
{




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




}
else if (target[j] == 1)
{
if (Math.Abs(z_input[0]) < Math.Abs(z_input[1]))
k = 0;
else
k = 1;




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




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









که الگوریتم madaline از شبکه های عصبی هستش ولی اینطور که معلومه اولا حلقه ی while هیچ تاثیری روی وزن ها نداره و اینکه جواب درستی نمیده
کد واسه شبیه سازی تابع xor هستش که بین دوتا عدد انجام میشه و با بدست آوردن وزن مناسب جواب رو تخمین میزنه
الگوریتمش اینجاست:
https://img5.file-upload.com/i/00347/jwacbvqqzicy.jpg

مشکل کدم چیه؟ ممنون میشم کمک کنید.

Mahmoud.Afrad
سه شنبه 27 تیر 1396, 21:43 عصر
به نظرم خط 67 باید input2 باشه که اشتباه نوشتی.

amir.nazarizadeh
سه شنبه 27 تیر 1396, 21:52 عصر
به نظرم خط 67 باید input2 باشه که اشتباه نوشتی.
بله درستش کردم ولی کماکان مشکل حل نمیشه :ناراحت: