更新到api 2.0后更新到keras 2.0错误

时间:2017-08-23 16:49:55

标签: neural-network deep-learning keras conv-neural-network

根据文档,我有一些代码我更新了keras 2.0,但它仍然给出了错误:

from __future__ import print_function
#simplified interface for building models 
import keras
#our handwritten character labeled dataset
from keras.datasets import mnist
from keras import applications
#because our models are simple
from keras.models import Sequential
#dense means fully connected layers, dropout is a technique to improve convergence, flatten to reshape our matrices for feeding
#into respective layers
from keras.layers import Dense, Dropout, Flatten
#for convolution (images) and pooling is a technique to help choose the most relevant features in an image
from keras.layers import Conv2D, MaxPooling2D
from keras import optimizers
from keras import backend as K

#mini batch gradient descent ftw
batch_size = 128
#10 difference characters
num_classes = 10
#very short training time
epochs = 12

# input image dimensions
#28x28 pixel images. 
img_rows, img_cols = 28, 28

# the data downloaded, shuffled and split between train and test sets
#if only all datasets were this easy to import and format
(x_train, y_train), (x_test, y_test) = mnist.load_data()

#this assumes our data format
#For 3D data, "channels_last" assumes (conv_dim1, conv_dim2, conv_dim3, channels) while 
#"channels_first" assumes (channels, conv_dim1, conv_dim2, conv_dim3).
if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

#more reshaping
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

#build our model
model = Sequential()
#convolutional layer with rectified linear unit activation
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
#again
model.add(Conv2D(64, (3, 3), activation='relu'))
#choose the best features via pooling
model.add(MaxPooling2D(pool_size=(2, 2)))
#randomly turn neurons on and off to improve convergence
model.add(Dropout(0.25))
#flatten since too many dimensions, we only want a classification output
model.add(Flatten())
#fully connected to get all relevant data
model.add(Dense(128, activation='relu'))
#one more dropout for convergence' sake :) 
model.add(Dropout(0.5))
#output a softmax to squash the matrix into output probabilities
model.add(Dense(num_classes, activation='softmax'))
#Adaptive learning rate (adaDelta) is a popular form of gradient descent rivaled only by adam and adagrad
#categorical ce since we have multiple classes (10) 
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

#train that ish!
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
 #how well did it do? 
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


#Save the model
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

以下是我得到的错误。 我已根据keras 2.0文档更新了我的代码,但我仍然无法分辨错误所在:

Using TensorFlow backend.
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(trainable=True, filters=32, use_bias=True, bias_regularizer=None, input_dtype="float32", batch_input_shape=[None, 28,..., activation="linear", kernel_initializer="glorot_uniform", kernel_constraint=None, activity_regularizer=None, padding="valid", strides=[1, 1], name="convolution2d_1", bias_constraint=None, data_format="channels_last", kernel_regularizer=None, kernel_size=(3, 3))`
  return cls(**config)
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(kernel_initializer="glorot_uniform", kernel_constraint=None, activity_regularizer=None, trainable=True, padding="valid", strides=[1, 1], filters=32, use_bias=True, name="convolution2d_2", bias_regularizer=None, bias_constraint=None, data_format="channels_last", kernel_regularizer=None, activation="linear", kernel_size=(3, 3))`
  return cls(**config)
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `MaxPooling2D` call to the Keras 2 API: `MaxPooling2D(name="maxpooling2d_1", trainable=True, data_format="channels_last", pool_size=[2, 2], padding="valid", strides=[2, 2])`
  return cls(**config)
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `Dropout` call to the Keras 2 API: `Dropout(rate=0.25, trainable=True, name="dropout_1")`
  return cls(**config)
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(name="dense_1", bias_regularizer=None, bias_constraint=None, activity_regularizer=None, trainable=True, kernel_constraint=None, kernel_regularizer=None, input_dim=None, units=128, kernel_initializer="glorot_uniform", use_bias=True, activation="linear")`
  return cls(**config)
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `Dropout` call to the Keras 2 API: `Dropout(rate=0.5, trainable=True, name="dropout_2")`
  return cls(**config)
/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py:1252: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(name="dense_2", bias_regularizer=None, bias_constraint=None, activity_regularizer=None, trainable=True, kernel_constraint=None, kernel_regularizer=None, input_dim=None, units=10, kernel_initializer="glorot_uniform", use_bias=True, activation="linear")`
  return cls(**config)
Loaded model from disc

请帮忙!

1 个答案:

答案 0 :(得分:1)

model.add(Dropout(rate = 0.1)) # rate = fraction of input units to drop

将删除警告:更新您对Keras 2 API的Dropout来电:Dropout(rate=0.5, trainable=True, name="dropout_2")

在旧的Keras API中,p是代表rate的变量,因此在旧API中,代码为model.add(Dropout(p = 0.1))

因此,请将p替换为rate