【问题标题】:to_categorical() missing 1 required positional argument: 'nb_classes' - tflearnto_categorical() 缺少 1 个必需的位置参数:'nb_classes' - tflearn
【发布时间】:2018-04-04 15:41:08
【问题描述】:

我正在尝试在 jupyter 笔记本上运行来自 https://github.com/tflearn/tflearn/blob/master/examples/nlp/bidirectional_lstm.py 的示例。由于我是 Tflearn、Jupyter 和 DNN 的新手,我无法调试错误是什么以及如何解决它。错误看起来像:

`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'`

我也无法理解它是如何加载数据集的。谢谢!

【问题讨论】:

  • 看来这个提交破坏了它。 github.com/tflearn/tflearn/pull/923
  • 您觉得这个答案有用吗?如果是,请接受(回答会占用受访者宝贵的时间)-谢谢

标签: typeerror tflearn


【解决方案1】:

在最新稳定版本的TFLearn(撰写本文时为0.3.2),安装pip,参数nb_classes是必要的:

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 解决它:

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 更新您的代码可能有效,但也可能无效 - 请参阅 this question 和我的答案。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。

    tflearn 版本好像太低了,只有在较新的版本中不再需要 nb_classes 参数。

    您可以尝试更新到最新版本。 (不是来自pip install tflearn。直到现在——2017/10/25——它还不够新。) pip install git+https://github.com/tflearn/tflearn.git

    或者您可以添加额外的参数,它是一个整数,表示类的总数,这可能会根据您使用的数据集而有所不同。

    【讨论】:

      猜你喜欢
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-10-10
      • 2017-03-27
      • 2019-11-12
      • 2019-12-23
      • 2021-02-01
      相关资源
      最近更新 更多