ML输出爆炸到无穷大或不变

时间:2018-10-30 08:32:58

标签: python numpy classification

如上所述,我一直在尝试从头开始编写我的第一个ML程序(?)。

这是一个简单的“分类器”程序,但我似乎无法弄清楚出了什么问题。有人可以帮忙看看代码吗?

import numpy as np

class Perceptron:
    def __init__(self, input_nodes, hidden_nodes, output_nodes, bias = None):
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes
        self.bias = bias
        self.hidden_weights = self.weight_creation(self.hidden_nodes, self.input_nodes)
        self.output_weights = self.weight_creation(self.output_nodes, self.hidden_nodes)
        self.learning_Rate = 0.1


    def weight_creation(self, layer_size, previous_layer_size):
        bias_node = 1 if self.bias else 0
        return np.random.randn(layer_size, previous_layer_size + bias_node) * np.sqrt(2 / previous_layer_size)


    @staticmethod
    def activation_function(entity):
        return np.maximum(0.0 * entity, entity)


    @staticmethod
    def step(entity):
        # Basically the derivative of the ReLU function
        return np.where(entity > 0.5, 1, 0)


    def run(self, input_one, input_two, answer):
        # preparation of input_vector
        bias_node = 1 if self.bias else 0
        input_vector = np.array((input_one, input_two))
        if self.bias:
            input_vector = np.concatenate((input_vector, [self.bias]))
        input_vector = np.array(input_vector, ndmin=2).T
        layer_one_input = np.dot(self.hidden_weights, input_vector)
        layer_one_output = self.activation_function(layer_one_input)
        # calculation of output_vector
        layer_two_basic = layer_one_output
        if self.bias:
            layer_two_basic = np.concatenate((layer_one_output, [[self.bias]]))
        layer_two_input = np.dot(self.output_weights, layer_two_basic)
        output = self.activation_function(layer_two_input)
        print(output)


        # calculation and updating of errors
        '''Initial Calculation'''
        error = output - answer
        print('\n\n Error: ')
        print(error)
        layer_two_error = np.dot(self.step(layer_two_input), error)
        '''Layer One Weight Error Updating'''
        layer_one_calculation = np.dot(layer_two_error, self.output_weights)
        layer_one_calculation = np.dot(self.step(layer_one_input), layer_one_calculation)
        if self.bias:
            layer_one_calculation = layer_one_calculation.T[:-1, :]
        layer_one_calculation = np.dot(layer_one_calculation, input_vector)
        self.hidden_weights += self.learning_Rate * layer_one_calculation
        '''Layer Two Weight Error Updating'''
        self.output_weights += self.learning_Rate * np.dot(layer_two_basic, layer_two_error).T
        print(layer_two_error)
        print(self.hidden_weights)


percy = Perceptron(2, 3, 1, bias=1)


while True:
    input_one = int(input('First Number: '))
    input_two = int(input('Second Number: '))
    answer = int(input('Answer: '))
    percy.run(input_one, input_two, answer)

我正在使用ReLU激活功能以及He等人的初始化方法。

我遵循了一些有关权重更新的解释,但是目前我面临的问题包括:

  1. 权重未更新

  2. 输出爆炸到inf

以下是我研究的有关权重更新的资料:

https://datascience.stackexchange.com/questions/19272/deep-neural-network-backpropogation-with-relu?newreg=2d400fb705ec404496984d2d3d74c431

https://www.python-course.eu/neural_networks_backpropagation.php

任何帮助将不胜感激!

0 个答案:

没有答案