简体   繁体   中英

ValueError: shapes (100,784) and (4,6836) not aligned: 784 (dim 1) != 4 (dim 0)

Update: I fixed the error, so I only need an answer on the second question!

I'm fairly new in Python and got an error while performing a task. I looked for this error, but didn't find my answer on it.

So, this is what I am trying to do.

I want to build a neural network that is able to predict a value. The code I used for the class is as follows

# neural network class definition

class neuralNetwork:

#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

I obviously imported the necessary things first:

import numpy 
#scipy.special for the sigmoid function expit()
import scipy.special

I then created an instance of the neural network:

#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)

After this, I read the excel file with the inputs and the target

import pandas as pd
df = pd.read_excel("Desktop\\PythonTest.xlsx")

The file looks like this:

snapshot of file

The columns h, P, D, o are inputs and the column EOQ is the number that the neural network should learn.

So, I first did this:

xcol=["h","P","D","o"]
ycol=["EOQ"]
x=df[xcol].values
y=df[ycol].values

To define the x and y columns. x are the inputs and y is the target.

I now want to train the neural network on this data and I used these lines of code;

# 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

This gives me the following error:

---------------------------------------------------------------------------
  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)

So two questions:

  1. What is going wrong in the code?
  2. I want to add an extra column in the file with the guess of the neural network after it is trained. How do I achieve this?

Many thanks in advance and appreciate any feedback!

Cheers

Steven

You are already using pandas, so you can simply get all the output, and make a new column to pandas df .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM