AI:Keras神经网络全连接层多分类问题的训练损失、验证损失和训练精度、验证精度,Python
import keras
from keras.layers import Dense
from keras import models
from keras import optimizers
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
def dummy_data(size, center_size):
# 制造一批训练数据。
x, y = make_blobs(n_samples=size, centers=center_size, n_features=2, random_state=1)
scaler = MinMaxScaler()
# MinMaxScaler:归一到 [ 0,1 ]
scaler.fit(x)
x = scaler.transform(x)
# print("原始训练数据")
# for i in range(len(x)):
# print("x=%s 标签:%s" % (x[i], y[i]))
# print("-----")
plt.scatter(x[:, 0], x[:, 1], c=y)
plt.grid()
plt.show()
return x, y
def fig_loss(history):
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values) + 1)
plt.plot(epochs, loss_values, 'b-.', label='Training loss')
plt.plot(epochs, val_loss_values, 'r--', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.grid()
plt.show()
def fig_acc(history):
history_dict = history.history
acc = history_dict['accuracy']
val_acc = history_dict['val_accuracy']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'g:', label='Training acc')
plt.plot(epochs, val_acc, 'r-', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.grid()
plt.show()
def predict(network):
print('泛化预测新数据:')
x_new, y_new = dummy_data(20, 4)
y_predict = network.predict_classes(x_new)
for i in range(len(x_new)):
print("数据=%s 正确分类:%s 预测分类:%s" % (x_new[i], y_new[i], y_predict[i]))
def dense():
MY_LAYER_NAME = 'layer_1'
MY_UNIT = 4
MY_SHAPE = (2,)
network = models.Sequential()
layer1 = Dense(name=MY_LAYER_NAME, units=MY_UNIT * 2, activation='relu', input_shape=MY_SHAPE)
network.add(layer1)
layer2 = Dense(units=MY_UNIT, activation="softmax")
network.add(layer2)
network.compile(loss="categorical_crossentropy",
optimizer=optimizers.RMSprop(lr=0.001),
metrics=['accuracy'])
# 训练数据
x_train, y_train = dummy_data(3000, MY_UNIT)
y_train = keras.utils.to_categorical(y_train)
# 验证数据
x_val, y_val = dummy_data(800, MY_UNIT)
y_val = keras.utils.to_categorical(y_val)
my_fit_history = network.fit(x_train, y_train, epochs=60, verbose=1, validation_data=(x_val, y_val))
# 绘制训练损失和验证损失
fig_loss(my_fit_history)
# 绘制训练精度和验证精度
fig_acc(my_fit_history)
x_test, y_test = dummy_data(100, 4)
results = network.evaluate(x_test, keras.utils.to_categorical(y_test))
print('训练结束,评估模型->')
print('损失:')
print(results[0])
print('精度:')
print(results[1])
# 利用训练好的模型泛化预测前所未知的新数据分类
predict(network)
if __name__ == "__main__":
dense()
程序实现意图:生成四种聚类数据散点,然后训练这批数据。训练完成后用模型泛化预测新的数据散点。
预训练数据集:
用来验证的数据集:
训练过程中的训练损失和验证损失:
训练精度和验证精度:
代码运行后的部分输出:
训练结束,评估模型->
损失:
0.04747089475393295
精度:
0.9900000095367432
泛化预测新数据:
数据=[0.5797192 0.50899869] 正确分类:3 预测分类:3
数据=[0.33139875 0.47067588] 正确分类:3 预测分类:3
数据=[0.97472153 0.86329803] 正确分类:0 预测分类:0
数据=[0.92136652 0.84507397] 正确分类:0 预测分类:0
数据=[1. 0.9613595] 正确分类:0 预测分类:0
数据=[0.44120969 0.01902758] 正确分类:2 预测分类:2
数据=[0.36449655 0.58074881] 正确分类:3 预测分类:3
数据=[0.37128437 0.45202284] 正确分类:3 预测分类:3
数据=[0.17903116 0.42564499] 正确分类:1 预测分类:1
数据=[0.81515505 0.98983219] 正确分类:0 预测分类:0
数据=[0. 0.47412934] 正确分类:1 预测分类:1
数据=[0.10218844 0.43170872] 正确分类:1 预测分类:1
数据=[0.29921329 0.00683701] 正确分类:2 预测分类:2
数据=[0.19979183 0.30467477] 正确分类:1 预测分类:1
数据=[0.34966961 0. ] 正确分类:2 预测分类:2
数据=[0.33670437 0.11068337] 正确分类:2 预测分类:2
数据=[0.872512 1. ] 正确分类:0 预测分类:0
数据=[0.41415172 0.38596844] 正确分类:3 预测分类:3
数据=[0.08299585 0.32143688] 正确分类:1 预测分类:1
数据=[0.29881198 0.04069237] 正确分类:2 预测分类:2