nn.py 文件源码

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

项目:Machine-Learning 作者: grasses 项目源码 文件源码
def fit(self, X, y, learning_rate = 0.2, epochs = 10000):
        X = np.atleast_2d(X)
        # temp.shape=(X.shape[0], X.shape[1] + 1) `+1` is for bais, so X[*][-1] = 1 => numpy.dot(x, weights) + numpy.dot(1 * bais)
        temp = np.ones([X.shape[0], X.shape[1] + 1])
        temp[:, 0:-1] = X
        X = temp
        y = np.array(y)

        '''
        loop operation for epochs times
        '''
        for k in range(epochs):
            # select a random line from X for training
            i = np.random.randint(X.shape[0])
            x = [X[i]]

            # going forward network, for each layer
            for l in range(len(self.weights)):
                # computer the node value for each layer (O_i) using activation function
                x.append(self.activation(np.dot(x[l], self.weights[l])))

            # computer the error at the top layer
            error = y[i] - x[-1]
            deltas = [error * self.activation_deriv(x[-1])]  # For output layer, Err calculation (delta is updated error)

            # start backprobagation
            for l in range(len(x) - 2, 0, -1):  # we need to begin at the second to last layer
                # compute the updated error (i,e, deltas) for each node going from top layer to input layer
                deltas.append(deltas[-1].dot(self.weights[l].T) * self.activation_deriv(x[l]))
            deltas.reverse()

            for i in range(len(self.weights)):
                layer = np.atleast_2d(x[i])
                delta = np.atleast_2d(deltas[i])
                self.weights[i] += learning_rate * layer.T.dot(delta)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号