如何为该反向传播代码添加循环?

时间:2019-07-17 16:01:14

标签: python machine-learning backpropagation

我尝试了for循环来训练算法。但这不起作用。在这里,我没有检测到使用for循环功能的问题。如果有人能帮助我,对我真的很有帮助。

 import numpy as np
    w1 = np.array([0.15, 0.2])
    w2 = np.array([0.25, 0.3])
    w3 = np.array([0.4, 0.45])
    w4 = np.array([0.5, 0.55])
    x= np.array([0.05,0.10])
    def sigmoid(x):
      return 1 / (1 + np.exp(-x))

    def deriv_sigmoid(x):
        fx = sigmoid(x)
        return fx * (1 - fx)

    def error(t,o):
      return((t-o)**2).mean()

    class Neuron(object):
        def __init__(self, weights, bias):
            self.weights = weights
            self.bias = bias

        def feedforward(self, inputs):
            total = np.dot(self.weights, inputs) + self.bias
            return sigmoid(total)

        for i in range(100):
            h1 = Neuron(w1, b1)
            h2 = Neuron(w2, b1)
            o1 = Neuron(w3, b2)
            o2 = Neuron(w4, b2)
            y = np.array([h1.feedforward(x), h2.feedforward(x)])
            output = np.array([o1.feedforward(y), o2.feedforward(y)])
            test = np.array([0.01, 0.99])
            l = error(test, output)
            print(l)




    b1 = 0.35
    b2=0.6
    lr=0.5

0 个答案:

没有答案