tflearn to_categorical类型错误

时间:2017-10-29 15:11:51

标签: typeerror tflearn

当我尝试使用typeError中的to_categorical时,我一直收到tflearn。输出错误是:`

 
 trainY = to_categorical(y = trainY, nb_classes=2)
  File "C:\Users\saleh\Anaconda3\lib\site-packages\tflearn\data_utils.py", line 46, in to_categorical
    return (y[:, None] == np.unique(y)).astype(np.float32)
TypeError: list indices must be integers or slices, not tuple

这是我正在尝试运行的可重现代码:

import tflearn
from tflearn.data_utils import to_categorical
from tflearn.datasets import imdb

#IMDB dataset loading
train, test, _ = imdb.load_data(path = 'imdb.pkl', n_words = 10000, valid_portion = 0.1)
trainX, trainY = train
testX, testY = test

#converting labels to binary vectors
trainY = to_categorical(y = trainY, nb_classes=2)  # **This is where I get the error**
testY = to_categorical(y = testY, nb_classes=2)

1 个答案:

答案 0 :(得分:1)

无法重现您的错误:

 
import tflearn
from tflearn.data_utils import to_categorical
from tflearn.datasets import imdb

train, test, _ = imdb.load_data(path = 'imdb.pkl', n_words = 10000, valid_portion = 0.1)
trainX, trainY = train
testX, testY = test

trainY[0:5]
# [0, 0, 0, 1, 0]

trainY = to_categorical(y = trainY, nb_classes=2) 
trainY[0:5]
# array([[ 1.,  0.],
#        [ 1.,  0.],
#        [ 1.,  0.],
#        [ 0.,  1.],
#        [ 1.,  0.]])

系统配置:

  • Python 2.7.12
  • Tensorflow 1.3.0
  • TFLearn 0.3.2
  • Ubuntu 16.04

更新:似乎最近的一些TFLearn提交已经中断to_categorical - 请参阅herehere。我建议卸载您当前的版本,并使用pip install tflearn安装最新的稳定一个(这实际上就是我上面所做的)。