to_categorical() 缺少 1 个必需的位置参数:'nb_classes' - tflearn [英] to_categorical() missing 1 required positional argument: 'nb_classes' - tflearn
问题描述
我正在尝试从 https://github.com/tflearn/tflearn/blob/master/examples/nlp/bidirectional_lstm.py 在 jupyter 笔记本上.由于我是 Tflearn、Jupyter 和 DNN 的新手,我无法调试错误是什么以及如何解决它.错误如下:
I am trying to run example from https://github.com/tflearn/tflearn/blob/master/examples/nlp/bidirectional_lstm.py on jupyter notebook. Since I am new on Tflearn, Jupyter and DNN I could not debug what is the error and how to resolve it. The error look like:
`TypeError Traceback (most recent call last)
<ipython-input-1-fa67bb48a391> in <module>()
38 testX = pad_sequences(testX, maxlen=100, value=0.)
39 # Converting labels to binary vectors
---> 40 trainY = to_categorical(trainY)
41 testY = to_categorical(testY)
42
TypeError: to_categorical() missing 1 required positional argument: 'nb_classes'`
我也不明白它是如何加载数据集的.谢谢!
Also I could not understand how it is loading the dataset. Thank you!
推荐答案
在最新的 stable TFLearn 版本(撰写本文时为 0.3.2)中,安装了 pipcode>,参数
nb_classes
是必要的:
In the latest stable version of TFLearn (0.3.2 at the time of writing), installed with pip
, the argument nb_classes
is necessary:
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]
# this gives error:
trainY = to_categorical(trainY)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-4a293ba390bc> in <module>()
----> 1 trainY = to_categorical(trainY) #, nb_classes=2)
TypeError: to_categorical() takes exactly 2 arguments (1 given)
本质上,尽管措辞不同,这与您收到的错误消息相同;包括 nb_classes=2
解决它:
Essentially, this is the same error message with the one you get, despite the different wording; including nb_classes=2
resolves it:
trainY = to_categorical(y=trainY, nb_classes=2)
trainY[0:5]
# array([[ 1., 0.],
# [ 1., 0.],
# [ 1., 0.],
# [ 0., 1.],
# [ 1., 0.]])
所以,我的建议是:
- 卸载您当前的 TFLearn
- 使用
pip install tflearn
安装最新的稳定版本 - 在
to_categorical
中包含参数
nb_classes=2
当然,简单地使用 nb_classes=2
更新您的代码可能会起作用,但也可能不起作用 - 请参阅 这个问题 和我的答案.
Of course, simply updating your code with nb_classes=2
might work, but it also might not - see this question and my answer there.
这篇关于to_categorical() 缺少 1 个必需的位置参数:'nb_classes' - tflearn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!