目录
线性回归:
加噪音后再回归预测
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
#使用numpy随机生成100个点
x_data = np.random.rand(100)
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = x_data*0.1 + 0.2 + noise
#打印
plt.scatter(x_data, y_data)
plt.show()
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
#使用numpy随机生成100个点
x_data = np.random.rand(100)
noise = np.random.normal(0, 0.02, x_data.shape)
y_data = x_data*0.1 + 0.2 + noise
#打印
# plt.scatter(x_data, y_data)
# plt.show()
#构建顺序模型
model = Sequential()
#在模型中添加一个全连接层
model.add(Dense(units=1, input_dim=1))
model.compile(optimizer='sgd', loss='mse')
#一次在模型中放入一个批次 循环3001次
for step in range(3001):
#数据量小 全放进去
cost = model.train_on_batch(x_data, y_data)
#每500个batch打印一次cost值
if step % 5 == 0:
print('cost:', cost)
#打印权值和偏置值
w, b = model.layers[0].get_weights()
print('W:', w, 'b:', b)
#x_data输入网络中,得到预测值y_pred
y_predict = model.predict(x_data)
#打印
plt.scatter(x_data,y_data)
plt.scatter(x_data, y_predict, c='red')
plt.show()
非线性回归:
线性回归不能很好的拟合数据
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
#-0.5 0.5 之间均匀分布的200个点
x_data = np.linspace(-0.5, 0.5, 200)
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
#打印
plt.scatter(x_data,y_data)
plt.show()
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
#-0.5 0.5 之间均匀分布的200个点
x_data = np.linspace(-0.5, 0.5, 200)
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
#打印
# plt.scatter(x_data,y_data)
# plt.show()
#构建顺序模型
model = Sequential()
#在模型中添加一个全连接层
model.add(Dense(units=1, input_dim=1))
model.compile(optimizer='sgd', loss='mse')
#一次在模型中放入一个批次 循环3001次
for step in range(3001):
#数据量小 全放进去
cost = model.train_on_batch(x_data, y_data)
#每500个batch打印一次cost值
if step % 500 == 0:
print('cost:', cost)
#打印权值和偏置值
w, b = model.layers[0].get_weights()
print('W:', w, 'b:', b)
#x_data输入网络中,得到预测值y_pred
y_predict = model.predict(x_data)
#打印
plt.scatter(x_data,y_data)
plt.scatter(x_data, y_predict, c='red')
plt.show()
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import SGD
#-0.5 0.5 之间均匀分布的200个点
x_data = np.linspace(-0.5, 0.5, 200)
noise = np.random.normal(0,0.02,x_data.shape)
y_data = np.square(x_data) + noise
#打印
# plt.scatter(x_data,y_data)
# plt.show()
#构建顺序模型
model = Sequential()
#在模型中添加一个全连接层
#1-10-1
model.add(Dense(units=10, input_dim=1))
model.add(Activation('tanh'))
#不设置默认上一层的输出时这一层的输入维度10
model.add(Dense(units=1))
model.add(Activation('tanh'))
#定义优化算法
sgd = SGD(lr=0.3)
#sed 随机梯度下降
#mse 均方误差
model.compile(optimizer=sgd, loss='mse')
#一次在模型中放入一个批次 循环3001次
for step in range(3001):
#数据量小 全放进去
cost = model.train_on_batch(x_data, y_data)
#每500个batch打印一次cost值
if step % 500 == 0:
print('cost:', cost)
#打印权值和偏置值
w, b = model.layers[0].get_weights()
print('W:', w, 'b:', b)
#x_data输入网络中,得到预测值y_pred
y_predict = model.predict(x_data)
#打印
plt.scatter(x_data, y_data)
plt.scatter(x_data, y_predict, c='red')
plt.show()
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 keras.optimizers import SGD
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
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 keras.optimizers import SGD
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型:输入784个神经元 输出10个神经元
model = Sequential([
Dense(units=10, input_dim=784, bias_initializer='one', activation='softmax')
])
#定义优化器 loss function
sgd = SGD(lr=0.2)
model.compile(
optimizer=sgd,
loss='mse',
metrics=['accuracy']
)
#训练模型 60000 每次取32个 为一个epoch 运行10次
model.fit(x_train, y_train, batch_size=32, epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print('\ntest loss', loss)
print('accuracy', accuracy)
loss---交叉熵
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 keras.optimizers import SGD
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型:输入784个神经元 输出10个神经元
model = Sequential([
Dense(units=10, input_dim=784, bias_initializer='one', activation='softmax')
])
#定义优化器 loss function
sgd = SGD(lr=0.2)
model.compile(
optimizer=sgd,
#交叉熵分类问题准确率提升 收敛速度快
loss='categorical_crossentropy',
metrics=['accuracy']
)
#训练模型 60000 每次取32个 为一个epoch 运行10次
model.fit(x_train, y_train, batch_size=32, epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print('\ntest loss', loss)
print('accuracy', accuracy)
添加隐藏层
trian训练集的准确率很高
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
from keras.optimizers import SGD
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型:输入784个神经元 输出10个神经元
model = Sequential([
Dense(units=200, input_dim=784, bias_initializer='one', activation='tanh'),
Dense(units=100, bias_initializer='one', activation='tanh'),
Dense(units=10, bias_initializer='one', activation='softmax')
])
#定义优化器 loss function
sgd = SGD(lr=0.2)
model.compile(
optimizer=sgd,
#交叉熵分类问题准确率提升 收敛速度快
loss='categorical_crossentropy',
metrics=['accuracy']
)
#训练模型 60000 每次取32个 为一个epoch 运行10次
model.fit(x_train, y_train, batch_size=32, epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print('\ntest loss', loss)
print('accuracy', accuracy)
loss,accuracy = model.evaluate(x_train, y_train)
print('\ntrain loss', loss)
print('train accuracy', accuracy)
结果:
使用Dropout后:
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
from keras.optimizers import SGD
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型:输入784个神经元 输出10个神经元
model = Sequential([
Dense(units=200, input_dim=784, bias_initializer='one', activation='tanh'),
#40%神经元不工作
Dropout(0.4),
Dense(units=100, bias_initializer='one', activation='tanh'),
Dropout(0.4),
Dense(units=10, bias_initializer='one', activation='softmax')
])
#定义优化器 loss function
sgd = SGD(lr=0.2)
model.compile(
optimizer=sgd,
#交叉熵分类问题准确率提升 收敛速度快
loss='categorical_crossentropy',
metrics=['accuracy']
)
#训练模型 60000 每次取32个 为一个epoch 运行10次
model.fit(x_train, y_train, batch_size=32, epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print('\ntest loss', loss)
print('accuracy', accuracy)
loss,accuracy = model.evaluate(x_train, y_train)
print('\ntrain loss', loss)
print('train accuracy', accuracy)
结果:
正则化:如果模型很适合就不需要正则化和Dropout
from keras.regularizers import l2
Dense(units=200, input_dim=784, bias_initializer='one', activation='tanh',kernel_regularizer=l2(0.0003)),
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 keras.optimizers import SGD
from keras.regularizers import l2
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型:输入784个神经元 输出10个神经元
model = Sequential([
Dense(units=200, input_dim=784, bias_initializer='one', activation='tanh',kernel_regularizer=l2(0.0003)),
Dense(units=100, bias_initializer='one', activation='tanh',kernel_regularizer=l2(0.0003)),
Dense(units=10, bias_initializer='one', activation='softmax',kernel_regularizer=l2(0.0003))
])
#定义优化器 loss function
sgd = SGD(lr=0.2)
model.compile(
optimizer=sgd,
#交叉熵分类问题准确率提升 收敛速度快
loss='categorical_crossentropy',
metrics=['accuracy']
)
#训练模型 60000 每次取32个 为一个epoch 运行10次
model.fit(x_train, y_train, batch_size=32, epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print('\ntest loss', loss)
print('accuracy', accuracy)
loss,accuracy = model.evaluate(x_train, y_train)
print('\ntrain loss', loss)
print('train accuracy', accuracy)
结果:
7.优化器 Adam
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 keras.optimizers import SGD,Adam
#载入数据
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#(60000,28,28)
print('x_shape:', x_train.shape)
print('y_shape:', y_train.shape)
#(60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0], -1)/255.0
x_test = x_test.reshape(x_test.shape[0], -1)/255.0
#换one hot格式
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型:输入784个神经元 输出10个神经元
model = Sequential([
Dense(units=10, input_dim=784, bias_initializer='one', activation='softmax')
])
#定义优化器 loss function 训练过程中计算准确率
sgd = SGD(lr=0.2)
#lr学习率
adam = Adam(lr=0.001)
model.compile(
optimizer=adam,
#交叉熵分类问题准确率提升 收敛速度快
loss='categorical_crossentropy',
metrics=['accuracy']
)
#训练模型 60000 每次取32个 为一个epoch 运行10次
model.fit(x_train, y_train, batch_size=32, epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test, y_test)
print('\ntest loss', loss)
print('accuracy', accuracy)
结果:
8.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,MaxPooling2D,Flatten
from keras.optimizers import SGD,Adam
#py3.8
#载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
#(60000,28,28)--->(60000,28,28,1) 个数 长 宽 深度1黑白 彩色是3
#归一化0-1之间 所以除以255
x_train = x_train.reshape(-1, 28, 28, 1)/255.0
x_test = x_test.reshape(-1, 28, 28, 1)/255.0
#换one hot格式
#将类别向量(从0到nb_classes的整数向量)映射为二值类别矩阵,
# 用于应用到以categorical_crossentropy为目标函数的模型中.
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#定义顺序模型
model = Sequential()
#第一个卷积层
#input_shape输入平面
#filters 卷积核/滤波器个数
#kernel_size 卷积窗口大小
#strides步长
#padding 方式
#activation 激活函数
model.add(Convolution2D(
input_shape=(28, 28, 1),
filters=32,
kernel_size=5,
strides=1,
padding='same',
activation='relu'
))
#第一个池化层
model.add(MaxPooling2D(
pool_size=2,
strides=2,
padding='same',
))
#第二个卷积层
model.add(Convolution2D(64, 5, strides=1, padding='same', activation='relu'))
#第二个池化层
model.add(MaxPooling2D(2,2,'same'))
#把第二个池化层输出扁平化为1维
model.add(Flatten())
#第一个全连接层
model.add(Dense(1024,activation='relu'))
#Dropout
model.add(Dropout(0.5))
#第二个全连接层
model.add(Dense(10,activation='softmax'))
#定义优化器
adam = Adam(lr=1e-4)
#定义优化器 loss function 训练过程中计算准确率
model.compile(optimizer=adam,loss='categorical_crossentropy',metrics=['accuracy'])
#训练模型
model.fit(x_train,y_train,batch_size=64,epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('test loss',loss)
print('test accuracy',accuracy)
效果:
9.RNN应用
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,MaxPooling2D,Flatten
from keras.layers.recurrent import SimpleRNN,LSTM,GRU
from keras.optimizers import SGD,Adam
#数据长度一行有28个像素
input_size = 28
#序列长度一共有28行,所以28*28
time_steps = 28
#隐藏层cell个数
cell_size = 50
#载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
#样本数 time_steps,input_size
#(60000,28,28)
x_train = x_train/255
y_train = y_train/255
#换one hot
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#创建模型
model = Sequential()
#循环神经网络
model.add(SimpleRNN(
units=cell_size,#输出
input_shape=(time_steps,input_size),#输入
))
#输出层
model.add(Dense(10,activation='softmax'))
#定义优化器
adam=Adam(lr=1e-4)
#定义优化器 loss function 训练总过程中计算准确率
model.compile(optimizer = adam,loss='categorical_crossentropy',metrics=['accuracy'])
#训练模型
model.fit(x_train,y_train,batch_size=64,epochs=10)
#评估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('test loss',loss)
print('test accuracy',accuracy)
效果:。。。。。。过拟合了
![](https://i-blog.csdnimg.cn/blog_migrate/137401eabca5aad0cadfa74c74c0446b.png)
10.模型保存 默认在当前路径
#保存模型 添加最后
#HDF5文件 pip install h5py
model.save('model.h5')
11.载入模型
from keras.models import load_model
#载入模型
model = load_model('model.h5')
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,MaxPooling2D,Flatten
from keras.optimizers import SGD,Adam
from keras.models import load_model
#py3.8
#载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
#(60000,28,28)--->(60000,28,28,1) 个数 长 宽 深度1黑白 彩色是3
#归一化0-1之间 所以除以255
x_train = x_train.reshape(-1, 28, 28, 1)/255.0
x_test = x_test.reshape(-1, 28, 28, 1)/255.0
#换one hot格式
#将类别向量(从0到nb_classes的整数向量)映射为二值类别矩阵,
# 用于应用到以categorical_crossentropy为目标函数的模型中.
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
#载入模型
model = load_model('model.h5')
#评估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('test loss',loss)
print('test accuracy',accuracy)
#保存模型
#HDF5文件 pip install h5py
model.save('model.h5')
#保存参数,载入参数
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5')
#保存网络结构,载入网络结构
from keras.models import model_from_json
json_string = model.to_json()
model = model_from_json(json_string)
print(json_string)