Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
namespacegit opened this issue Jan 16, 2018 · 13 comments
Open

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

namespacegit opened this issue Jan 16, 2018 · 13 comments

Comments

@namespacegit
Copy link

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)

为什么会出现这个错误?

@Edward1Chou
Copy link
Owner

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

@ResolveWang
Copy link

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

@jeness
Copy link

jeness commented Apr 28, 2018

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
就能跑了

@skoundin
Copy link

skoundin commented Jul 4, 2018

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.

@DerekChia
Copy link

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

@yuhaoroy
Copy link

yuhaoroy commented May 9, 2019

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

@Edward-Joker
Copy link

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
Copy link

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

@Jhouny
Copy link

Jhouny commented May 27, 2021

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!

@rituvermaCS
Copy link

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.

@13223486896
Copy link

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

@tennisee
Copy link

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)

@sebastianTech
Copy link

sebastianTech commented Mar 10, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests