2012-06-19 148 views
2

我需要幫助糾正這段代碼,以在matlab中使用神經網絡實現XOR。但是,我無法將輸入權重從輸入層設置爲第一層。該網絡分別具有2,2和1個神經元的輸入層,隱層和輸出層。 有人可以幫助我嗎?matlab中的神經網絡:如何指定輸入權重

net=network; 
net.numInputs = 1; 
net.inputs{1}.size = 2; 
net.numLayers = 2; 
net.layers{1}.size = 2; 
net.layers{2}.size = 1; 
net.inputConnect(1) = 1; 
net.layerConnect(2, 1) = 1; 
net.outputConnect(2) = 1; 
net.targetConnect(2) = 1; 
net.layers{1}.transferFcn = 'logsig';%>> net.layers{2}.transferFcn = 'purelin'; 
net.layers{2}.transferFcn = 'logsig'; 
net.biasConnect = [ 1 ; 1]; 
net.layers{1}.initFcn = 'initwb'; 
net.layers{2}.initFcn = 'initwb'; 
net.inputWeights={1 1;1 1};%ask this. error is not explanatory. probably syntax. 
net.biases{1}={-1.5 -0.5}; 
net.biases{2}=-0.5; 
net.layerWeights{2,1}={-2 1}; 
P=[0 1 0 1;0 0 1 1]; 
T=[0 1 1 0]; 
net.initFcn = 'initlay'; 
net = init(net); 
net.adaptFcn = 'adaptwb'; 
net.inputWeights{1,1}.learnFcn = 'learnp'; 
net.biases{1}.learnFcn = 'learnp'; 
net.adaptParam.passes =3; 
net.performFcn = 'mse'; 
y = sim(net,P) 

回答

0

doc network告訴我:

如果net.inputConnect(I,J)爲1,則net.inputWeights {I,J}是定義重量以從輸入層I的結構 學家

因此,而不是設置一個單元陣列的net.inputWeights您應該設置元件net.inputWeights對於這樣的輸入和第一層節點的每個組合:

net.inputWeights{1,1} = weight11; % input1 node 1 
net.inputWeigtts{1,2} = weight12; % input1 node 2 
...