Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras for implement convolution neural network

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

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?

like image 901
Khang Truong Avatar asked Mar 15 '17 16:03

Khang Truong


People also ask

Does Keras support convolutional neural network?

Convolutional Neural Network in Keras is popular for image processing, image recognition, etc. It is very influential in the field of computer vision.

How do you implement a convolutional neural network in Python?

You convert the image matrix to an array, rescale it between 0 and 1, reshape it so that it's of size 28 x 28 x 1, and feed this as an input to the network. You'll use three convolutional layers: The first layer will have 32-3 x 3 filters, The second layer will have 64-3 x 3 filters and.

What is convolution layer Keras?

It is a convolution 2D layer. It creates a convolutional kernel with the layer input creates a tensor of outputs. input_shape refers the tuple of integers with RGB value in data_format = “channels_last”. The signature of the Conv2D function and its arguments with default value is as follows − keras.


1 Answers

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.

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

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

like image 148
Nassim Ben Avatar answered Oct 03 '22 02:10

Nassim Ben