tflearn to_categorical type error

折月煮酒 提交于 2019-12-11 16:13:04

问题


I keep getting a typeError when I try to use to_categorical from tflearn. The output error is:`

 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

This is the reproducible code that I am trying to run:

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:


Cannot reproduce your error:

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.]])

System configuration:

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

UPDATE: It seems that some recent TFLearn commit has broken to_categorical - see here and here. I suggest to uninstall your current version and install the latest stable one with pip install tflearn (this is actually what I have done myself above).



来源:https://stackoverflow.com/questions/47001784/tflearn-to-categorical-type-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!