我是 Keras 的新手,我试图在数据集上做二进制 MLP,并且不断让索引超出界限,不知道为什么。
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
model = Sequential()
model.add(Dense(64, input_dim=20, init='uniform', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop')
model.fit(trainx, trainy, nb_epoch=20, batch_size=16) # THROWS INDICES ERROR
错误:
model.fit(trainx, trainy, nb_epoch=20, batch_size=16)
Epoch 1/20
Traceback (most recent call last):
File "<ipython-input-6-c81bd7606eb0>", line 1, in <module>
model.fit(trainx, trainy, nb_epoch=20, batch_size=16)
File "C:UsersThiruAnaconda3libsite-packageskerasmodels.py", line 646, in fit
shuffle=shuffle, metrics=metrics)
File "C:UsersThiruAnaconda3libsite-packageskerasmodels.py", line 271, in _fit
ins_batch = slice_X(ins, batch_ids)
File "C:UsersThiruAnaconda3libsite-packageskerasmodels.py", line 65, in slice_X
return [x[start] for x in X]
File "C:UsersThiruAnaconda3libsite-packageskerasmodels.py", line 65, in <listcomp>
return [x[start] for x in X]
File "C:UsersThiruAnaconda3libsite-packagespandascoreframe.py", line 1963, in __getitem__
return self._getitem_array(key)
File "C:UsersThiruAnaconda3libsite-packagespandascoreframe.py", line 2008, in _getitem_array
return self.take(indexer, axis=1, convert=True)
File "C:UsersThiruAnaconda3libsite-packagespandascoregeneric.py", line 1371, in take
convert=True, verify=True)
File "C:UsersThiruAnaconda3libsite-packagespandascoreinternals.py", line 3619, in take
indexer = maybe_convert_indices(indexer, n)
File "C:UsersThiruAnaconda3libsite-packagespandascoreindexing.py", line 1750, in maybe_convert_indices
raise IndexError("indices are out-of-bounds")
IndexError: indices are out-of-bounds
有谁知道为什么会这样?我能够很好地运行其他模型
评论中的答案 - trainx 和 trainy 应该是 numpy 数组。您可以使用as_matrix()
方法将数据框转换为 numpy 数组。我也面临这个问题。奇怪的是,Keras没有给出有意义的错误消息。
我来到这里为auto-sklearn和pandas数据帧寻找相同的问题解决方案。解决方案是将 X 数据帧作为 X.values 传递。即拟合(X.值,y)
来自官方 Keras 页面:
Keras 模型在输入数据和标签的 Numpy 数组上进行训练。对于训练模型,通常使用拟合函数。
要将熊猫数据帧转换为 numpy 数组,您可以使用 np.array(dataframe)
.例如:
x_train = np.array(x_train)