cscの作法 その32 ニューラルネットワーク
3277 ワード
概要
cscの作法、調べてみた。
tensorflow.jsでxorを学習して、バイアスとウェイトを取り出して、c#で推論してみた。
写真
サンプルコード
using System;
using System.Windows.Forms;
using System.Drawing;
class form1: Form {
form1() {
Text = "nn";
ClientSize = new Size(200, 200);
double[][] w1 = {
new double[] {-0.8971094, 1.4029436, -1.3633214, -0.775598, 1.2531579, 1.0529767, 1.3460118, -1.5597397},
new double[] {1.57735, -1.1888924, 1.6184694, -0.5399966, -0.9783241, 0.9480417, 0.9573314, -1.1738404}
};
double[] b1 = {0.428151, 0.7863605, 0.8015493, 0.8869765, 0.6552361, 0.3099377, 0.1825127, 0.0495548};
double[][] w2 = {
new double[] {-1.7588012},
new double[] {-1.5193572},
new double[] {-1.1239196},
new double[] {0.8726832},
new double[] {-1.7272931},
new double[] {1.4907871},
new double[] {1.1091278},
new double[] {-0.9800876}
};
double[] b2 = {0.1271933};
string res = "";
for (int i = 0; i < 4; i++)
{
int k;
int l;
double[] x = {0, 0};
double[] y = {0};
double[] h = {0, 0, 0, 0, 0, 0, 0, 0};
if (i == 0)
{
x[0] = 1.0;
x[1] = 0;
res += "1, 0 = ";
}
if (i == 1)
{
x[0] = 0;
x[1] = 1.0;
res += "0, 1 = ";
}
if (i == 2)
{
x[0] = 1.0;
x[1] = 1.0;
res += "1, 1 = ";
}
if (i == 3)
{
x[0] = 0;
x[1] = 0;
res += "0, 0 = ";
}
for (k = 0; k < 2; k++)
{
for (l = 0; l < 8; l++)
{
h[l] += x[k] * w1[k][l];
}
}
for (l = 0; l < 8; l++)
{
h[l] += b1[l];
h[l] = tanh(h[l]);
}
for (k = 0; k < 8; k++)
{
for (l = 0; l < 1; l++)
{
y[l] += h[k] * w2[k][l];
}
}
for (k = 0; k < 1; k++)
{
y[k] += b2[k];
y[k] = sigmoid(y[k]);
}
res += y[0].ToString() + "\r\n";
}
MessageBox.Show(res);
}
double tanh(double x) {
double a = Math.Exp(x);
double b = Math.Exp(-x);
return (a - b) / (a + b);
}
double sigmoid(double x) {
return 1.0 / (1.0 + Math.Exp(-x));
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
class form1: Form {
form1() {
Text = "nn";
ClientSize = new Size(200, 200);
double[][] w1 = {
new double[] {-0.8971094, 1.4029436, -1.3633214, -0.775598, 1.2531579, 1.0529767, 1.3460118, -1.5597397},
new double[] {1.57735, -1.1888924, 1.6184694, -0.5399966, -0.9783241, 0.9480417, 0.9573314, -1.1738404}
};
double[] b1 = {0.428151, 0.7863605, 0.8015493, 0.8869765, 0.6552361, 0.3099377, 0.1825127, 0.0495548};
double[][] w2 = {
new double[] {-1.7588012},
new double[] {-1.5193572},
new double[] {-1.1239196},
new double[] {0.8726832},
new double[] {-1.7272931},
new double[] {1.4907871},
new double[] {1.1091278},
new double[] {-0.9800876}
};
double[] b2 = {0.1271933};
string res = "";
for (int i = 0; i < 4; i++)
{
int k;
int l;
double[] x = {0, 0};
double[] y = {0};
double[] h = {0, 0, 0, 0, 0, 0, 0, 0};
if (i == 0)
{
x[0] = 1.0;
x[1] = 0;
res += "1, 0 = ";
}
if (i == 1)
{
x[0] = 0;
x[1] = 1.0;
res += "0, 1 = ";
}
if (i == 2)
{
x[0] = 1.0;
x[1] = 1.0;
res += "1, 1 = ";
}
if (i == 3)
{
x[0] = 0;
x[1] = 0;
res += "0, 0 = ";
}
for (k = 0; k < 2; k++)
{
for (l = 0; l < 8; l++)
{
h[l] += x[k] * w1[k][l];
}
}
for (l = 0; l < 8; l++)
{
h[l] += b1[l];
h[l] = tanh(h[l]);
}
for (k = 0; k < 8; k++)
{
for (l = 0; l < 1; l++)
{
y[l] += h[k] * w2[k][l];
}
}
for (k = 0; k < 1; k++)
{
y[k] += b2[k];
y[k] = sigmoid(y[k]);
}
res += y[0].ToString() + "\r\n";
}
MessageBox.Show(res);
}
double tanh(double x) {
double a = Math.Exp(x);
double b = Math.Exp(-x);
return (a - b) / (a + b);
}
double sigmoid(double x) {
return 1.0 / (1.0 + Math.Exp(-x));
}
[STAThread]
public static void Main() {
Application.Run(new form1());
}
}
以上。
Author And Source
この問題について(cscの作法 その32 ニューラルネットワーク), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/cc75361c703c4d1f5c93著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .