SentimentAnalysis icon indicating copy to clipboard operation
SentimentAnalysis copied to clipboard

module 'keras.utils' has no attribute 'to_categorical'

Open namespacegit opened this issue 6 years ago • 13 comments

from keras import utils as np_utils
y_train = np_utils.to_categorical(y_train,num_classes=3) y_test = np_utils.to_categorical(y_test,num_classes=3)

为什么会出现这个错误?

namespacegit avatar Jan 16 '18 03:01 namespacegit

keras版本不同的原因,我在调试的时候也遇到这个问题,看不同版本的api解决把,是个坑...

Edward1Chou avatar Jan 17 '18 07:01 Edward1Chou

我用的Keras==2.1.3好像没这个问题,其他也有一些API不兼容,需要更新一下

ResolveWang avatar Jan 18 '18 05:01 ResolveWang

Keras==2.1.5 gensim==3.4.0 jieba==0.38 我用的版本是这样, 然后增加from keras import utils as np_utils 120行附近,修改为:

 y_train = np_utils.to_categorical(y_train,num_classes=3)
 y_test = np_utils.to_categorical(y_test,num_classes=3)

107行附近,修改为:

model.train(combined,total_examples=model.corpus_count, epochs=model.iter)

72行附近,修改为:

gensim_dict.doc2bow(model.wv.vocab.keys(),
                            allow_update=True)

然后建立一个名字是lstm_data_test的directory 就能跑了

jeness avatar Apr 28 '18 18:04 jeness

Hi, I am getting the same issue, I was using cifar code from the location - https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py

Error I get is - module 'keras.utils' has no attribute 'to_categorical'.

Let me know if anyone was able to resolve this. Thanks.

skoundin avatar Jul 04 '18 04:07 skoundin

@skoundin I have no problem running it. What version of Keras and TF are you on? Mind sharing your environment text?

DerekChia avatar Jul 25 '18 04:07 DerekChia

model.fit(x_train, y_train, batch_size=batch_size, epochs=n_epoch,verbose=1) ^ SyntaxError: invalid syntax 请问这里是怎么回事? 没找到哪里有语法错误

yuhaoroy avatar May 09 '19 08:05 yuhaoroy

Keras==2.1.5 gensim==3.4.0 jieba==0.38 我用的版本是这样, 然后增加from keras import utils as np_utils 120行附近,修改为:

 y_train = np_utils.to_categorical(y_train,num_classes=3)
 y_test = np_utils.to_categorical(y_test,num_classes=3)

107行附近,修改为:

model.train(combined,total_examples=model.corpus_count, epochs=model.iter)

72行附近,修改为:

gensim_dict.doc2bow(model.wv.vocab.keys(),
                            allow_update=True)

然后建立一个名字是lstm_data_test的directory 就能跑了

Edward-Joker avatar Aug 01 '20 14:08 Edward-Joker

我试过了按照这种方法确实能用,感谢!

Edward-Joker avatar Aug 01 '20 14:08 Edward-Joker

I tried the solution @Edward-Joker posted and it didn't work for me.

I'm running Tensorflow version 2.5.0. By trial and error I found that keras.utils.np_utils works. I guess they moved it into np_utils in some update, so with that .to_categorical works just fine.

Hope it helps someone!

Jhouny avatar May 27 '21 18:05 Jhouny

TF version- 2.5.0 keras version -2.5.0

Resolving this issue, first import to_categorical()

from tensorflow.keras.utils import to_categorical

then write like

keras.utils.to_categorical()

It will work.

rituvermaCS avatar Jul 03 '21 12:07 rituvermaCS

I tried the solution @Edward-Joker posted and it didn't work for me.

I'm running Tensorflow version 2.5.0. By trial and error I found that keras.utils.np_utils works. I guess they moved it into np_utils in some update, so with that .to_categorical works just fine.

Hope it helps someone!

it is ok

13223486896 avatar Jul 09 '21 02:07 13223486896

from keras import utils as np_utils y_train = np_utils.to_categorical(y_train,num_classes=3) y_test = np_utils.to_categorical(y_test,num_classes=3)

为什么会出现这个错误?

keras version -2.6.0

from keras.utils import np_utils y_train = np_utils.to_categorical(y_train, num_classes) y_test = np_utils.to_categorical(y_test, num_classes)

tennisee avatar Oct 25 '21 10:10 tennisee

I am not sure if this was solved, however, I encountered the same error and this solved it for me.

First import from keras.utils import np_utils

Then copy the below code and you should be fine

y_train = keras.utils.np_utils.to_categorical(y_train,num_classes=3) y_test =keras.utils.np_utils.to_categorical(y_test,num_classes=3)

sebastianTech avatar Mar 10 '22 05:03 sebastianTech