编码Perceptron时出错:ValueError:形状(124,124)和(1,10)未对齐:124(dim 1)!= 1(dim 0)

时间:2017-09-29 01:23:16

标签: python arrays pandas perceptron genfromtxt

我试图编写一个多层感知器,但是当我尝试使用numpy库中的genfromtxt函数从csv文件导入数据时,我似乎弄错了。

from numpy import genfromtxt
dfX = genfromtxt('C:/Users/m15x/Desktop/UFABC/PDPD/inputX(editado_bits).csv', delimiter=',')
dfy = genfromtxt('C:/Users/m15x/Desktop/UFABC/PDPD/inputY(editado_bits).csv', delimiter=',')

X = dfX
y = dfy

print(X)
print(y)

# Whole Class with additions:
class Neural_Network(object):
    def _init_(self):
        # Define Hyperparameters
        self.inputLayerSize = 26
        self.outputLayerSize = 1
        self.hiddenLayerSize = 10

        # Weights (parameters)
        self.W1 = np.random.randn(self.inputLayerSize, self.hiddenLayerSize)
        self.W2 = np.random.randn(self.hiddenLayerSize, self.outputLayerSize)

我的X(124,1)和y(124,26)分别是以下数组:

[[ 1.  0.  1. ...,  1.  0.  0.]
 [ 0.  1.  1. ...,  1.  0.  0.]
 [ 0.  1.  1. ...,  1.  0.  0.]
 ..., 
 [ 0.  1.  1. ...,  1.  0.  0.]
 [ 1.  0.  1. ...,  1.  0.  0.]
 [ 1.  0.  1. ...,  1.  0.  0.]]

[ 0.  0.  1.  0.  1.  0.  1.  1.  0.  0.  0.  1.  1.  0.  0.  0.  0.  0.
  0.  0.  1.  1.  0.  0.  0.  1.  0.  0.  1.  0.  0.  0.  1.  1.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  1.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  1.  0.  1.  0.  0.  1.  0.  0.  0.  0.  0.
  0.  1.  0.  1.  0.  1.  0.  0.  1.  1.  0.  0.  0.  1.  0.  1.  0.  1.
  1.  1.  0.  0.  0.  1.  0.  0.  0.  0.  0.  0.  1.  0.  1.  0.  1.  0.
  1.  0.  0.  0.  0.  1.  0.  1.  0.  1.  0.  1.  0.  0.  0.  0.]

我收到通知:

Traceback (most recent call last):
  File "C:/Users/m15x/PycharmProjects/Deep Learning/MLP_tinnitus_1.py", line 141, in <module>
    T.train(X,y)
  File "C:/Users/m15x/PycharmProjects/Deep Learning/MLP_tinnitus_1.py", line 134, in train
    args=(X, y), options=options, callback=self.callbackF)
  File "C:\Users\m15x\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 444, in minimize
    return _minimize_bfgs(fun, x0, args, jac, callback, **options)
  File "C:\Users\m15x\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 913, in _minimize_bfgs
    gfk = myfprime(x0)
  File "C:\Users\m15x\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 292, in function_wrapper
    return function(*(wrapper_args + args))
  File "C:\Users\m15x\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 71, in derivative
    self(x, *args)
  File "C:\Users\m15x\Anaconda3\lib\site-packages\scipy\optimize\optimize.py", line 63, in _call_
    fg = self.fun(x, *args)
  File "C:/Users/m15x/PycharmProjects/Deep Learning/MLP_tinnitus_1.py", line 119, in costFunctionWrapper
    grad = self.N.computeGradients(X, y)
  File "C:/Users/m15x/PycharmProjects/Deep Learning/MLP_tinnitus_1.py", line 76, in computeGradients
    dJdW1, dJdW2 = self.costFunctionPrime(X, y)
  File "C:/Users/m15x/PycharmProjects/Deep Learning/MLP_tinnitus_1.py", line 56, in costFunctionPrime
    delta2 = np.dot(delta3, self.W2.T) * self.sigmoidPrime(self.z2)
ValueError: shapes (124,124) and (1,10) not aligned: 124 (dim 1) != 1 (dim 0)

主要是这个错误在我尝试使用以前的数据训练我的代码时开始。

def train(self, X, y):
    # Make an internal variable for the callback function:
    self.X = X
    self.y = y

    # Make empty list to store costs:
    self.J = []

    params0 = self.N.getParams()

    options = {'maxiter': 10000, 'disp': True}
    _res = optimize.minimize(self.costFunctionWrapper, params0, jac=True, method='BFGS', \
                             args=(X, y), options=options, callback=self.callbackF)

    self.N.setParams(_res.x)
    self.optimizationResults = _res

我知道我的数组来自X和y不适合,但我不知道一些可用的函数,我可以应用来处理变量y的数据,这是由(124, 1)形状数据csv文件(&#39; C:/ Users / m15x / Desktop / UFABC / PDPD / inputY(editado_bits).csv&#39;)和我的X变量由(124,26)形状的csv文件提供(&#39; C:/用户/ M15X /桌面/ UFABC / PDPD / inputX(editado_bits)的.csv&#39)。 似乎我使用genfromtxt函数导入的数据似乎不合适。

0 个答案:

没有答案