早有耳闻keras是一个神器,对于我这种需要在一个月内完成竞赛任务的同时,还要上课的人来说,很方便。之前学习了tensorflow,keras安装时也是基于tensorflow作为backen的。主要学习了莫老师的课程,在此总结一下keras的基本使用方法和技巧。
由于服务器上keras版本较低,需要以下修改方式
# 新版keras中有几个地方修改了:
np_utils.to_categorical(y_test, nb_classes=10) ----> nb_classes变成num_classes
model.fit(X_train,y_train,epochs=2,batch_size=32) -----> nb_epoch变成epoch
实现一个全连接层
######################################################
######################################################
# 数据标准化处理[regression将输入变成一列]
X_train = X_train.reshape(X_train.shape[0], -1) / 255. # normalize
X_test = X_test.reshape(X_test.shape[0], -1) / 255. # normalize
#CNN将输入变成[batch,channel,width,height]
X_train = X_train.reshape(-1, 1,28, 28)/255.
X_test = X_test.reshape(-1, 1,28, 28)/255.
#RNN将数据看成 [batch,time_step,input_dim]
X_train = X_train.reshape(-1, 28, 28) / 255. # normalize
X_test = X_test.reshape(-1, 28, 28) / 255. # normalize
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
######################################################
######################################################
from keras.models import Sequential
from keras.layers import Dense
######################################################
######################################################
# 模型建立时不考虑batch
model = Sequential() #建立模型
model.add(Dense(output_dim=1, input_dim=1)) #建立全连接层
# 建立多层模型,第一层定义输入格式,后面网络就不需要定义输入格式了,默认用前一层输出
model = Sequential([
Dense(output_dim=32, input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
#建立CNN模型
model = Sequential()
# Conv layer 1 output shape (32, 28, 28)
model.add(Convolution2D(
nb_filter=32,
nb_row=5,
nb_col=5,
border_mode='same', # Padding method
dim_ordering='th', # if use tensorflow, to set the input dimension order to theano ("th") style, but you can change it.
input_shape=(1, 28, 28) # (channel, height, width)
))
model.add(Activation('relu'))
# Pooling layer 1 (max pooling) output shape (32, 14, 14)
model.add(MaxPooling2D(
pool_size=(2, 2),
strides=(2, 2),
border_mode='same', # Padding method
))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))
# 建立一个RNN模型
model.add(SimpleRNN(
# for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size. Otherwise, model.evaluate() will get error.
batch_input_shape=(None, TIME_STEPS, INPUT_SIZE), # Or: input_dim=INPUT_SIZE, input_length=TIME_STEPS,
output_dim=CELL_SIZE,
unroll=True,
))
# build a LSTM RNN
model.add(LSTM(
batch_input_shape=(BATCH_SIZE, TIME_STEPS, INPUT_SIZE), # Or: input_dim=INPUT_SIZE, input_length=TIME_STEPS,
output_dim=CELL_SIZE,
return_sequences=True, # True: output at all steps. False: output as last step.
stateful=True, # True: the final state of batch1 is feed into the initial state of batch2
))
# add output layer
model.add(TimeDistributed(Dense(OUTPUT_SIZE)))
######################################################
######################################################
#定义优化算法和损失函数
model.compile(loss='mse', optimizer='sgd')
# Define your optimizer RMSprop
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
# We add metrics to get more results you want to see
model.compile(optimizer=rmsprop,
loss='categorical_crossentropy',
metrics=['accuracy'])
# Define your optimizer Adam
adam = Adam(lr=1e-4)
adam = Adam(LR)
# We add metrics to get more results you want to see
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
######################################################
######################################################
# 训练模型,减少cost [默认优化loss,让其最小]
cost = model.train_on_batch(X_train, Y_train)
#得到训练模型的参数
W, b = model.layers[0].get_weights()
# cost,原来train_on_batch还可以自己指定batch的
X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]
Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :]
cost = model.train_on_batch(X_batch, Y_batch)
cost = model.train_on_batch(X_batch, Y_batch)
# 模型训练 (两轮,batch_size=32)
model.fit(X_train, y_train, nb_epoch=2, batch_size=32)
model.fit(X_train, y_train, nb_epoch=1, batch_size=32)
######################################################
######################################################
# 计算模型在测试集上的误差以及训练结果
cost = model.evaluate(X_test, Y_test, batch_size=40) #在模型建立时就定义了
Y_pred = model.predict(X_test)
pred = model.predict(X_batch, BATCH_SIZE)
# 在模型建立时就定义好了能输出哪些值
# 不知道为什么,如果不加batch_size=10000,有的数据不会被测试到。也就是说,如果你做测试,还是加上batch_size
loss, accuracy = model.evaluate(X_test, y_test)
loss, accuracy = model.evaluate(X_test, y_test)
cost, accuracy = model.evaluate(X_test, y_test, batch_size=y_test.shape[0], verbose=False)
######################################################
######################################################
# 保存模型以及导入模型
print('test before save: ', model.predict(X_test[0:2]))
model.save('my_model.h5')
del model # 删除现有的模型
# load
model = load_model('my_model.h5')
print('test after load: ', model.predict(X_test[0:2]))