NeuralNetwork.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:MLLearning 作者: buptdjd 项目源码 文件源码
def fit(self, X, y, learning_rate=0.2, epochs=10000):
        x = np.atleast_2d(X)
        # add bias to X
        temp = np.ones((x.shape[0], x.shape[1]+1))
        temp[:, 0:-1] = x
        x = temp
        y = np.array(y)
        for k in range(epochs):
            # random to select one sample
            i = np.random.randint(x.shape[0])
            a = [x[i]]

            for l in range(len(self.weights)):
                a.append(self.activation(np.dot(a[l], self.weights[l])))
            error = y[i] - a[-1]
            deltas = [error*self.activation_derivative(a[-1])]

            for l in range(len(a)-2, 0, -1):
                deltas.append(deltas[-1].dot(self.weights[l].T)*self.activation_derivative(a[l]))
            deltas.reverse()
            for i in range(len(self.weights)):
                layer = np.atleast_2d(a[i])
                delta = np.atleast_2d(deltas[i])
                self.weights[i] += learning_rate * layer.T.dot(delta)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号