展开

keras

最后发布时间 : 2023-10-17 23:37:06 浏览量 :

生信小木屋

load_data进行数据载入处理

import numpy as np

# 导入Keras库
from keras.models import Sequential
# 导入Keras层
from keras.layers import Dense,Dropout,Activation
# 导入Keras优化器
from keras.optimizers import SGD,Adam
# 导入Keras数据集
from keras.utils import to_categorical
from keras.datasets import mnist

print(np.__version__) # 1.23.5
print(keras.__version__) # 2.13.1

# 加载数据函数,返回训练集和测试集
def load_data():
    # 加载mnist数据集,获取训练集和测试集
    (x_train,y_train),(x_test,y_test)=mnist.load_data()
    # 获取前10000个样本
    number=10000
    # 获取前10000个样本的训练集
    x_train=x_train[0:number]
    # 获取前10000个样本的标签
    y_train=y_train[0:number]
    # 将训练集的形状转换为(number,28*28)
    x_train=x_train.reshape(number,28*28)
    # 将测试集的形状转换为(number,28*28)
    x_test=x_test.reshape(x_test.shape[0],28*28) 
    # 将训练集的类型转换为float32
    x_train=x_train.astype('float32')
    # 将测试集的类型转换为float32
    x_test=x_test.astype('float32')
    # 将标签转换为one-hot编码
    y_train=to_categorical(y_train,10)
    y_test=to_categorical(y_test,10)
    # 获取训练集
    x_train=x_train
    # 获取测试集
    x_test=x_test
    # 将训练集的像素值转换为0-1
    x_train=x_train/255
    # 将测试集的像素值转换为0-1
    x_test=x_test/255
    # 返回训练集和测试集
    return (x_train,y_train),(x_test,y_test)

(x_train,y_train),(x_test,y_test)=load_data()

查看x_train的形状

print(x_train.shape) # (10000, 784)

表示有10000个数据,每个数据有784个像素点

可视化x_train

import matplotlib.pyplot as plt
plt.imshow(x_train[0].reshape(28,28),cmap='gray')
plt.show()

生信小木屋

查看y_train的形状

print(y_train.shape) # (10000, 10)

查看y_train

y_train[0]
array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)

define a set of function

生信小木屋

# 定义一个模型
model = Sequential()
# 添加一个输入层,输入维度为28*28,输出维度为633,激活函数为sigmoid
model.add(Dense(input_dim=28*28, units=633,activation='sigmoid'))
# 添加一个隐藏层,输出维度为633,激活函数为sigmoid
model.add(Dense(units=633,activation="sigmoid"))
# 添加一个隐藏层,输出维度为633,激活函数为sigmoid
model.add(Dense(units=633,activation="sigmoid"))
# 添加一个输出层,输出维度为10,激活函数为softmax
model.add(Dense(units=10,activation="softmax"))

goodness of function

生信小木屋

# 编译模型,损失函数为mse,优化器为SGD,目标函数为accuracy
model.compile(loss="mse",optimizer=SGD(learning_rate=0.1), metrics=['accuracy'])
# 训练模型,每次训练100个样本,训练20次
model.fit(x_train, y_train, batch_size=100, epochs=20)

# 评估模型,计算测试集上的准确率
restult = model.evaluate(x_test, y_test)
print("Test Acc:",restult[1])

调参过程

分类问题mse不适合,将loss mse function 改为categorical_crossentropy,看看有怎样的差别

model.compile(loss="categorical_crossentropy",optimizer=SGD(learning_rate=0.1), metrics=['accuracy'])

当我们一换categorical_crossentropy,在train set上的结果就起飞了。得到87.34%的正确率,现在就比较有train起来了。

Epoch 20/20
100/100 [==============================] - 1s 14ms/step - loss: 0.4975 - accuracy: 0.8551
1/1 [==============================] - 0s 426ms/step - loss: 0.4771 - accuracy: 0.8661
Train Acc: 0.866100013256073
313/313 [==============================] - 1s 3ms/step - loss: 0.5052 - accuracy: 0.8471
Test Acc: 0.847100019454956