导包
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import rmsprop_v2
from keras.utils import np_utils
获取数据
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print(X_train.shape)
print(X_test.shape)
可以看到X_train, y_train张量形状
转化为全连接模型所需要的形状
X_train = X_train.reshape(50000, 3072) # 50,000 images for training
X_test = X_test.reshape(10000, 3072) # 10,000 images for test
# Making data Float type
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# Gaussian Normalization( Z- score),标准化数据
X_train = (X_train- np.mean(X_train))/np.std(X_train)
X_test = (X_test- np.mean(X_test))/np.std(X_test)
数据展示
# Display number of training and test instances,打印数量
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# Convert class vectors to binary class matrices (ie one-hot vectors)
nb_classes = 10 #One class per digit
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
构建模型
# 输入是[n,3072]
model = Sequential()
model.add(Dense(512, input_shape=(3072,))) # 3*32*32 = 3072
# 激活层
model.add(Activation('relu'))
# Dropout层
model.add(Dropout(0.2)) # Regularization
model.add(Dense(120))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(10)) #Last layer with one output per class
# 使用softmax激活函数
model.add(Activation('softmax')) #We want a score simlar to a probability for each class
定义损失函数
# Compile the model
# Use rmsprop as an optimizer
rms = rmsprop_v2.RMSprop()
编译运行
model.compile(loss='categorical_crossentropy', optimizer=rms, metrics=["accuracy"])
#Evaluate how the model does on the test set
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
'''
Test score: 2.6132407188415527
Test accuracy: 0.09650000184774399
'''
模型
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 512) 1573376
_________________________________________________________________
activation (Activation) (None, 512) 0
_________________________________________________________________
dropout (Dropout) (None, 512) 0
_________________________________________________________________
dense_1 (Dense) (None, 120) 61560
_________________________________________________________________
activation_1 (Activation) (None, 120) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 120) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 1210
_________________________________________________________________
activation_2 (Activation) (None, 10) 0
预测数据
#Predict digit(0-9) for test Data
model.predict(X_test)
存储模型
#Saving the model
model.save('model.h5')
jsonModel = model.to_json()
model.save_weights('modelWeight.h5')
模型内存参数
#Summary of the model
model.summary()
展示结果
Total params: 1,636,146
Trainable params: 1,636,146
Non-trainable params: 0