Python神经网络返回未对齐的形状(2,1)和(2,1)

时间:2018-11-29 04:32:11

标签: python neural-network

我想知道我是否可以得到一些帮助。今天,我决定编写自己的神经网络,直到我添加了保存并加载后,一切都进行得很顺利。下面的代码是整个网络类。之所以要开发此功能,是因为我想将神经网络的使用添加到我自己的bot中,并使用具有转弯功能的文本到语音引擎(在将来)。     将numpy导入为np     从Arrays.arrays导入Arrays     导入系统     导入操作系统

class NeuralNetwork:

def __init__(self):
    #Creating a random number seed
    np.random.seed(1)

    #converting weights to a 3 by 1 matrix
    self._weights = 2 * np.random.random((2,1)) - 1


def __sigmoid(self, _input, derivative=False):
    #Applying a sigmoid to the neural network
    if(derivative):
        return _input * (1 - _input)
    return 1/(1+np.exp(-_input))

def train(self, _inputs, _outputs, _iterations,use_stored, store=False, path=None):
    _input_array = Arrays([])
    if(use_stored):
        if(os.path.exists(path)):
            x = _inputs
            data = Arrays(x)
            result = np.load(path)
            self.__think(result)
    _count = 0
    for iteration in range(_iterations):
        _count += 1
        #Grab the data via neuron
        output = self.__think(_inputs)

        #Grab the errors for back-propogation
        error = _outputs - output

        #Adjust the weights
        adjusted = np.dot(_inputs.T, error * self.__sigmoid(output, derivative=True))

        self._weights += adjusted
        if(_count >= _iterations):
            if(store):
                if(path is None or path == None):
                    raise Exception("Storage path cannot be None or empty")
                print("Storing outputs for later input")
                np.save(path, output)

def __think(self, _inputs):
    #passing the data to the neuron to get an output
    _inputs = _inputs.astype(float)
    _output = self.__sigmoid(np.dot(_inputs, self._weights))
    return _output

def different_input(self, inputs):
    return self.__think(inputs)


print(str(np.load("network.npy")))

neural_net = NeuralNetwork()
print("Randomly generating weights")

_inputs = np.array([[0,1], [3,0]])
_outputs = np.array([[1]]).T
neural_net.train(_inputs, _outputs, 600000, True, store=True, path="network.npy")


print("Weights after training")
print(neural_net._weights)

错误:      形状(2,1)和(2,1)不对齐

0 个答案:

没有答案