神经网络代码说明 [英] neural network code explanation
问题描述
以下是博客.
input = [0 0; 0 1; 1 0; 1 1];
numIn = 4;
desired_out = [0;1;1;1];
bias = -1;
coeff = 0.7;
rand('state',sum(100*clock));
weights = -1*2.*rand(3,1);
iterations = 10;
for i = 1:iterations
out = zeros(4,1);
for j = 1:numIn
y = bias*weights(1,1)+...
input(j,1)*weights(2,1)+input(j,2)*weights(3,1);
out(j) = 1/(1+exp(-y));
delta = desired_out(j)-out(j);
weights(1,1) = weights(1,1)+coeff*bias*delta;
weights(2,1) = weights(2,1)+coeff*input(j,1)*delta;
weights(3,1) = weights(3,1)+coeff*input(j,2)*delta;
end
end
我有以下问题,
(1)这里是哪个训练数据?
(1) which one is training data here?
(2)哪一个是测试数据?
(2) which one is test data here?
(3)这里的标签是什么?
(3) which are the labels here?
推荐答案
训练数据为[0 0; 0 1; 1 0; 1 1]在另一视图中,每行是一组训练数据,如下所示:
training data is [0 0; 0 1; 1 0; 1 1] in the other view every row is one set of training data as follow
>> input
input =
0 0
0 1
1 0
1 1
目标是
desired_out =
0
1
1
1
请考虑期望_out这是您的标签 .. 训练数据(输入)中的每一行在二进制集{0,1}中都有特定的输出(标签)(因为此示例用于实现OR逻辑电路.
在matlab中,您可以使用以下功能或功能进行进一步了解:
in matlab you can use or function as below for further understanding:
>> or(0,0)
ans =
0
>> or(1,0)
ans =
1
>> or(0,1)
ans =
1
>> or(1,1)
ans =
1
请注意,您的代码没有任何训练测试,并且该代码只是试图获取感知器的权重和其他参数,但是您可以通过很少的程序在代码中添加训练测试
Note that your code has not any training test and this code just trying to get weights and other parameters of perceptron but you can add training test to your code by just little program
NumDataTest = 10 ;
test=randi( [0 , 1] , [ NumDataTest , 2]) ...
+(2*rand(NumDataTest,2)-1)/20;
因此测试数据将与以下类似
so test data will be similar to below
test =
1.0048 1.0197
0.0417 0.9864
-0.0180 1.0358
1.0052 1.0168
1.0463 0.9881
0.9787 0.0367
0.9624 -0.0239
0.0065 0.0404
1.0085 -0.0109
-0.0264 0.0429
要测试此数据,您可以通过以下代码使用自己的程序:
for test this data you can use your own program by below code:
for i=1:NumDataTest
y = bias*weights(1,1)+test(i,1)*weights(2,1)+test(i,2)*weights(3,1);
out(i) = 1/(1+exp(-y));
end
最后:
table(test(:,1),test(:,2),out,'VariableNames',{'input1' 'input2' 'output'})
输出是
input1 input2 output
_________ _________ ________
1.0048 1.0197 0.99994
0.041677 0.98637 0.97668
-0.017968 1.0358 0.97527
1.0052 1.0168 0.99994
1.0463 0.98814 0.99995
0.97875 0.036674 0.9741
0.96238 -0.023861 0.95926
0.0064527 0.040392 0.095577
1.0085 -0.010895 0.97118
-0.026367 0.042854 0.080808
代码部分:
clc
clear
input = [0 0; 0 1; 1 0; 1 1];
numIn = 4;
desired_out = [0;1;1;1];
bias = -1;
coeff = 0.7;
rand('state',sum(100*clock));
weights = -1*2.*rand(3,1);
iterations = 100;
for i = 1:iterations
out = zeros(4,1);
for j = 1:numIn
y = bias*weights(1,1)+input(j,1)*weights(2,1)+input(j,2)*weights (3,1);
out(j) = 1/(1+exp(-y));
delta = desired_out(j)-out(j);
weights(1,1) = weights(1,1)+coeff*bias*delta;
weights(2,1) = weights(2,1)+coeff*input(j,1)*delta;
weights(3,1) = weights(3,1)+coeff*input(j,2)*delta;
end
end
%% Test Section
NumDataTest = 10 ;
test=randi( [0 , 1] , [ NumDataTest , 2]) ...
+(2*rand(NumDataTest,2)-1)/20;
for i=1:NumDataTest
y = bias*weights(1,1)+test(i,1)*weights(2,1)+test(i,2)*weights(3,1);
out(i) = 1/(1+exp(-y));
end
table(test(:,1),test(:,2),out,'VariableNames',{'input1' 'input2' 'output'})
我希望这会有所帮助,如果英语不好,请为我的英语感到抱歉
I hope this helps and sorry for my English if it's bad
这篇关于神经网络代码说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!