SentimentAnalysis
SentimentAnalysis copied to clipboard
module 'keras.utils' has no attribute 'to_categorical'
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版本不同的原因,我在调试的时候也遇到这个问题,看不同版本的api解决把,是个坑...
我用的Keras==2.1.3
好像没这个问题,其他也有一些API不兼容,需要更新一下
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
就能跑了
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 I have no problem running it. What version of Keras and TF are you on? Mind sharing your environment text?
model.fit(x_train, y_train, batch_size=batch_size, epochs=n_epoch,verbose=1) ^ SyntaxError: invalid syntax 请问这里是怎么回事? 没找到哪里有语法错误
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 就能跑了
我试过了按照这种方法确实能用,感谢!
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!
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.
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 intonp_utils
in some update, so with that.to_categorical
works just fine.Hope it helps someone!
it is ok
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)
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)