tflearn to_categorical 类型错误 [英] tflearn to_categorical type error

查看:37
本文介绍了tflearn to_categorical 类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 tflearn 中的 to_categorical 时,我不断收到 typeError.输出错误为:`

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)

推荐答案

无法重现您的错误:

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 - 请参阅 此处此处.我建议卸载您当前的版本并使用 pip install tflearn 安装最新的 stable 版本(这实际上是我自己在上面所做的).

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

这篇关于tflearn to_categorical 类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆