在MATLAB中创建简单神经网络的困惑

时间:2018-09-30 14:13:14

标签: matlab loops machine-learning neural-network

我试图通过MATLAB创建一个简单的神经网络(参考文献https://becominghuman.ai/making-a-simple-neural-network-2ea1de81ec20,尽管作者使用JavaScript进行了编码,但我想使用MATLAB进行相同的操作)。我创建了自己的MATLAB Live Script,但是我对创建的权重向量没有更新感到困惑。我正在尝试向weights(3)元素添加0.20的学习率,以使其达到1(我正在尝试6次试验来训练网络)。我是使用MATLAB的新手,通常是使用Python编写代码,因此,如果我犯了错误/我所缺少的东西得到了善意的解释,或者哪一行代码是错误的,我将不胜感激。非常感谢!

这是我的代码:-

import React from "react";

export default ({ err }) => (
  <div style={{ color: "red", padding: 20 }}>
    <i style={{ marginRight: 5 }} className="fas fa-exclamation-circle" /> {err}
  </div>
);

编辑

以下是根据Marouen接受的答案更新的(工作代码):-

inputs = [0 1 0 0]'
weights = [0 0 0 0]'
desiredresult = 1
disp('Neural Net Result')
res_net = evaluateNeuralNetwork(inputs, weights)
disp('Error')
evaluateNeuralNetError(1, res_net);
learn(inputs, weights)
train(6, inputs, weights)



function result = evaluateNeuralNetwork(inputVector, weightVector)
    result = 0;

    for i = 1:numel(inputVector)
        result = result + (inputVector(i) * weightVector(i));
    end
end

function res = evaluateNeuralNetError(desired, actual)
    res = desired - actual
end

    function learn(inputs, weights)
    learningRate = 0.20

         weights(3) = weights(3) + learningRate
end

function neuralNetResult = train(trials, inputs, weights)
    for i = 1:trials
        neuralNetResult = evaluateNeuralNetwork(inputs,weights)
        learn(inputs, weights)
    end
end

1 个答案:

答案 0 :(得分:1)

听起来像您错过了learn函数中的循环,请仔细检查原始文章。

function learn(inputs, weights)
    learningRate = 0.20

    for i =1:length(weights)
        if(inputs(i)> 0)
            weights(i) = weights(i) + learningRate
        end
    end
end

编辑

您还必须在train函数循环中更新权重

weights=learn(inputs, weights)

并在学习功能声明中添加权重作为输出

function weights=learn(inputs, weights)

否则weights不会更新。您还可以将weights声明为全局变量。