单层神经网络
对于单层神经网络的实现,我有两个数据文件。
In:
0.832 64.643
0.818 78.843
Out:
0 0 1
0 0 1
以上是2个数据文件的格式。
对于相应输入所属的特定类,目标输出为“1”,对于其余 2 个输出,目标输出为“0”。
问题如下:
你的单层神经网络将 找到 A(3 x 2 矩阵)和 b(3 x 1 向量),Y = A*X + b,其中 Y 是 [C1, C2, C3]' 且 X 为 [x1, x2]'。
解决上述问题的方法是 神经网络,我们可以重写 方程如下: Y = A' * X' 其中 A' = [A b](3 x 3 矩阵)且 X' 为 [x1, x2, 1]'
现在您可以使用神经网络 三个输入节点(一个用于 x1、x2 和 分别为 1)和三个输出(C1、 C2、C3)。
结果是 9(因为我们有 9 3 个输入和 3 个输入之间的连接 输出)权重将等于 A' 矩阵的元素。
基本上,我试图做这样的事情,但它不起作用:
function neuralNetwork
load X_Q2.data
load T_Q2.data
x = X_Q2(:,1);
y = X_Q2(:,2);
learningrate = 0.2;
max_iteration = 50;
% initialize parameters
count = length(x);
weights = rand(1,3); % creates a 1-by-3 array with random weights
globalerror = 0;
iter = 0;
while globalerror ~= 0 && iter <= max_iteration
iter = iter + 1;
globalerror = 0;
for p = 1:count
output = calculateOutput(weights,x(p),y(p));
localerror = T_Q2(p) - output
weights(1)= weights(1) + learningrate *localerror*x(p);
weights(2)= weights(1) + learningrate *localerror*y(p);
weights(3)= weights(1) + learningrate *localerror;
globalerror = globalerror + (localerror*localerror);
end
end
我在其他文件中编写此函数并在以前的代码中调用它。
function result = calculateOutput (weights, x, y)
s = x * weights(1) + y * weights(2) + weights(3);
if s >= 0
result = 1;
else
result = -1;
end
For the implementation of single layer neural network, I have two data files.
In:
0.832 64.643
0.818 78.843
Out:
0 0 1
0 0 1
The above is the format of 2 data files.
The target output is "1" for a particular class that the corresponding input belongs to and "0" for the remaining 2 outputs.
The problem is as follows:
Your single layer neural network will
find A (3 by 2 matrix) and b (3 by 1
vector) in Y = A*X + b where Y is [C1,
C2, C3]' and X is [x1, x2]'.To solve the problem above with a
neural network, we can re-write the
equation as follow: Y = A' * X' where
A' = [A b] (3 by 3 matrix) and X' is
[x1, x2, 1]'Now you can use a neural network with
three input nodes (one for x1, x2, and
1 respectively) and three outputs (C1,
C2, C3).The resulting 9 (since we have 9
connections between 3 inputs and 3
outputs) weights will be equivalent to
elements of A' matrix.
Basicaly, I am trying to do something like this, but it is not working:
function neuralNetwork
load X_Q2.data
load T_Q2.data
x = X_Q2(:,1);
y = X_Q2(:,2);
learningrate = 0.2;
max_iteration = 50;
% initialize parameters
count = length(x);
weights = rand(1,3); % creates a 1-by-3 array with random weights
globalerror = 0;
iter = 0;
while globalerror ~= 0 && iter <= max_iteration
iter = iter + 1;
globalerror = 0;
for p = 1:count
output = calculateOutput(weights,x(p),y(p));
localerror = T_Q2(p) - output
weights(1)= weights(1) + learningrate *localerror*x(p);
weights(2)= weights(1) + learningrate *localerror*y(p);
weights(3)= weights(1) + learningrate *localerror;
globalerror = globalerror + (localerror*localerror);
end
end
I write this function in some other file and calling it in my previous code.
function result = calculateOutput (weights, x, y)
s = x * weights(1) + y * weights(2) + weights(3);
if s >= 0
result = 1;
else
result = -1;
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以发现代码中的一些问题。主要问题是目标是多类(不是binary),因此您需要使用 3 个输出节点,每个类一个(称为 1-of-N 编码),或使用具有不同 激活函数(不仅仅是二进制输出 -1/1 或 0/1)
在下面的解决方案中, 感知器 具有以下结构:
对您提供的五个样本进行训练的结果:
请注意上面示例中使用的数据仅包含 5 个样本。如果每个班级有更多的训练实例,您将获得更有意义的结果。
I can spot a few problems with the code. The main issue is that the target is multi-class (not binary), so you need to either use 3 output nodes one for each class (called 1-of-N encoding), or use a single output node with a different activation function (something capable of more than just binary output -1/1 or 0/1)
In the solution below, the perceptron has the following structure:
The results of training over the five sample you provided:
Note that the data used in the example above only contains 5 samples. You would get more meaningful results if you had more training instances in each class.