keras.utils.to_categorical()-未定义名称keras [英] keras.utils.to_categorical() - name keras not defined

查看:982
本文介绍了keras.utils.to_categorical()-未定义名称keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Keras网站上运行测试脚本Perceptron(MLP)用于多类softmax分类.在jupyter笔记本中运行时,出现错误名称'keras'未定义".这可能是我不喜欢的简单python语法问题,但是此代码直接来自keras,因此我希望它应该可以正常工作.我已经使用keras运行了其他神经网络,因此我很确定我已经安装了所有内容(使用anaconda安装了keras).有人可以帮忙吗?我在底部同时包含了代码和错误.谢谢!

I am running the test script from the Keras website for Multilayer Perceptron (MLP) for multi-class softmax classification. Running in the jupyter notebook I get the error "name 'keras' is not defined". This may be a simple python syntax problem that I am not keen to, however this code comes straight from keras so I expect it should work as is. I have run other neural nets using keras, so I am pretty sure that I have installed everything (installed keras using anaconda). Can anyone help? I have included both the code and the error at the bottom. Thanks!

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)

这是错误消息:

NameError                                 Traceback (most recent call last)
<ipython-input-1-6d8174e3cf2a> in <module>()
      6 import numpy as np
      7 x_train = np.random.random((1000, 20))
----> 8 y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
      9 x_test = np.random.random((100, 20))
     10 y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

NameError: name 'keras' is not defined

推荐答案

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

从上面,您仅导入了keras

  • keras.models
  • keras.layers
  • keras.optimizers
  • keras.models
  • keras.layers
  • keras.optimizers

但这不会自动导入外部模块,例如keras 或其他子模块keras.utils

But this does not automatically import the outer module like keras or other submodules keras.utils

因此,您可以任一个

import keras
import keras.utils
from keras import utils as np_utils

但是from keras import utils as np_utils是使用最广泛的.

尤其是import keras并不是一个好习惯,因为导入高级模块并不一定会导入其子模块(尽管它在Keras中有效)

Especially import keras is not a good practice because importing the higher module does not necessarily import its submodules (though it works in Keras)

例如,

import urllib 不一定要导入urllib.request,因为如果有这么大的子模块,那么每次导入其所有子模块的效率都会很低.

import urllib does not necessarily import urllib.request because if there are so many big submodules, it's inefficient to import all of its submodules every time.

随着Tensorflow 2的引入,现在应将keras子模块(例如keras.utils)导入为

With the introduction of Tensorflow 2, keras submodules such as keras.utils should now be imported as

from tensorflow.keras import utils as np_utils

这篇关于keras.utils.to_categorical()-未定义名称keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆