简体   繁体   English

错误的维度数:预期 1,得到 2

[英]Wrong number of dimensions: expected 1, got 2

I'm doing logistic regression on a dataset for binary classification, but I'm not able to train the model for some reasons.我正在对用于二进制分类的数据集进行逻辑回归,但由于某些原因,我无法训练 model。 The error:错误:

TypeError: Bad input argument to theano function with name "<ipython-input-41-da82a78c1e80>:4" at index 1 (0-based).  
Backtrace when that variable is created:

  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-37-e46f93a2582c>", line 2, in <module>
    y = T.ivector('y')
Wrong number of dimensions: expected 1, got 2 with shape (1096, 1).

Please can someone tell me how to fix this as I'm new to theano.请有人告诉我如何解决这个问题,因为我是 theano 的新手。

import pandas as pd
import io
from sklearn import preprocessing
test_data = pd.read_csv('bank_data_test.csv').to_numpy()
train_data= pd.read_csv('bank_data_train.csv').to_numpy()
y_train=train_data[:,:1]
y_test=test_data[:,:1]
x_train=train_data[:,1:]
x_test=test_data[:,1:]
sc=StandardScaler()
sc.fit(x_train)
x_train=sc.transform(x_train)
x_test=sc.transform(x_test)
y_train.shape
x = T.fmatrix('x')
y = T.ivector('y')
w_init=np.zeros(x_train.shape[1])
b_init=0.0
w=theano.shared(w_init)
b=theano.shared(b_init)
hypo=1.0/(1.0+T.exp(-T.dot(x,w)-b))
py_x=hypo>0.5
cost=-T.mean(y*T.log(hypo)+(1-y)*T.log(1-hypo))
w_grad=T.grad(cost,w)
b_grad=T.grad(cost,b)
train_op=theano.function(inputs=[x,y],outputs=cost,updates=[
                                                            (w,w-0.05*w_grad),
                                                            (b,b-0.05*b_grad)],
                                                             allow_input_downcast=True)
predict_op=theano.function(inputs=[x],outputs=py_x,allow_input_downcast=True)
for i in range(2000):
  train_op(x_train,y_train)

The error where it shows is: train_op(x_train,y_train)它显示的错误是: train_op(x_train,y_train)

It looks like y is a matrix instead of a vector.看起来y是矩阵而不是向量。 To solve this try:要解决此尝试:

y = T.ivector('y')[0]

instead.反而。

I don't know what is the problem in your code, but your error comes from the line我不知道您的代码有什么问题,但是您的错误来自该行

y = T.ivector('y')

and not in而不是在

train_op(x_train,y_train)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 缓冲区的维数错误(预期为 1,得到 2) - Buffer has wrong number of dimensions (expected 1, got 2) 熊猫:“ ValueError:缓冲区的维数错误(预期1,得到0) - Pandas: "ValueError: Buffer has wrong number of dimensions (expected 1, got 0) ValueError:如果在语句中,则缓冲区的维数错误(预期为1,得到2) - ValueError: Buffer has wrong number of dimensions (expected 1, got 2) on if in statement 尺寸错误:期望3,得到2个形状(119、80) - Wrong number of dimensions: expected 3, got 2 with shape (119, 80) Pandas ValueError:缓冲区的维数错误(预期为 1,得到 2) - Pandas ValueError: Buffer has wrong number of dimensions (expected 1, got 2) Python scikits - 缓冲区的维数错误(预期1,得2) - Python scikits - Buffer has wrong number of dimensions (expected 1, got 2) Cython ValueError:缓冲区的维数错误(预期2,得3) - Cython ValueError: Buffer has wrong number of dimensions (expected 2, got 3) pd.cut:缓冲区的维数错误(预期为 1,得到 2) - pd.cut: Buffer has wrong number of dimensions (expected 1, got 2) 缓冲区的维数错误(预期为 1,得到 2)。 如何拟合尺寸问题? - Buffer has wrong number of dimensions (expected 1, got 2). How to fit the dimensions problem? pandas:转换数据帧集合时,缓冲区的维数错误(预期为1,得0) - pandas: Buffer has wrong number of dimensions (expected 1, got 0) when transforming a dataframe column of sets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM