tflearn KeyError: '[23566... not in index exception
tflearn KeyError: '[23566... not in index exception
我正在使用 tflearn 构建一个简单的神经网络,并将 public 成人收入数据传递给它进行训练。当我收到错误时:
Exception in thread Thread-6:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
data = self.retrieve_data(batch_ids)
File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 222, in retrieve_data
utils.slice_array(self.feed_dict[key], batch_ids)
File "/usr/local/lib/python2.7/dist-packages/tflearn/utils.py", line 187, in slice_array
return X[start]
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1958, in __getitem__
return self._getitem_array(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2002, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1231, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: '[23566 22547 27149 12793 17126 30292 18889 15170 31777 8499 24748 15401\n 7317 11624 8017 10984] not in index'
这是我的代码:
import pandas as pd
import copy
df_train = pd.read_csv('adult.data', header = None)
X_train_orig = df_train.drop(df_train.columns[[14]], axis=1, inplace=False)
Y_train_orig = df_train[[14]]
df_test = pd.read_csv('adult.test', header = None)
X_test_orig = df_test.drop(df_test.columns[[14]], axis=1, inplace=False)
Y_test_orig = df_test[[14]]
dataset = pd.concat(objs=[X_train_orig, X_test_orig], axis = 0)
data = pd.get_dummies(dataset) # get one hot encoding
X_train = copy.copy(data[:len(X_train_orig)])
X_test = copy.copy(data[len(X_train_orig):])
Y_train = pd.get_dummies(Y_train_orig) # get one hot encoding
Y_test = pd.get_dummies(Y_test_orig) # get one hot encoding
print "X train shape = " + str(X_train.shape)
print "Y train shape = " + str(Y_train.shape)
print "X test shape = " + str(X_test.shape)
print "Y test shape = " + str(Y_test.shape)
import tflearn
import tensorflow as tf
tf.reset_default_graph()
network = tflearn.input_data([None, 108])
network = tflearn.fully_connected(network, 10)
network = tflearn.fully_connected(network, 10)
network = tflearn.fully_connected(network, 2, activation='softmax')
network = tflearn.regression(network, loss='categorical_crossentropy')
model = tflearn.DNN(network)
model.fit(X_train, Y_train, show_metric=True, batch_size=16, n_epoch=5000, snapshot_epoch=False)
我已经在做:
import tensorflow as tf
tf.reset_default_graph()
根据一些相关帖子的建议。我在这里错过了什么?
我遇到了同样的问题
我找到了这个解决方案:https://github.com/fchollet/keras/issues/2262
它指出您需要将输入转换为 numpy 数组。如果你在调用 fit 时这样做:
model.fit(np.array(X_train), np.array(Y_train), show_metric=True, batch_size=16, n_epoch=5000, snapshot_epoch=False)
希望它能解决您的问题。
我正在使用 tflearn 构建一个简单的神经网络,并将 public 成人收入数据传递给它进行训练。当我收到错误时:
Exception in thread Thread-6:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 187, in fill_feed_dict_queue
data = self.retrieve_data(batch_ids)
File "/usr/local/lib/python2.7/dist-packages/tflearn/data_flow.py", line 222, in retrieve_data
utils.slice_array(self.feed_dict[key], batch_ids)
File "/usr/local/lib/python2.7/dist-packages/tflearn/utils.py", line 187, in slice_array
return X[start]
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1958, in __getitem__
return self._getitem_array(key)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2002, in _getitem_array
indexer = self.loc._convert_to_indexer(key, axis=1)
File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1231, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: '[23566 22547 27149 12793 17126 30292 18889 15170 31777 8499 24748 15401\n 7317 11624 8017 10984] not in index'
这是我的代码:
import pandas as pd
import copy
df_train = pd.read_csv('adult.data', header = None)
X_train_orig = df_train.drop(df_train.columns[[14]], axis=1, inplace=False)
Y_train_orig = df_train[[14]]
df_test = pd.read_csv('adult.test', header = None)
X_test_orig = df_test.drop(df_test.columns[[14]], axis=1, inplace=False)
Y_test_orig = df_test[[14]]
dataset = pd.concat(objs=[X_train_orig, X_test_orig], axis = 0)
data = pd.get_dummies(dataset) # get one hot encoding
X_train = copy.copy(data[:len(X_train_orig)])
X_test = copy.copy(data[len(X_train_orig):])
Y_train = pd.get_dummies(Y_train_orig) # get one hot encoding
Y_test = pd.get_dummies(Y_test_orig) # get one hot encoding
print "X train shape = " + str(X_train.shape)
print "Y train shape = " + str(Y_train.shape)
print "X test shape = " + str(X_test.shape)
print "Y test shape = " + str(Y_test.shape)
import tflearn
import tensorflow as tf
tf.reset_default_graph()
network = tflearn.input_data([None, 108])
network = tflearn.fully_connected(network, 10)
network = tflearn.fully_connected(network, 10)
network = tflearn.fully_connected(network, 2, activation='softmax')
network = tflearn.regression(network, loss='categorical_crossentropy')
model = tflearn.DNN(network)
model.fit(X_train, Y_train, show_metric=True, batch_size=16, n_epoch=5000, snapshot_epoch=False)
我已经在做:
import tensorflow as tf
tf.reset_default_graph()
根据一些相关帖子的建议。我在这里错过了什么?
我遇到了同样的问题
我找到了这个解决方案:https://github.com/fchollet/keras/issues/2262
它指出您需要将输入转换为 numpy 数组。如果你在调用 fit 时这样做:
model.fit(np.array(X_train), np.array(Y_train), show_metric=True, batch_size=16, n_epoch=5000, snapshot_epoch=False)
希望它能解决您的问题。