利用tensorflow.keras创建四层神经网络并绘制loss和accuracy曲线,打印和生成神经网络模型图

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential  # 引入引入网络模型
from tensorflow.keras.layers import Dense, Dropout  # 引入全连接层,输出层,激活器
from tensorflow.keras.optimizers import SGD  # 随机梯度下降法
import matplotlib.pyplot as plt
# 产生训练和测试数据
import numpy as np

x_train = np.random.random((1000, 20))  # 产生随机数,1000个样本,20个属性
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)  # one-hot编码,1000个样本,10种分类

x_test = np.random.random((100, 20))  # 产生随机数,100个样本,20个属性
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)  # one-hot编码,100个样本,10种分类

model = Sequential()  # 序贯模型

model.add(Dense(64, activation='relu', input_dim=20))  # 20个输入,64个隐藏节点,relu激活的全连接网络
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))  # 64个输出节点的
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))  # 10个输出节点的

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

history = model.fit(x_train, y_train, epochs=200, batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)


# 绘图函数
def print_history(history):
    # 绘制训练 & 验证的准确率值
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['loss'])
    plt.title('Model accuracy&loss')
    plt.xlabel('Epoch')
    plt.legend(['Train_acc', 'Val_acc'])
    plt.show()


print(history.params)
print_history(history)  # 调用绘图函数
model.summary()
tf.keras.utils.plot_model(model, "my_first_model_with_shape_info.png", show_shapes=True)

你可能感兴趣的:(tensorflow,keras,深度学习)