用于实现卷积神经网络的Keras [英] Keras for implement convolution neural network

查看:64
本文介绍了用于实现卷积神经网络的Keras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了tensorflow和keras.我有一个简单的演示,如下所示:

I have just install tensorflow and 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))

我有这个警告:

/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?

推荐答案

正如Matias在评论中所说,这非常简单... Keras昨天将其API更新为2.0版本.显然,您已经下载了该版本,并且该演示仍然使用旧" API. 他们已经创建了警告,以便旧" API在2.0版中仍然可以使用,但是说它将更改,因此请从现在开始使用2.0 API.

As Matias says in the comments, this is pretty straightforward... Keras updated their API yesterday to 2.0 version. Obviously you have downloaded that version and the demo still uses the "old" 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的方法是将所有Dense()层的"init"参数更改为"kernel_initializer",以及在fit()函数中将"nb_epoch"更改为"epochs".

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.

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))

这不应引发任何警告,它是代码的keras 2.0版本.

This shouldn't throw any warnings, it's the keras 2.0 version of the code.

这篇关于用于实现卷积神经网络的Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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