Thread Subject: neural network implementation

Subject: neural network implementation

From: recep mansiz

Date: 8 Jan, 2012 13:58:09

Message: 1 of 6

Hi all!!

I have a problem on implementing a neural network with using addition and multiplication.

I'm trying to implement the network below,

p = [1 3];
t = [2 4];
net = newff(p,t,1);
net = train(net, p,t);
______________________________
after running the code above, I fetch the weights and biases with

input_weights = net.IW;
layer_weights = net.LW;
biases = net.b;

write a function " try_neural" with resulting weights

function [net_out] = try_neural(in_1)
w_1_1 = -1.47057806618821; b_1 = 0.0252873414230115;
L1_out = tansig(in_1*w_1_1 + b_1);
wo_1 = -1.11163638238458; b_o = 0.00535792495355342;
net_out = L1_out*wo_1 + b_o;

then run the line : result = try_neural (1);

I'm getting different results with
result = sim(net,1);

Most probably I'm missing something very basic and simple but I can't figure out the source of error. (I'm using MATLAB 2010b)

Thanks in advance!!

Subject: neural network implementation

From: Greg Heath

Date: 8 Jan, 2012 21:43:21

Message: 2 of 6

On Jan 8, 5:58 am, "recep mansiz" <aalmo...@hotmail.com> wrote:
> Hi all!!
>
> I have a problem on implementing a neural network with using addition and multiplication.
>
> I'm trying to implement the network below,
>
> p = [1 3];
> t = [2 4];
> net = newff(p,t,1);
> net = train(net, p,t);
> ______________________________
> after running the code above, I fetch the weights and biases with
>
> input_weights = net.IW;
> layer_weights = net.LW;
> biases = net.b;
>
> write a function " try_neural" with resulting weights
>
> function [net_out] = try_neural(in_1)
> w_1_1 = -1.47057806618821;  b_1 = 0.0252873414230115;
> L1_out = tansig(in_1*w_1_1 + b_1);
> wo_1 = -1.11163638238458; b_o = 0.00535792495355342;
> net_out = L1_out*wo_1 + b_o;
>
> then run the line :   result = try_neural (1);
>
> I'm getting different results with
> result = sim(net,1);
>
> Most probably I'm missing something very basic and simple but I can't figure out the source of error. (I'm using MATLAB 2010b)
>
> Thanks in advance!!

Have you accounted for the automatic defaults (e.g., normalization)
used in NEWFF?

Hope this helps.

Greg.

Subject: neural network implementation

From: recep mansiz

Date: 8 Jan, 2012 22:59:09

Message: 3 of 6

> Have you accounted for the automatic defaults (e.g., normalization)
> used in NEWFF?
>
> Hope this helps.
>
> Greg.

No, I haven't accounted any other issue. Do you mean that each time a new input is presented, there is one more operation that should be applied to the input before directly applying it to the network?
If so, what are the additional procedures to carry on in order to get the same results with original network or how can I learn to deal with it ?

Because I thought that if I have the resultant weights and bias, all I have to do is to add and multiply the new inputs with them.

(In fact I'm going to try to implement the resultant neural network with a microcontroller.)

Many thanks...

Subject: neural network implementation

From: Greg Heath

Date: 9 Jan, 2012 01:57:54

Message: 4 of 6

On Jan 8, 5:59 pm, "recep mansiz" <aalmo...@hotmail.com> wrote:
> > Have you accounted for the automatic defaults (e.g., normalization)
> > used in NEWFF?
>
> > Hope this helps.
>
> > Greg.
>
> No, I haven't accounted any other issue. Do you mean that each time a new input is presented, there is one more operation that should be applied to the input before directly applying it to the network?

Yes.

> If so, what are the additional procedures to carry on in order to get the same results with original network or how can I learn to deal with it ?
>
> Because I thought that if I have the resultant weights and bias, all I have to do is to add and multiply the new inputs with them.
>
> (In fact I'm going to try to implement the resultant neural network with a microcontroller.)
>
> Many thanks...

Read the documentation (I'm traveling without access to MATLAB and
cannot give you details).

However, if you read the documentation, e. g.,

help newff
doc newff

help trainlm
doc trainlm

you can find the defaults. For example, newff automatically normalizes
the inputs (and outputs?).
Also, I think there may be an automatic random split into trn/val/tst
sets.

Hope this helps.

Greg

Subject: neural network implementation

From: Steven_Lord

Date: 9 Jan, 2012 02:44:17

Message: 5 of 6



"recep mansiz" <aalmoraa@hotmail.com> wrote in message
news:jed73s$hmt$1@newscl01ah.mathworks.com...
>> Have you accounted for the automatic defaults (e.g., normalization)
>> used in NEWFF?
>>
>> Hope this helps.
>>
>> Greg.
>
> No, I haven't accounted any other issue. Do you mean that each time a new
> input is presented, there is one more operation that should be applied to
> the input before directly applying it to the network? If so, what are the
> additional procedures to carry on in order to get the same results with
> original network or how can I learn to deal with it ?

http://www.mathworks.com/help/toolbox/nnet/ug/bss324a-1.html#bss324a-5

> Because I thought that if I have the resultant weights and bias, all I
> have to do is to add and multiply the new inputs with them.
> (In fact I'm going to try to implement the resultant neural network with a
> microcontroller.)

If you have Simulink and the necessary tools to target your microcontroller,
use GENSIM to generate a Simulink model directly from your network and let
Simulink generate the code for the controller.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: neural network implementation

From: recep mansiz

Date: 9 Jan, 2012 11:18:08

Message: 6 of 6

> you can find the defaults. For example, newff automatically normalizes
> the inputs (and outputs?).
(Greg)

> http://www.mathworks.com/help/toolbox/nnet/ug/bss324a-1.html#bss324a-5
(Steven_Lord)

After these advices; instead of directly applying inputs to the network
(result = try_neural (1))

I tried

max_in = max(p); min_in = min(p);
max_out = max(t); min_out = min(t);

in_1 = map_input (in_1,max_in,min_in,1,-1);

out_1 = try_neural (in_1);

out_1 = map_input (out_1,1,-1,max_out,min_out);

where; (the function below is the same as mapminmax algorithm)

function [mapped_data] = map_input(in_1,max_in,min_in,max_out,min_out)

mapped_data = (max_out-min_out)*(in_1-min_in)/(max_in-min_in) + min_out;

Thus, first input data is normalized with max and min of input, then normalized data is procesed with weights and biases and finally the output is reversely normalized with max and min of output.

The result is the same as sim(net,p). Many thanks to Greg and Steven, your advices worked perfectly... Next, I may try to apply the Simulink method.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
neural networks recep mansiz 8 Jan, 2012 08:59:12
rssFeed for this Thread

Contact us at files@mathworks.com