简体   繁体   English

用于实现卷积神经网络的 Keras

[英]Keras for implement convolution neural network

I have just install tensorflow and keras.我刚刚安装了 tensorflow 和 keras。 And I have the simple demo as follow:我有一个简单的演示如下:

from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, nb_epoch=10, batch_size=10)
# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

And I have this warning:我有这个警告:

/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py:86: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(12, activation="relu", kernel_initializer="uniform", input_dim=8)` '` call to the Keras 2 API: ' + signature)
/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py:86: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(8, activation="relu", kernel_initializer="uniform")` '` call to the Keras 2 API: ' + signature)
/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py:86: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(1, activation="sigmoid", kernel_initializer="uniform")` '` call to the Keras 2 API: ' + signature)
/usr/local/lib/python2.7/dist-packages/keras/models.py:826: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`. warnings.warn('The `nb_epoch` argument in `fit` '

So, How can I handle this?那么,我该如何处理呢?

As Matias says in the comments, this is pretty straightforward... Keras updated their API yesterday to 2.0 version.正如 Matias 在评论中所说,这非常简单...... Keras 昨天将他们的 API 更新为 2.0 版本。 Obviously you have downloaded that version and the demo still uses the "old" API.显然你已经下载了那个版本并且演示仍然使用“旧的”API。 They have created warnings so that the "old" API would still work in the version 2.0, but saying that it will change so please use 2.0 API from now on.他们创建了警告,以便“旧”API 仍然可以在 2.0 版中使用,但说它会发生变化,所以请从现在开始使用 2.0 API。

The way to adapt your code to API 2.0 is to change the "init" parameter to "kernel_initializer" for all of the Dense() layers as well as the "nb_epoch" to "epochs" in the fit() function.使您的代码适应 API 2.0 的方法是将所有Dense()层的“init”参数更改为“kernel_initializer”,并将fit()函数中的“nb_epoch”更改为“epochs”。

from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer ='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer ='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer ='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=10, batch_size=10)
# evaluate the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

This shouldn't throw any warnings, it's the keras 2.0 version of the code.这不应该抛出任何警告,这是代码的 keras 2.0 版本。

In your own case the problem was that you were using a parameter name from the older API version.在您自己的情况下,问题在于您使用的是旧 API 版本中的参数名称。 To get rid of this warning, in the compile() method, instead of using nb_epochs , you should use epochs .为了摆脱这一警告的,在compile()方法,而不是使用nb_epochs ,你应该使用epochs Now, the warning message should disappear.现在,警告消息应该消失。 The warning message describes the issue literally.警告消息从字面上描述了该问题。

The new API from Keras will often prompt you about that automatically, as they're introducing more and more changes with every new update. Keras 的新 API 通常会自动提示您,因为每次更新都会引入越来越多的更改。 However, this warning has no effect or whatsoever on the performance of the model.但是,此警告对模型的性能没有影响或没有任何影响。

而不是使用init使用kernel_initializer ,你应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM