#include<iostream> #include<stdio.h> #include<math.h> using namespace std; double sigmoid(int sum,int theta)/*シグモイド関数*/ { double sig; sig=1/(1+exp(theta-sum)); return sig; } int main() { int x1=1; int x2=0; int x3=1; int sum; int cnt=0; int theta=1; //double gain=1.0; srand( (unsigned int)time( NULL ) ); sum=x1*3+x2*2+x3*(-1); for(int i=0;i<1000;i++){ if((double)rand()/(double)RAND_MAX<=sigmoid(sum,theta)){ cnt++; }else{ } } cout<<"実験値:"<<cnt<<endl; cout<<"理論値:"<<1000*sigmoid(sum,theta)<<endl; return 0; }
ニューラル・ネットワークについての質問です。
以下のようなモデルのニューラル・ネットワークについてプログラムをしたのですが、回答と答えが合いません。
gain:1.0、繰り返し回数:1000回
---結果---
実験値:730 理論値:731.059
このコードでは回答と結果が違ってきます。
シグモイド関数内のthetaとsumを入れ替えると結果が合うのですが、シグモイド関数自体が1/1+exp(-αz)であるため、符号はこのままで良いと思うのですが。
符号を変えると回答に近い結果が出ます。
わかる方がいらっしゃればよろしくお願いいたします。
#include <stdio.h> #include <math.h> #include <stdlib.h> /* メインプログラム */ main() { int count_1, cycle, Num_of_cycle, Output, ideal, unit_index; double Input[3], weight[3], theta, gain, net_input; int probablistic_unit( double* weight, double theta, double* Input, double gain ); double sigmoid( double ney_input, double gain ); /* ゲインを1に設定する。 */ gain = 1.0; /* 繰り返し回数を設定する。 */ Num_of_cycle = 1000; /* 重みと閾値を設定する。 */ weight[0] = 3; weight[1] = 2; weight[2] = -1; theta = 1.0; /* 乱数の種を設定する。 */ srand( 0 ); /* 素子に (1, 0, 1) を入力する時 */ Input[0] = 1; Input[1] = 0; Input[2] = 1; /* 理論値 */ net_input = 0; for ( unit_index = 0; unit_index < 3; unit_index++ ) { net_input = net_input + weight[unit_index] * Input[unit_index]; } net_input = net_input - theta; ideal = (int) (Num_of_cycle * sigmoid( net_input, gain )); /* 実験値 */ count_1 = 0; for ( cycle = 0; cycle < Num_of_cycle; cycle++ ) { Output = probablistic_unit(weight, theta, Input, gain); if ( Output == 1) count_1 = count_1 + 1; } printf("----- 素子に (1, 0, 1) を入力する時 ----- ¥n"); printf("素子が 1 を出力する頻度(実測値): %d / %d ¥n", count_1, Num_of_cycle); printf("素子が 1 を出力する頻度(理論値): %d / %d ¥n", ideal, Num_of_cycle); /* 素子に (1, -1, 0) を入力する時 */ Input[0] = 1; Input[1] = -1; Input[2] = 0; /* 理論値 */ net_input = 0; for ( unit_index = 0; unit_index < 3; unit_index++ ) { net_input = net_input + weight[unit_index] * Input[unit_index]; } net_input = net_input - theta; ideal = (int) (Num_of_cycle * sigmoid( net_input, gain )); /* 実験値 */ count_1 = 0; for ( cycle = 0; cycle < Num_of_cycle; cycle++ ) { Output = probablistic_unit(weight, theta, Input, gain); if ( Output == 1) count_1 = count_1 + 1; } printf("¥n"); printf("----- 素子に (1, -1, 0) を入力する時 ----- ¥n"); printf("素子が 1 を出力する頻度(実測値): %d / %d ¥n", count_1, Num_of_cycle); printf("素子が 1 を出力する頻度(理論値): %d / %d ¥n", ideal, Num_of_cycle); } /* 素子動作を計算する関数 */ int probablistic_unit( double* weight, double theta, double* Input, double gain ) { int unit_index; double net_input; net_input = 0; for ( unit_index = 0; unit_index < 3; unit_index++ ) { net_input = net_input + weight[unit_index] * Input[unit_index]; } net_input = net_input - theta; if ( rand() < sigmoid( net_input, gain ) * RAND_MAX ) return 1; else return 0; } /* シグモイド関数 */ double sigmoid( double z, double gain ) { return 1.0 / (1.0 + exp( gain * z )); } ---------------------------------- プログラムの実行結果 --------------------------------- ----- 素子に (1, 0, 1) を入力する時 ----- 素子が 1 を出力する頻度(実測値): 237 / 1000 素子が 1 を出力する頻度(理論値): 268 / 1000
上のプログラムが回答となります。
回答5件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/01 05:07