Code of a single neuron Sensor

Source: Internet
Author: User

I have been looking at Neural Network Design (Chinese version) for the past two days. I don't know much about it in some places. Therefore, you can write your own code to deepen your understanding.

By the way, paste the code. Let's take a look. C. The Code has been explained in detail.

Pay one

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace neuralnetworks
{
/// <Summary>
// Reference: Neural Network Design-Chinese Version chapter 4th
/// Fun:
/// </Summary>
Class singleperceptron
{
//
Int r, s; // R is the dimension of the input vector, and s is the dimension of the output vector. Here S = 1;
Public double [,] W; // weight matrix = int [s, R]
Public double [,] B; // offset vector B = [s, 1]
Public singleperceptron (INT inputdimension, int outputdimension)
{
R = inputdimension;
S = outputdimension;
Init ();
 
}
Private void Init ()
{
// Initialize the Sensor
W = new double [s, R];
B = new double [s, 1];
// W [0, 0] = 0.5;
// W [0, 1] =-1;
// W [0, 2] =-0.5;
// B [0, 0] = 0.5;
}
Public void train (double [,] inputs, int [] outputs)
{
// Train the sensor. For more information, see p42.
Int samplecount = inputs. getlength (0); // number of samples in the training set
Bool error = true; // The calculation result is inconsistent with the target
Double sum;
Int E;
While (error)
{
// Update W, B iteratively using the training set
Error = false;
For (Int J = 0; j <samplecount; j ++)
{
For (INT I = 0; I <s; I ++)
{
// W [I] * inputs [J] + B [I], W [I] is r-dimensional, and B [I] is 1-dimensional
// A matrix operation function will be written here for Standardization. Use the for statement directly.
Sum = 0;
For (INT h = 0; H <r; H ++)
{
Sum = sum + W [I, H] * inputs [J, H];
}
Sum = sum + B [I, 0];
// Transfer Functions

// Calculate the e value
E = outputs [J]-transmissionfun. hardlim (sum, 0 );
// Modify the weight
If (E! = 0)
{
Error = true;
For (INT h = 0; H <r; H ++)
{
W [I, H] = W [I, H] + E * inputs [J, H];

}
B [I, 0] = B [I, 0] + E;

}
//
 
}
}
}

}
Public int SIM (double [] input)
{
Double sum = 0;
For (INT I = 0; I <s; I ++)
{
For (INT h = 0; H <r; H ++)
{
Sum = sum + W [I, H] * input [H];
}
Sum = sum + B [I, 0];
}
Return transmissionfun. hardlim (sum, 0 );

}
Public void tostring ()
{
 
}
}
Static class transmissionfun
{
Static public int hardlim (Double V, double thresh)
{
If (V> = thresh)
Return 1;
Return 0;
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.