Python 值错误:形状(100784)和(46836)未对齐:784(尺寸1)!=4(尺寸0)

Python 值错误:形状(100784)和(46836)未对齐:784(尺寸1)!=4(尺寸0),python,machine-learning,neural-network,valueerror,Python,Machine Learning,Neural Network,Valueerror,更新:我修正了错误,所以我只需要回答第二个问题 我是Python新手,在执行任务时出错。我查找了这个错误,但没有找到答案 所以,这就是我想要做的 我想建立一个能够预测数值的神经网络。 我用于该类的代码如下 # neural network class definition 类神经网络: #Step 1: initialise the neural network: number of input layers, hidden layers and output layers def __ini

更新:我修正了错误,所以我只需要回答第二个问题

我是Python新手,在执行任务时出错。我查找了这个错误,但没有找到答案

所以,这就是我想要做的

我想建立一个能够预测数值的神经网络。 我用于该类的代码如下

# neural network class definition
类神经网络:

#Step 1: initialise the neural network: number of input layers, hidden layers and output layers
def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate):
    #set number of nodes in each input, hidden, output layer
    self.inodes = inputnodes
    self.hnodes = hiddennodes
    self.onodes = outputnodes

    #link weight matrices, wih and who (weights in hidden en output layers), we are going to create matrices for the multiplication of it to get an output
    #weights inside the arrays (matrices) are w_i_j, where link is from node i to node j in the next layer
    #w11 w21
    #w12 w22 etc
    self.wih = numpy.random.normal(0.0,pow(self.inodes,-0.5),( self.hnodes, self.inodes))
    self.who = numpy.random.normal(0.0,pow(self.hnodes,-0.5),( self.onodes, self.hnodes))

    # setting the learning rate
    self.lr = learningrate

    # activation function is the sigmoid function
    self.activation_function = lambda x: scipy.special.expit(x)

    pass

#Step 2: training the neural network - adjust the weights based on the error of the network
def train(self, inputs_list, targets_list):
    #convert input lists to 2d array (matrice)
    inputs = numpy.array(inputs_list, ndmin=2).T
    targets = numpy.array(targets_list, ndmin=2).T

    #calculate signals into hidden layer
    hidden_inputs = numpy.dot(self.wih, inputs)
    #calculate signals emerging from hidden layer
    hidden_outputs = self.activation_function(hidden_inputs)

    #calculate signals into final output layer
    final_inputs = numpy.dot(self.who, hidden_outputs)
    #calculate signals emerging from final output layer
    final_outputs = self.activation_function(final_inputs)
    # output layer error is the (target-actual)
    output_errors = targets -final_outputs
    #hidden layer error is the output_errors, split by weights, recombined at hidden nodes
    hidden_errors = numpy.dot(self.who.T, output_errors)

    #update the weights for the links between the hidden and output layers
    self.who += self.lr * numpy.dot((output_errors*final_outputs * (1.0-final_outputs)),numpy.transpose(hidden_outputs))

    # update the weights for the links between the input and hidden layers
    self.wih += self.lr*numpy.dot((hidden_errors*hidden_outputs*(1.0-hidden_outputs)),numpy.transpose(inputs))

    pass

#Seap 3: giving an output- thus making the neural network perform a guess
def query(self, inputs_list):
    #convert input lists to 2d array (matrice)
    inputs = numpy.array(inputs_list, ndmin=2).T

    #calculate signals into hidden layer
    hidden_inputs = numpy.dot(self.wih, inputs)
    #calculate signals emerging from hidden layer
    hidden_outputs = self.activation_function(hidden_inputs)

    #calculate signals into final output layer
    final_inputs = numpy.dot(self.who, hidden_outputs)
    #calculate signals emerging from final output layer
    final_outputs = self.activation_function(final_inputs)

    return final_outputs
显然,我首先导入了必要的东西:

import numpy 
#scipy.special for the sigmoid function expit()
import scipy.special
然后我创建了一个神经网络的实例:

#number of input, hidden and output nodes
input_nodes = 784
hidden_nodes = 100
output_nodes = 10

#learning rate is 0.8
learning_rate = 0.8

#create instance of neural network
n = neuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)
之后,我读取包含输入和目标的excel文件

import pandas as pd
df = pd.read_excel("Desktop\\PythonTest.xlsx")
该文件如下所示:

列h、p、D、o是输入,列EOQ是神经网络应该学习的数字

所以,我首先做的是:

xcol=["h","P","D","o"]
ycol=["EOQ"]
x=df[xcol].values
y=df[ycol].values
定义x和y列。x是输入,y是目标

我现在想在这些数据上训练神经网络,我使用了这些代码行

# train the neural network
# go through all records in the training data set 
for record in df:
inputs = x
targets = y
n.train(inputs, targets)
pass
这给了我以下错误:

---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call 
  last)
  <ipython-input-23-48e0e741e8ec> in <module>()
  4     inputs = x
  5     targets = y
   ----> 6     n.train(inputs, targets)
  7     pass

  <ipython-input-13-12c121f6896b> in train(self, inputs_list, targets_list)
 31 
 32         #calculate signals into hidden layer
  ---> 33         hidden_inputs = numpy.dot(self.wih, inputs)
 34         #calculate signals emerging from hidden layer
 35         hidden_outputs = self.activation_function(hidden_inputs)

 ValueError: shapes (100,784) and (4,6836) not aligned: 784 (dim 1) != 4 
 (dim 0)
---------------------------------------------------------------------------
ValueError回溯(最近的调用
最后)
在()
4个输入=x
5个目标=y
---->6 n.列车(输入、目标)
7通
列车内(自身、输入列表、目标列表)
31
32#将信号计算到隐藏层
--->33隐藏的输入=numpy.dot(self.wih,输入)
34#计算来自隐藏层的信号
35隐藏输出=自激活功能(隐藏输入)
值错误:形状(100784)和(46836)未对齐:784(尺寸1)!=4.
(尺寸0)
所以有两个问题:

  • 代码中出了什么问题
  • 我想在文件中添加一个额外的列,其中包含训练后的神经网络的猜测。我如何做到这一点
  • 非常感谢您的预先和感谢任何反馈

    干杯


    Steven

    您已经在使用pandas了,因此您可以简单地获取所有输出,并为pandas
    df
    创建一个新列

    result = [nn.query(input) for input in df]
    df['result'] = result
    

    hidden\u inputs=numpy.dot(self.wih,inputs)
    的参数没有正确的矩阵乘法形状。为了乘以矩阵
    a*b
    a
    的第二维度必须与
    b
    的第一维度相匹配。这不会发生,
    self.wih
    具有形状
    (100784)
    ,而
    输入具有形状
    (46836)
    ,因此您得到了错误(
    784!=4
    )<代码>断言self.wih.shape[1]==inputs.shape[0]
    谢谢!但是我必须在我的代码中插入“assert self.wih.shape[1]==inputs.shape[0]”在调用
    np.dot
    之前,assert只是一个健全的检查。我不知道导致错误的问题在哪里,我只是告诉你错误到底在说什么!啊哈!谢谢我发现了错误。我说有784个输入和10个输出,但事实并非如此。现在唯一的问题是:我如何在文件中添加一列,并猜测网络?