我正在使用带有 Theano 后端的 Keras,以下代码会引发标题中提到的错误。
from keras.models import Sequential
from keras.layers import Dense
import theano
import theano.tensor as T
seed = 150
np.random.seed(seed)
# Define Model
model = Sequential()
model.add(Dense(500, input_dim=6, init='uniform', activation='relu'))
model.add(Dense(6, init='uniform', activation='softmax'))
def customObjective(y_true, y_pred):
cce = T.nnet.categorical_crossentropy(y_pred, y_true)
return cce
model.compile(loss=customObjective, optimizer='adam',metrics= ["hinge"])
kf = KFold(n_splits=5)
Y = trainData['label'].as_matrix()
X = trainData[['input','feat1','feat2','feat3','feat4','feat5']].as_matrix()
for train, test in kf.split(trainData):
trainX = X[train]
print trainX.shape
trainY = Y[train]
print trainY.shape
testX = X[test]
testY = Y[test]
**model.fit(trainX, trainY, nb_epoch=25, batch_size=1)** # Error on this line
print("[INFO] evaluating on testing set...")
(loss, accuracy) = model.evaluate(testData, testLabels, batch_size=128, verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
生成的错误如下所示:
ValueError: ('Bad input argument to theano function with name "/home/krishna/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py:653" at index 0(0-based)', 'setting an array element with a sequence.')
让您了解 X 和 Y 的尺寸。
"""Every cell in X is a np.array with shape (300,) with float type"""
"""Dimension X = 1542x6x300"""
X = trainData[['input','feat1','feat2','feat3','feat4','feat5']]
for val in X.iloc[0].values:
print val.shape
> (300,)
> (300,)
> (300,)
> (300,)
> (300,)
> (300,)
""" Every cell in Y is a np.array with shape (19,) with float type, Basically a one-hot vector representing one of the 19 classes """
""" Dimension Y = 1542x6x19 """
我真的很感激任何帮助。谢谢。