keras 安装
首先安装anaconda,因为keras是在TensorFlow基础上再封装的框架。需要再anaconda环境里面安装TensorFlow。最后安装keras。注意:TensorFlow=2.6、Keras=2.6、Python=3.6版本兼容问题。
线性回归
导入依赖
import numpy as np # 生成随机点
import matplotlib.pyplot as plt # 画图显示
from keras.models import Sequential # 构建网络模型,使用序列化创建
from keras.layers import Dense # 全连接层
生成散点图
x_data = np.random.rand(100) # 随机生成100整数
noise = np.random.normal(0, 0.01, x_data.shape) # 随机生成0 0.01 的小数
y_data = 0.2 * x_data + 0.2 + noise # 构建函数
plt.scatter(x_data, y_data) # 输出散点图
plt.show() # 显示
进行训练并且输出预测结果
# 构建模型
model = Sequential() # 序列化构建模型
model.add(Dense(units=1, input_dim=1)) # Dense 全连接层
model.compile(optimizer='sgd', loss='mse') # 配置训练参数
for step in range(5000): # 迭代次数 5000
cost = model.train_on_batch(x_data, y_data) # 一批次更新一次训练
if cost % 500 == 0:
print("cost_step",step,":",cost)
W, b = model.layers[0].get_weights() #获得参数权重
print("W:",W,"b",b)
y_pred = model.predict(x_data) # 预测y
plt.scatter(x_data, y_data)
plt.plot(x_data, y_pred, '-r' ,lw=3)
plt.show()
结果展示
非线性回归
导入依赖
from keras.models import Sequential
from keras.layers import Dense, Activation
from tensorflow.keras.optimizers import SGD
import numpy as np
import matplotlib.pyplot as plt
画出散点图
x_data = np.linspace(-2, 2, 100)
nosize = np.random.normal(-0.2, 0.2, x_data.shape)
y_data = np.square(x_data) + nosize
plt.scatter(x_data, y_data)
plt.show()
开始训练并画出拟合曲线
# 构建网络结构
model = Sequential()
model.add(Dense(units=10, input_dim=1))
model.add(Activation('selu')) # 收敛效果不好
model.add(Dense(units=1))
model.add(Activation('selu'))
sgd = SGD(learning_rate=0.01) # 学习率过度过小收敛效果不好成直线
# 设置训练配置
model.compile(optimizer=sgd, loss='mse')
for step in range(5000):
cost = model.train_on_batch(x_data, y_data)
if step % 500 == 0 :
print('cost',step,":",cost)
wt = model.layers[1].get_weights()
print("权重",wt)
# 获取模型参数
y_pre = model.predict(x_data)
plt.scatter(x_data, y_data)
plt.plot(x_data, y_pre, '-r', 2)
plt.show()
运行结果:
小结:1:不同的学习速率对拟合图像情况不同,速率过大导致无法有效拟合,速率过小导致拟合速度太慢。
2: 激活函数不同拟合出来的曲线不同,有的激活函数可以拟合出来,有的激活不能拟合出来。
MNIST分类
导入依赖
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from tensorflow.keras.optimizers import SGD
加载数据集、构建模型、开始训练
# 1 加载数据集
(x_train, x_label),(t_train, t_label) = mnist.load_data()
# 2 数据格式处理
# 2.1 数据
x_train = x_train.reshape(x_train.shape[0], -1)/255
t_train = t_train.reshape(t_train.shape[0], -1)/255
# 2.2 标签
x_label = np_utils.to_categorical(x_label, num_classes=10)
t_label = np_utils.to_categorical(t_label, num_classes=10)
print(x_train.shape, x_label.shape)
# 3 构建模型
model = Sequential(
Dense(units=10, input_dim=784,bias_initializer='one', activation='softmax')
)
sgd = SGD(learning_rate=0.15)
# 4 开始训练进行结果展示
model.compile(
optimizer=sgd,
loss='mse',
metrics=['accuracy']
)
model.fit(x_train, x_label, batch_size=64, epochs=20)
loss, accuray = model.evaluate(t_train, t_label)
print("loss:",loss)
print("accuray:",accuray)
运行结果
loss='categorical_crossentropy' # 由mse 修改为categorical_crossentropy 在训练相同次数是,准确度有明显提高
提高模型准确度
1:修改网络结构,添加网络层,并修改激活函数。
model = Sequential(
[
Dense(units=200, input_dim=784,bias_initializer='one', activation='relu'),
Dense(units=100, input_dim=200,bias_initializer='one', activation='relu'),
Dense(units=10, input_dim=100,bias_initializer='one', activation='softmax')
]
)
修改后的网络模型运行结果:准确度大幅提升
防止过拟合网络模型添加Dropout层:
model = Sequential(
[
Dense(units=200, input_dim=784,bias_initializer='one', activation='relu'),
Dropout(0.35),
Dense(units=100, input_dim=200,bias_initializer='one', activation='relu'),
Dropout(0.35),
Dense(units=10, input_dim=100,bias_initializer='one', activation='softmax')
]
)
添加Dropout层运行结果:与没有添加Dropout相比,准确度有所下降,但是能有效避免过拟合。
CNN网络模型训练手写数字
导入依赖
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout,Convolution2D, MaxPool2D, Flatten
from tensorflow.keras.optimizers import SGD
使用卷积神经网络构建模型
# 1 加载数据集
(x_train, x_label),(t_train, t_label) = mnist.load_data()
# 2 数据格式处理
# 2.1 数据
x_train = x_train.reshape(-1, 28, 28, 1)/255.0
t_train = t_train.reshape(-1, 28, 28, 1)/255.0
# 2.2 标签
x_label = np_utils.to_categorical(x_label, num_classes=10)
t_label = np_utils.to_categorical(t_label, num_classes=10)
print(x_train.shape, x_label.shape)
# 3 构建模型 卷积 池化 卷积 池化 展平 全连接 全连接 输出
model = Sequential()
# 卷积层
model.add(Convolution2D(
input_shape = (28,28,1), # 输入形状
filters = 32, # 卷积核/过滤器个数
kernel_size = 5, # 卷积核大小
strides = 1, # 步长步幅
padding = 'same', # 填充类型,same 填充 前后大小形状不变
activation = 'relu' # 激活函数类型
# 6000 28 28 32
))
# 最大池化
model.add(MaxPool2D(
pool_size = 2, # 池化大小
strides = 2, # 步长步幅
padding = 'same', # 填充类型
# 6000 14 14 32
))
model.add(Convolution2D(
filters = 64,
kernel_size = 5,
strides = 1,
padding = 'same',
activation = 'relu'
# 60000 14 14 64
))
model.add(MaxPool2D(
pool_size = 2,
strides = 2,
padding = 'same'
# 6000 7 7 64
))
model.add(Flatten())
model.add(Dense(
units = 1024,
activation = 'relu'
# 60000 1024
))
model.add(Dropout(0.35))
model.add(Dense(units = 10, activation='softmax'))
sgd = SGD(learning_rate=0.15)
# 4 开始训练进行结果展示
model.compile(
optimizer=sgd,
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, x_label, batch_size=64, epochs=8)
loss, accuray = model.evaluate(t_train, t_label)
print("test_loss:",loss)
print("test_accuray:",accuray)
loss, accuray = model.evaluate(x_train, x_label)
print("train_loss:",loss)
print("train_accuray:",accuray)
运行结果:添加卷积层后的模型,准确度达到99%以上。