![](/img/trans.png)
[英]ValueError: shapes (784,32) and (10,784) not aligned: 32 (dim 1) != 10 (dim 0) for Neural Network
[英]ValueError: shapes (100,784) and (4,6836) not aligned: 784 (dim 1) != 4 (dim 0)
更新:我已修復該錯誤,因此只需要回答第二個問題即可!
我是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)
有兩個問題:
在此先感謝您,並感謝您的任何反饋!
干杯
史蒂芬
您已經在使用pandas,因此只需獲取所有輸出,然后為pandas df
創建新列即可。
result = [nn.query(input) for input in df]
df['result'] = result
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.