12.7.keras快速开始
Windows 10
Python 3.7.3 @ MSC v.1915 64 bit (AMD64)
Latest build date 2020.07.05
tensorflow version: 2.2.0
import keras
from toolkit import H
from keras import models
from keras import layers
# tf 2.0+ API更改 keras 和 tf.keras 混用可能不兼容
# from keras.models import Sequential
# from keras.layers import Dense, Activation
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
# tf 2.0+ API更改 以下语句不再可用
# from tensorflow.keras.backend import K
# from keras.backend import K
from tensorflow.keras.backend import mean
gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(gpus[0], True)
keras 的顶层api
h = H(keras)
d = h.dicts
module
d["function"]
[]
d["class"]
['Model', 'RandomRotation', 'Sequential']
d["module"]
['activations.py',
'applications',
'backend.py',
'callbacks.py',
'constraints.py',
'datasets',
'engine',
'initializers.py',
'layers',
'losses.py',
'metrics.py',
'models.py',
'objectives.py',
'optimizers',
'preprocessing',
'regularizers.py',
'utils',
'wrappers']
建立 Sequential 模型
model = Sequential([Dense(32, input_shape=(784,)),
Activation("relu"), Dense(10),
Activation("softmax")])
或者使用add方法将各网络层添加到模型
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation("relu"))
配置学习过程 compile方法
编译模型时必须指明损失函数和优化器,也可以自定义损失函数。
多分类
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
metrics=['accuracy'])
二分类
model.compile(optimizer='rmsprop', loss='binary_crossentropy',
metrics=['accuracy'])
均方误差回归问题
model.compile(optimizer='rmsprop', loss='mse')
自定义评估函数
def mean_pred(y_true, y_pred):
# return K.mean(y_pred)
return mean(y_pred)
model.compile(optimizer='rmsprop', loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
训练
Keras 模型在输入数据和标签的 Numpy 矩阵上进行训练。为了训练一个模型,你通常会使 用 fit 函数。
二分类
对于具有 2 个类的单输入模型(二进制分类):
import numpy as np
# 生成虚拟数据
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))
# 搭建模型
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy',
metrics=['accuracy'])
# 训练模型 以 64 个样本为一个 batch 进行迭代
model.fit(data, labels, epochs=2, batch_size=64)
data_pred = model.predict_classes(data)
Epoch 1/2
1/16 [>.............................] - ETA: 0s - loss: 0.6671 -
accuracy:
0.6250
8/16 [==============>...............] - ETA: 0s - loss: 0.7103 -
accuracy:
0.5488
15/16 [===========================>..] - ETA: 0s - loss: 0.7050 -
accuracy:
0.5302
16/16 [==============================] - 0s 7ms/step - loss: 0.7075 -
accuracy: 0.5240
Epoch 2/2
1/16 [>.............................] - ETA: 0s - loss: 0.6855 -
accuracy:
0.6094
9/16 [===============>..............] - ETA: 0s - loss: 0.6984 -
accuracy:
0.5104
16/16 [==============================] - 0s 6ms/step - loss: 0.7003 -
accuracy: 0.5030
多分类
对于具有 10 个类的单输入模型(多分类分类):
# 生成虚拟数据
np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))
# 将标签转换为分类的 one-hot 编码
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax', ))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',
metrics=['accuracy'])
# 训练模型 以 64 个样本为一个 batch 进行迭代
model.fit(data, one_hot_labels, epochs=2, batch_size=64)
model.summary()
# model.inputs[0]
# model.evaluate()
Epoch 1/2
1/16 [>.............................] - ETA: 0s - loss: 2.6500 -
accuracy:
0.0938
7/16 [============>.................] - ETA: 0s - loss: 2.4654 -
accuracy:
0.0804
13/16 [=======================>......] - ETA: 0s - loss: 2.4299 -
accuracy:
0.0817
16/16 [==============================] - 0s 9ms/step - loss: 2.4209 -
accuracy: 0.0810
Epoch 2/2
1/16 [>.............................] - ETA: 0s - loss: 2.3873 -
accuracy:
0.0625
6/16 [==========>...................] - ETA: 0s - loss: 2.3356 -
accuracy:
0.0911
12/16 [=====================>........] - ETA: 0s - loss: 2.3405 -
accuracy:
0.0872
16/16 [==============================] - 0s 9ms/step - loss: 2.3407 -
accuracy: 0.0900
Model: "sequential_43"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_75 (Dense) (None, 32) 3232
_________________________________________________________________
dense_76 (Dense) (None, 10) 330
=================================================================
Total params: 3,562
Trainable params: 3,562
Non-trainable params: 0
_________________________________________________________________