Visualisasi output space MLP Neural Network

Iseng-iseng saya coba membuat program sederhana memvisualisasikan output space multilayer perceptron.

Source code nya adalah sbb.

1) File param.dat

-5    5    weight dari hidden neuron ke-1 dengan input neuron ke-1 dan ke-2
-5    5    weight dari hidden neuron ke-2 dengan input neuron ke-1 dan ke-2
-5    5    weight dari output neuron ke-1 dengan hidden neuron ke-1 dan ke-2
-2.5    2.5    bias dari hidden neuron ke-1 dan ke-2
-2.5        bias dari output neuron

2) File neuro.c


// Simulasi visualisasi output space dari multilayer perceptron dengan struktur 2-2-1

#include  <stdio.h>
#include  <stdlib.h>
#include  <math.h>

float weight_h1i1,weight_h1i2,weight_h2i1,weight_h2i2,weight_oh1,weight_oh2;
float bias_h1,bias_h2,bias_o;

FILE *myfopen( char *filename, char *mode)
{
FILE       *fp;

if((fp=fopen(filename,mode))==NULL)
{
fprintf(stderr,"Cannot open file %s.\n",filename);
exit(1);
}
return(fp);
}

void myfgets(void *ptr, int size, FILE *stream)
{
if(fgets(ptr,size,stream)==NULL)
{
fprintf(stderr,"Read error.\n");
exit(1);
}
}

float sigmoid(float net)
{
return 1.0/(1+exp(-net));
}

void read_param(char *filename)
{
FILE  *fpi;
char  line[100];

fpi=myfopen(filename,"r");
myfgets(line,100,fpi);
sscanf(line,"%f %f",&weight_h1i1,&weight_h1i2);
myfgets(line,100,fpi);
sscanf(line,"%f %f",&weight_h2i1,&weight_h2i2);
myfgets(line,100,fpi);
sscanf(line,"%f %f",&weight_oh1,&weight_oh2);
myfgets(line,100,fpi);
sscanf(line,"%f %f",&bias_h1,&bias_h2);
myfgets(line,100,fpi);
sscanf(line,"%f",&bias_o);
fclose(fpi);
}

main()
{
FILE *fp;
int   i,j;
unsigned char pixel,green=0;
float sum,x1,x2,h1,h2,o;

read_param("param.dat");
fp=myfopen("out.ppm","w");
fprintf(fp,"P6\n200 200\n255\n");
for(j=0;j<200;j++)
for(i=0;i<200;i++) {
x1=(float)(j-100)/20;
x2=(float)(i-100)/20;
sum=x1*weight_h1i1  + x2*weight_h1i2 - bias_h1;
h1=sigmoid(sum);
sum=x1*weight_h2i1  + x2*weight_h2i2 - bias_h2;
h2=sigmoid(sum);
sum=h1*weight_oh1+h2*weight_oh2-bias_o;
pixel=(unsigned char)(255*sigmoid(sum));
fwrite(&pixel,sizeof(unsigned char),1,fp);
fwrite(&green,sizeof(unsigned char),1,fp);
pixel=255-pixel;
fwrite(&pixel,sizeof(unsigned char),1,fp);
}
fclose(fp);
}

Cara menjalankannya adalah sbb.

  1. tempatkan param.dat dan neuro.c pada folder/direktori yang sama
  2. compile dengan cara  gcc neuro.c -o neuro -lm
  3. eksekusi dengan cara ./neuro
  4. program akan membaca nilai weight dan bias dari file param.dat, menghitung nilai output dan memvisualisasikan nilai outputnya dalam warna biru s/d merah.output.ppm adalah hasil visualisasi dimana merah merepresentasikan nilai output mendekati 1 dan warna biru merepresentasikan nilai output mendekati 0

Contoh hasil eksekusi untuk XOR problem adalah

Tentang Anto Satriyo Nugroho

My name is Anto Satriyo Nugroho. I am working as research scientist at Center for Information & Communication Technology, Agency for the Assessment & Application of Technology (PTIK-BPPT : Pusat Teknologi Informasi & Komunikasi, Badan Pengkajian dan Penerapan Teknologi). I obtained my doctoral degree (Dr.Eng) from Nagoya Institute of Technology, Japan in 2003. My office is located in Serpong, Tangerang Selatan City.Since 2015, I was appointed as Program Director of R&D activities in Intelligent Computing Laboratory (former name: Digital Signal Processing Laboratory). The activities in the laboratory are organized into three groups : (i) Natural Language Processing (ii) Multimodal biometrics Identification (iii) ICT solution for Tropical Disease. I also enjoy to teach the students, as a part time lecturer in Swiss German University Serpong & UNS Sebelas Maret Surakarta. Should you want to know further information on my academic works, please visit my professional site at http://asnugroho.net
Pos ini dipublikasikan di research. Tandai permalink.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s