首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:形状( 4886 , 400 )和(4886,400)未对齐:400(尺寸1) != 4886(尺寸0)

ValueError:形状( 4886 , 400 )和(4886,400)未对齐:400(尺寸1) != 4886(尺寸0)
EN

Stack Overflow用户
提问于 2021-03-09 18:23:58
回答 1查看 31关注 0票数 0

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

代码语言:javascript
复制
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))

然而,这是我得到的错误:

代码语言:javascript
复制
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)

我能解决这个问题吗?我应该如何改变我的矩阵的尺寸?我应该如何确定权重矩阵的维度?

EN

回答 1

Stack Overflow用户

发布于 2021-03-09 22:57:07

似乎有一些逻辑错误

我假设你的训练数据有4886行400列,

假设训练分数为4886行和100列

在这种情况下,您应该一次转发一行或实例

(作为矢量,即形状为(400,1)和(100,1))

Weight_matrix的形状应该是

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

输出层的形状应等于training_scores的形状。

让我们假设它有一个(100, 1)形状

所以你的weight_matrix应该是(100, 400)的形状

我做了一些小调整,

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66545030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档