PB算法的C#好像自己做的有问题
public partial class Form1 : Form{
//Sigmoid函数
static double Sigmoid(double x1, double x2, double x3, double w1, double w2, double w3){
double s = x1 * w1 + x2 * w2 + x3 * w3;
return 1 / (1 + Math.Pow(Math.E, -s));
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double c = 0.8;
double[,] W = { { 2, -2, 0 }, { 1, 3, -1 }, { 3, -2, -1 } };
double[,] x = { { 1, 0, 1 }, { 0, 0, 1 },{ 0, 1, 1 }, { 1, 1, 1 } };
double[] d = { 0, 1,0, 1 };
double f11 = Sigmoid(x[0, 0], x[0, 1], x[0, 2], W[0, 0], W[0, 1], W[0, 2]);
double f12 = Sigmoid(x[0, 0], x[0, 1], x[0, 2], W[1, 0], W[1, 1], W[1, 2]);
double f = Sigmoid(f11, f12, 1, W[2, 0], W[2, 1], W[2, 2]);
//仅用第一组数:W全用,x用{1,0,1}d:0 c:学习率
textBox1.Text = f11.ToString();textBox2.Text = f12.ToString();
textBox3.Text = f.ToString();
//第一次运算后f11、f12、f的值
double[,] q = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } ,{0,0,0}};
q[0,0] = (d[0] - f) * f * (1 - f);
q[0,1] = f11 * (1 - f11) * (q[0,0] * W[2, 0]);
q[0,2] = f12 * (1 - f12) * (q[0,0] * W[2, 1]);
for (var j = 0; j < 3; j++){W[0, j] = W[0, j] + c * q[0,1] * x[0, j];}
//W[0, 0] = W[0, 0] + c * q[1] * f11;
//W[0, 1] = W[0, 1] + c * q[1] * f12;
//W[0, 2] = W[0, 2] + c * q[1] * 1;
for (var j = 0; j < 3; j++){W[1, j] = W[1, j] + c * q[0,2] * x[0, j];}
W[2, 0] = W[2, 0] + c * q[0,0] * f11;
W[2, 1] = W[2, 1] + c * q[0,0] * f12;
W[2, 2] = W[2, 2] + c * q[0,0] * 1;
textBox23.Text = W[0, 0].ToString();textBox22.Text = W[0, 1].ToString();
textBox21.Text = W[0, 2].ToString();
textBox20.Text = W[1, 0].ToString();textBox19.Text = W[1, 1].ToString();
textBox18.Text = W[1, 2].ToString();
textBox17.Text = W[2, 0].ToString();textBox16.Text = W[2, 1].ToString();
textBox15.Text = W[2, 2].ToString();
//第一次运算后3个W的值
long m = 1;
int aa;
double bb=1;
int.TryParse(textBox14.Text,out aa);
//int i;
//进入循环 aa表示次数
//for (var i = 0; i < aa;i++ )
while(bb>=Math.Pow(10,-aa))
{
f11 = Sigmoid(x[m % 4, 0], x[m % 4, 1], x[m % 4, 2], W[0, 0], W[0, 1], W[0, 2]);
f12 = Sigmoid(x[m % 4, 0], x[m % 4, 1], x[m % 4, 2], W[1, 0], W[1, 1], W[1, 2]);
f = Sigmoid(f11, f12, 1, W[2, 0], W[2, 1], W[2, 2]);
q[m%4,0] = (d[m % 4] - f) * f * (1 - f);
q[m % 4, 1] = f11 * (1 - f11) * (q[m % 4,0] * W[2, 0]);
q[m % 4, 2] = f12 * (1 - f12) * (q[m % 4,0] * W[2, 1]);
for (var j = 0; j < 3; j++) { W[0, j] = W[0, j] + c * q[m % 4,1] * x[m % 4, j]; }
for (var j = 0; j < 3; j++) { W[1, j] = W[1, j] + c * q[m % 4,2] * x[m % 4, j]; }
W[2, 0] = W[2, 0] + c * q[m % 4,0] * f11; W[2, 1] = W[2, 1] + c * q[m % 4, 0] * f12;
W[2, 2] = W[2, 2] + c * q[m % 4,0];
bb = (Math.Abs(q[0, 0]) + Math.Abs(q[0, 1]) + Math.Abs(q[0, 2]) + Math.Abs(q[1, 0])
+ Math.Abs(q[1, 1]) + Math.Abs(q[1, 2]) + Math.Abs(q[2, 0]) + Math.Abs(q[2, 1])
+ Math.Abs(q[2, 2]) + Math.Abs(q[3, 0]) + Math.Abs(q[3, 1]) + Math.Abs(q[3, 2])) / 12;
m++;
}
//bb = (Math.Abs(q[0]) + Math.Abs(q[1]) + Math.Abs(q[2])) / 3;
textBox4.Text = W[0, 0].ToString();textBox5.Text = W[0, 1].ToString();
textBox6.Text = W[0, 2].ToString();
textBox7.Text = W[1, 0].ToString();textBox8.Text = W[1, 1].ToString();
textBox9.Text = W[1, 2].ToString();
textBox10.Text = W[2, 0].ToString();textBox11.Text = W[2, 1].ToString();
textBox12.Text = W[2, 2].ToString();
textBox13.Text = m.ToString();
//逼近后的结果
double x11 = Sigmoid(x[0, 0], x[0, 1], x[0, 2], W[0, 0], W[0, 1], W[0, 2]);
double x21 = Sigmoid(1, 0, 1, W[1, 0], W[1, 1], W[1, 2]);
double xa1 = Sigmoid(x11, x21, 1, W[2, 0], W[2, 1], W[2, 2]);
textBox24.Text = xa1.ToString();
double x12 = Sigmoid(x[1, 0], x[1, 1], x[1, 2], W[0, 0], W[0, 1], W[0, 2]);
double x22 = Sigmoid(x[1, 0], x[1, 1], x[1, 2], W[1, 0], W[1, 1], W[1, 2]);
double xa2 = Sigmoid(x12, x22, 1, W[2, 0], W[2, 1], W[2, 2]);
textBox25.Text = xa2.ToString();
double x13 = Sigmoid(0, 1, 1, W[0, 0], W[0, 1], W[0, 2]);
double x23 = Sigmoid(0, 1, 1, W[1, 0], W[1, 1], W[1, 2]);
double xa3 = Sigmoid(x13, x23, 1, W[2, 0], W[2, 1], W[2, 2]);
textBox26.Text = xa3.ToString();
double x14 = Sigmoid(1, 1, 1, W[0, 0], W[0, 1], W[0, 2]);
double x24 = Sigmoid(1, 1, 1, W[1, 0], W[1, 1], W[1, 2]);
double xa4 = Sigmoid(x14, x24, 1, W[2, 0], W[2, 1], W[2, 2]);
textBox27.Text = xa4.ToString();
textBox28.Text = bb.ToString();
}
}
我觉得自己写得没问题,另外q[]最为dalte用一组和全用的方式都使用了,可是结果都是{0,0,1}和{0,1,1}两组是对的,其他2组都只趋近0。5,感觉是q的问题,通过检测,发现不能成功的2组的q的变化太慢了,几乎不能趋近0;
强帮忙看看是什么地方我有问题的。
补充:.NET技术 , C#