keras总结帖

来源:互联网 发布:syntax评分软件中文版 编辑:程序博客网 时间:2024/05/29 15:31

早有耳闻keras是一个神器,对于我这种需要在一个月内完成竞赛任务的同时,还要上课的人来说,很方便。之前学习了tensorflow,keras安装时也是基于tensorflow作为backen的。主要学习了莫老师的课程,在此总结一下keras的基本使用方法和技巧。

由于服务器上keras版本较低,需要以下修改方式

# 新版keras中有几个地方修改了:np_utils.to_categorical(y_test, nb_classes=10) ----> nb_classes变成num_classesmodel.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.   # normalizeX_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.      # normalizeX_test = X_test.reshape(-1, 28, 28) / 255.        # normalizey_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 Sequentialfrom keras.layers import Dense############################################################################################################# 模型建立时不考虑batchmodel = 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 RNNmodel.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 layermodel.add(TimeDistributed(Dense(OUTPUT_SIZE)))#############################################################################################################定义优化算法和损失函数model.compile(loss='mse', optimizer='sgd') # Define your optimizer RMSproprmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)# We add metrics to get more results you want to seemodel.compile(optimizer=rmsprop,              loss='categorical_crossentropy',              metrics=['accuracy'])# Define your optimizer Adamadam = Adam(lr=1e-4)adam = Adam(LR)# We add metrics to get more results you want to seemodel.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_sizeloss, 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  # 删除现有的模型# loadmodel = load_model('my_model.h5')print('test after load: ', model.predict(X_test[0:2]))
0 0