MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# how to initialize the neural network to a set of weights ???

Asked by Mariem Harmassi on 16 Oct 2012

I created my NN with patternet ??

## Products

Answer by Greg Heath on 20 Oct 2012

Unlike the older nets (e.g., newfit, newpr, newff,...), you cannot assign weights to the newer networks (e.g., fitnet, patternnet, feedforwardnet,...) unless the net is configured.

There are two ways to configure the net before manually assigning your own initial weights. Both will assign initial weights that you can overwrite:

1. help/doc configure.

net = configure(net, x, t );

2. Train the net for 1 epoch

net.trainParam.epochs= 1.

net = train(net,x,t);

Hope this helps.

Thank you for formally accepting my answer.

Greg

## 1 Comment

Mariem Harmassi on 20 Oct 2012

ok i will try to cinfigure the net before training cauz the second solution is not a good one i need to train the net according to a specifical set of weignts .

Answer by renz on 19 Oct 2012
```net.IW{1} = %input weights
net.LW{2} = %layer weights
% biases:
net.b{1}  =
net.b{2}  =
```

Answer by Greg Heath on 19 Oct 2012
Edited by Greg Heath on 20 Oct 2012

net = patternet;

will default to H = 10 hidden nodes. For other values use

net = patternnet(H);

If

size(input) = [I N ]

size(target) = [O N ]

the node topology is I-H-O.

For a manual weight initialization, first configure the net:

net = configure(net,x,t);

For a random weight initialization, initialize the random number generator. Then generate and assign the weights:

rng(0)

IW = 0.01*randn(H,I);

b1 = 0.01*randn(H,1);

LW = 0.01*randn(O,H);

b2 = 0.01*randn(O,1);

then

net.IW{1,1} = IW;

net.b{1,1} = b1;

net.LW{2,1} = LW;

net.b{2,1} = b2;

Hope this helps.

Thank you for formally accepting my answer.

Greg

Mariem Harmassi on 20 Oct 2012

i tried to initialize the IW but matlag genrate error because the net.IW is an empty matrix and the same for the second layer net.LW {2,1} net=patternnet(10); net.IW

ans =

```    [10x0 double]
[]```

>> net.LW

ans =

```               []    []
[0x10 double]    []```

suppose taht i have 4 input nodes i tried this : net.IW{1}=rand(10,4); Error using network/subsasgn>network_subsasgn (line 536) net.IW{1,1} must be a 10-by-0 matrix.

Error in network/subsasgn (line 13) net = network_subsasgn(net,subscripts,v,netname); which means the network does not accept initialization of IW let try whith the layer suppose that i want to set the number of output nodes to 1 : net.LW{2,1}=rand(1,10); Error using network/subsasgn>network_subsasgn (line 536) net.LW{2,1} must be a 0-by-10 matrix.

Error in network/subsasgn (line 13) net = network_subsasgn(net,subscripts,v,netname); what s the hell ?

Greg Heath on 24 Oct 2012

As I said before, you have to configure the net before manually assigning weights.

help configure

doc configure