首页 > 解决方案 > ValueError:形状(4886,400)和(4886,400)未对齐:400(dim 1)!= 4886(dim 0)

问题描述

我正在尝试在 Python 中实现单层神经网络。我的训练数据的形状是 (4886,400)。这是我写的:

from numpy import exp, array, random, dot, tanh


# Class to create a neural
# network with single neuron
class NeuralNetwork():
    def __init__(self):
        # Using seed to make sure it'll
        # generate same weights in every run
        random.seed(1)


        self.weight_matrix = 2 * random.random((4886, 400)) - 1

    # tanh as activation function
    def tanh(self, x):
        return tanh(x)

        # derivative of tanh function.

    # Needed to calculate the gradients.
    def tanh_derivative(self, x):
        return 1.0 - tanh(x) ** 2

    # forward propagation
    def forward_propagation(self, inputs):
        return self.tanh(dot(inputs, self.weight_matrix))

        # training the neural network.

    def train(self, train_inputs, train_outputs,
              num_train_iterations):
        # Number of iterations we want to
        # perform for this set of input.
        for iteration in range(num_train_iterations):
            output = self.forward_propagation(train_inputs)

            # Calculate the error in the output.
            error = train_outputs - output

            # multiply the error by input and then
            # by gradient of tanh funtion to calculate
            # the adjustment needs to be made in weights
            adjustment = dot(train_inputs.T, error *
                             self.tanh_derivative(output))

            # Adjust the weight matrix
            self.weight_matrix += adjustment

            # Driver Code


if __name__ == "__main__":
    neural_network = NeuralNetwork()

    print('Random weights at the start of training')
    print(neural_network.weight_matrix)

    train_inputs = training_data
    train_outputs = training_scores.T

    neural_network.train(train_inputs, train_outputs, 10000)

    print('New weights after training')
    print(neural_network.weight_matrix)

    # Test the neural network with a new situation.
    print("Testing network on new examples ->")
    for i in test_data:
        print(neural_network.forward_propagation(i))

但是,这是我得到的错误:

Traceback (most recent call last):
  File "untitled1/dfgd.py", line 169, in <module>
    neural_network.train(train_inputs, train_outputs, 10000)
  File "untitled1/dfgd.py", line 143, in train
    output = self.forward_propagation(train_inputs)
  File "untitled1/dfgd.py", line 134, in forward_propagation
    return self.tanh(dot(inputs, self.weight_matrix))
  File "<__array_function__ internals>", line 6, in dot
ValueError: shapes (4886,400) and (4886,400) not aligned: 400 (dim 1) != 4886 (dim 0)

我怎样才能解决这个问题?我应该如何更改矩阵的尺寸?我应该如何确定我的权重矩阵的尺寸?

标签: pythonnumpyneural-network

解决方案


似乎有一些逻辑错误

我假设您的训练数据有 4886 行和 400 列,
假设训练分数有 4886 行和 100 列
在这种情况下,您应该一次转发一行或实例
(作为向量,即形状为 ( 400, 1) 和 (100, 1))

Weight_matrix 的形状应为

(下一层神经元个数 x 上一层神经元个数)

输出层的形状应该等于 training_scores 的形状,
假设它的形状是(100, 1)
所以你的 weight_matrix 应该是形状(100, 400)

我做了一些小的调整,

import numpy as np
np.random.seed(1)
#from numpy import exp, array, random, dot, tanh, newaxis


# Class to create a neural
# network with single neuron
class NeuralNetwork():
    def __init__(self, y_shape):
        # Using seed to make sure it'll
        # generate same weights in every run
        self.weight_matrix = 2 * np.random.random((y_shape,400)) - 1

    # tanh as activation function
    def tanh(self, x):
        return np.tanh(x)

        # derivative of tanh function.

    # Needed to calculate the gradients.
    def tanh_derivative(self, x):
        return 1.0 - np.tanh(x) ** 2

    # forward propagation
    def forward_propagation(self, inputs):
        # (100, 400) x (400, 1) -> (100, 1)
        return self.tanh(np.dot(self.weight_matrix, inputs))

        # training the neural network.

    def train(self, train_inputs, train_outputs,
              num_train_iterations):
        
        summed_error = 0
        # Number of iterations we want to
        # perform for this set of input.
        for iteration in range(num_train_iterations):
            # send one instance at a time
            for x, y in zip(train_inputs, train_outputs):
                output = self.forward_propagation(x)

                # Calculate the error in the output.
                error =  output - y

                # multiply the error by input and then
                # by gradient of tanh funtion to calculate
                # the adjustment needs to be made in weights
                derror_dw = error * self.tanh_derivative(output)
                #(100, 1) x (1, 400) -> (100, 400)
                adjustment = np.dot(derror_dw, x.T)

                # Adjust the weight matrix
                # add a learning rate
                self.weight_matrix -= 0.001 * adjustment
                
                #just to measure the error of each iteration
                summed_error += np.sum(0.5*(output - y)**2)/len(train_inputs)
            print(summed_error)
            summed_error = 0
                # Driver Code


if __name__ == "__main__":
    
    #dummy input and output
    training_data = np.random.normal(size=(4886, 400))
    training_scores = np.random.normal(size=(4886, 100))
    
    print(training_data.shape)
    neural_network = NeuralNetwork(training_scores.shape[1])

    print('Random weights at the start of training')
    print(neural_network.weight_matrix.shape)

    # reshape the X and y to (4886, 400, 1) and (100, 1)
    # i.e make them vectors
    training_data = training_data[:, :, np.newaxis]
    training_scores = training_scores[:, :, np.newaxis]

    train_inputs = training_data
    train_outputs = training_scores

    neural_network.train(train_inputs, train_outputs, 10)

推荐阅读