gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:05:48 25 4
gpt4 key购买 nike

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

这是我的代码:-

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

编辑

这是根据 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 weights = learn(inputs, weights)
learningRate = 0.20

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

function neuralNetResult = train(trials, inputs, weights)
for i = 1:trials
disp('Neural Network Result')
neuralNetResult = evaluateNeuralNetwork(inputs,weights)
weights = learn(inputs, weights)
disp('Error')
evaluateNeuralNetError(1, neuralNetResult)
end
end

最佳答案

听起来您错过了 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 声明为全局变量。

关于matlab - 在 MATLAB 中创建简单神经网络时的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52578672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com