2017-03-15 114 views
8

我剛剛安裝了tensorflow和keras。和我有簡單的演示如下:用於實現卷積神經網絡的Keras

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` ' 

所以,我如何處理呢?

+2

該警告消息字面上表示您需要更改。 –

回答

17

正如Matias在評論中所說的,這非常簡單... Keras昨天更新了他們的API到2.0版本。很明顯,你已經下載了該版本,演示仍然使用「舊」API。 他們已經創建了警告,以便「舊」API仍然可以在2.0版本中工作,但是說它會改變,所以請從現在開始使用2.0 API。

使代碼適應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)) 

這不應該引發任何警告,它是keras 2.0版本的代碼。