- 本文为365天深度学习训练营 中的学习记录博客
- 参考文章地址:深度学习100例-循环神经网络(RNN)心脏病预测 | 第46天
- 作者:K同学啊
要求:
1.本地读取并加载数据。
2.了解循环神经网络(RNN)的构建过程
3.测试集accuracy到达87%
拔高:
1.测试集accuracy到达89%
import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")
if gpus:
gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPU
tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用
tf.config.set_visible_devices([gpu0],"GPU")
gpus
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
数据介绍:
● age:1) 年龄
● sex:2) 性别
● cp:3) 胸痛类型 (4 values)
● trestbps:4) 静息血压
● chol:5) 血清胆甾醇 (mg/dl
● fbs:6) 空腹血糖 > 120 mg/dl
● restecg:7) 静息心电图结果 (值 0,1 ,2)
● thalach:8) 达到的最大心率
● exang:9) 运动诱发的心绞痛
● oldpeak:10) 相对于静止状态,运动引起的ST段压低
● slope:11) 运动峰值 ST 段的斜率
● ca:12) 荧光透视着色的主要血管数量 (0-3)
● thal:13) 0 = 正常;1 = 固定缺陷;2 = 可逆转的缺陷
● target:14) 0 = 心脏病发作的几率较小 1 = 心脏病发作的几率更大
import pandas as pd
import numpy as np
df = pd.read_csv("heart.csv")
df.head()
age | sex | cp | trestbps | chol | fbs | restecg | thalach | exang | oldpeak | slope | ca | thal | target | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 63 | 1 | 3 | 145 | 233 | 1 | 0 | 150 | 0 | 2.3 | 0 | 0 | 1 | 1 |
1 | 37 | 1 | 2 | 130 | 250 | 0 | 1 | 187 | 0 | 3.5 | 0 | 0 | 2 | 1 |
2 | 41 | 0 | 1 | 130 | 204 | 0 | 0 | 172 | 0 | 1.4 | 2 | 0 | 2 | 1 |
3 | 56 | 1 | 1 | 120 | 236 | 0 | 1 | 178 | 0 | 0.8 | 2 | 0 | 2 | 1 |
4 | 57 | 0 | 0 | 120 | 354 | 0 | 1 | 163 | 1 | 0.6 | 2 | 0 | 2 | 1 |
# 检查是否有空值
df.isnull().sum()
age 0
sex 0
cp 0
trestbps 0
chol 0
fbs 0
restecg 0
thalach 0
exang 0
oldpeak 0
slope 0
ca 0
thal 0
target 0
dtype: int64
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1, random_state = 1)
X_train.shape, y_train.shape
((272, 13), (272,))
# 将每一列特征标准化为标准正太分布,注意,标准化是针对每一列而言的
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
⭐函数原型
tf.keras.layers.SimpleRNN(units,activation=‘tanh’,use_bias=True,kernel_initializer=‘glorot_uniform’,
recurrent_initializer=‘orthogonal’,bias_initializer=‘zeros’,kernel_regularizer=None,recurrent_regularizer=None,bias_regularizer=None,activity_regularizer=None,kernel_constraint=None,recurrent_constraint=None,
bias_constraint=None,dropout=0.0,recurrent_dropout=0.0,return_sequences=False,return_state=False,
go_backwards=False,stateful=False,unroll=False,**kwargs)
关键参数说明
● units: 正整数,输出空间的维度。
● activation: 要使用的激活函数。 默认:双曲正切(tanh)。 如果传入 None,则不使用激活函数 (即 线性激活:a(x) = x)。
● use_bias: 布尔值,该层是否使用偏置向量。
● kernel_initializer: kernel 权值矩阵的初始化器, 用于输入的线性转换 (详见 initializers)。
● recurrent_initializer: recurrent_kernel 权值矩阵 的初始化器,用于循环层状态的线性转换 (详见 initializers)。
● bias_initializer:偏置向量的初始化器 (详见initializers).
● dropout: 在 0 和 1 之间的浮点数。 单元的丢弃比例,用于输入的线性转换。
import tensorflow
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,LSTM,SimpleRNN
model = Sequential()
model.add(SimpleRNN(200, input_shape= (13,1), activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
simple_rnn_1 (SimpleRNN) (None, 200) 40400
_________________________________________________________________
dense_2 (Dense) (None, 100) 20100
_________________________________________________________________
dense_3 (Dense) (None, 1) 101
=================================================================
Total params: 60,601
Trainable params: 60,601
Non-trainable params: 0
_________________________________________________________________
opt = tf.keras.optimizers.Adam(learning_rate=1e-4)
model.compile(loss='binary_crossentropy',
optimizer=opt,
metrics="accuracy")
from tensorflow.keras.callbacks import ModelCheckpoint
#保存最佳模型参数
checkpointer=ModelCheckpoint('best_model.h5',
monitor='val_accuracy',
verbose=1,
save_best_only=True,
save_weights_only=True
)
epochs = 100
history = model.fit(X_train, y_train,
epochs=epochs,
batch_size=128,
validation_data=(X_test, y_test),
verbose=1,
callbacks=[checkpointer])
Epoch 1/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6846 - accuracy: 0.6016
Epoch 00001: val_accuracy improved from -inf to 0.87097, saving model to best_model.h5
3/3 [==============================] - 0s 75ms/step - loss: 0.6814 - accuracy: 0.6618 - val_loss: 0.6546 - val_accuracy: 0.8710
Epoch 2/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6734 - accuracy: 0.7500
Epoch 00002: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 25ms/step - loss: 0.6714 - accuracy: 0.7390 - val_loss: 0.6392 - val_accuracy: 0.8710
Epoch 3/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6649 - accuracy: 0.7656
Epoch 00003: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 25ms/step - loss: 0.6619 - accuracy: 0.7574 - val_loss: 0.6245 - val_accuracy: 0.8710
Epoch 4/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6531 - accuracy: 0.7656
Epoch 00004: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 27ms/step - loss: 0.6529 - accuracy: 0.7574 - val_loss: 0.6099 - val_accuracy: 0.8710
Epoch 5/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6531 - accuracy: 0.7266
Epoch 00005: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 24ms/step - loss: 0.6441 - accuracy: 0.7647 - val_loss: 0.5951 - val_accuracy: 0.8387
Epoch 6/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6336 - accuracy: 0.7891
Epoch 00006: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 23ms/step - loss: 0.6357 - accuracy: 0.7721 - val_loss: 0.5805 - val_accuracy: 0.8387
Epoch 7/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6275 - accuracy: 0.7812
Epoch 00007: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 17ms/step - loss: 0.6272 - accuracy: 0.7721 - val_loss: 0.5663 - val_accuracy: 0.8387
Epoch 8/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6124 - accuracy: 0.7812
Epoch 00008: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 18ms/step - loss: 0.6187 - accuracy: 0.7757 - val_loss: 0.5517 - val_accuracy: 0.8387
Epoch 9/100
1/3 [=========>....................] - ETA: 0s - loss: 0.6114 - accuracy: 0.7812
Epoch 00009: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 16ms/step - loss: 0.6097 - accuracy: 0.7721 - val_loss: 0.5371 - val_accuracy: 0.8387
Epoch 10/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5947 - accuracy: 0.8203
Epoch 00010: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 18ms/step - loss: 0.6005 - accuracy: 0.7684 - val_loss: 0.5223 - val_accuracy: 0.8387
Epoch 11/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5712 - accuracy: 0.8359
Epoch 00011: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 15ms/step - loss: 0.5914 - accuracy: 0.7757 - val_loss: 0.5068 - val_accuracy: 0.8387
Epoch 12/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5737 - accuracy: 0.7969
Epoch 00012: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 18ms/step - loss: 0.5811 - accuracy: 0.7831 - val_loss: 0.4918 - val_accuracy: 0.8065
Epoch 13/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5675 - accuracy: 0.7969
Epoch 00013: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 16ms/step - loss: 0.5711 - accuracy: 0.7868 - val_loss: 0.4760 - val_accuracy: 0.8710
Epoch 14/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5722 - accuracy: 0.7812
Epoch 00014: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 14ms/step - loss: 0.5602 - accuracy: 0.7868 - val_loss: 0.4589 - val_accuracy: 0.8710
Epoch 15/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5525 - accuracy: 0.7656
Epoch 00015: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 16ms/step - loss: 0.5486 - accuracy: 0.7868 - val_loss: 0.4405 - val_accuracy: 0.8710
Epoch 16/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5496 - accuracy: 0.7734
Epoch 00016: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 12ms/step - loss: 0.5360 - accuracy: 0.7978 - val_loss: 0.4203 - val_accuracy: 0.8710
Epoch 17/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5248 - accuracy: 0.7969
Epoch 00017: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 18ms/step - loss: 0.5230 - accuracy: 0.7978 - val_loss: 0.3988 - val_accuracy: 0.8710
Epoch 18/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5185 - accuracy: 0.8047
Epoch 00018: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 20ms/step - loss: 0.5078 - accuracy: 0.8088 - val_loss: 0.3764 - val_accuracy: 0.8710
Epoch 19/100
1/3 [=========>....................] - ETA: 0s - loss: 0.5054 - accuracy: 0.7734
Epoch 00019: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 19ms/step - loss: 0.4927 - accuracy: 0.7941 - val_loss: 0.3547 - val_accuracy: 0.8710
Epoch 20/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4773 - accuracy: 0.7891
Epoch 00020: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 20ms/step - loss: 0.4780 - accuracy: 0.7978 - val_loss: 0.3330 - val_accuracy: 0.8387
Epoch 21/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4849 - accuracy: 0.7734
Epoch 00021: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 15ms/step - loss: 0.4645 - accuracy: 0.8051 - val_loss: 0.3120 - val_accuracy: 0.8387
Epoch 22/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4734 - accuracy: 0.7656
Epoch 00022: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 13ms/step - loss: 0.4515 - accuracy: 0.8088 - val_loss: 0.2943 - val_accuracy: 0.8387
Epoch 23/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4143 - accuracy: 0.8281
Epoch 00023: val_accuracy did not improve from 0.87097
3/3 [==============================] - 0s 14ms/step - loss: 0.4425 - accuracy: 0.8162 - val_loss: 0.2824 - val_accuracy: 0.8710
Epoch 24/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4150 - accuracy: 0.8516
Epoch 00024: val_accuracy improved from 0.87097 to 0.90323, saving model to best_model.h5
3/3 [==============================] - 0s 19ms/step - loss: 0.4347 - accuracy: 0.8088 - val_loss: 0.2752 - val_accuracy: 0.9032
Epoch 25/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4011 - accuracy: 0.8359
Epoch 00025: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.4298 - accuracy: 0.8125 - val_loss: 0.2680 - val_accuracy: 0.9032
Epoch 26/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3838 - accuracy: 0.8281
Epoch 00026: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 16ms/step - loss: 0.4225 - accuracy: 0.8162 - val_loss: 0.2622 - val_accuracy: 0.8710
Epoch 27/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4491 - accuracy: 0.7812
Epoch 00027: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 18ms/step - loss: 0.4161 - accuracy: 0.8051 - val_loss: 0.2588 - val_accuracy: 0.8387
Epoch 28/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4063 - accuracy: 0.8047
Epoch 00028: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 20ms/step - loss: 0.4118 - accuracy: 0.8088 - val_loss: 0.2570 - val_accuracy: 0.8387
Epoch 29/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3714 - accuracy: 0.8125
Epoch 00029: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 20ms/step - loss: 0.4081 - accuracy: 0.8125 - val_loss: 0.2579 - val_accuracy: 0.8387
Epoch 30/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3850 - accuracy: 0.8438
Epoch 00030: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 20ms/step - loss: 0.4068 - accuracy: 0.8125 - val_loss: 0.2601 - val_accuracy: 0.8387
Epoch 31/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4048 - accuracy: 0.8203
Epoch 00031: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 21ms/step - loss: 0.4059 - accuracy: 0.8125 - val_loss: 0.2601 - val_accuracy: 0.8387
Epoch 32/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4111 - accuracy: 0.7969
Epoch 00032: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 21ms/step - loss: 0.3999 - accuracy: 0.8162 - val_loss: 0.2581 - val_accuracy: 0.8387
Epoch 33/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4208 - accuracy: 0.8125
Epoch 00033: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.3943 - accuracy: 0.8272 - val_loss: 0.2573 - val_accuracy: 0.8710
Epoch 34/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3998 - accuracy: 0.8359
Epoch 00034: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.3922 - accuracy: 0.8346 - val_loss: 0.2570 - val_accuracy: 0.9032
Epoch 35/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3938 - accuracy: 0.8203
Epoch 00035: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 17ms/step - loss: 0.3904 - accuracy: 0.8346 - val_loss: 0.2546 - val_accuracy: 0.9032
Epoch 36/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3135 - accuracy: 0.8906
Epoch 00036: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 15ms/step - loss: 0.3864 - accuracy: 0.8346 - val_loss: 0.2505 - val_accuracy: 0.9032
Epoch 37/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3499 - accuracy: 0.8516
Epoch 00037: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.3818 - accuracy: 0.8382 - val_loss: 0.2468 - val_accuracy: 0.8710
Epoch 38/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3721 - accuracy: 0.8203
Epoch 00038: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 13ms/step - loss: 0.3798 - accuracy: 0.8309 - val_loss: 0.2448 - val_accuracy: 0.8710
Epoch 39/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4043 - accuracy: 0.8125
Epoch 00039: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 13ms/step - loss: 0.3796 - accuracy: 0.8309 - val_loss: 0.2449 - val_accuracy: 0.8710
Epoch 40/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3250 - accuracy: 0.8516
Epoch 00040: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 16ms/step - loss: 0.3773 - accuracy: 0.8272 - val_loss: 0.2455 - val_accuracy: 0.8710
Epoch 41/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3971 - accuracy: 0.8359
Epoch 00041: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 18ms/step - loss: 0.3729 - accuracy: 0.8346 - val_loss: 0.2469 - val_accuracy: 0.8710
Epoch 42/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3976 - accuracy: 0.8359
Epoch 00042: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 23ms/step - loss: 0.3684 - accuracy: 0.8493 - val_loss: 0.2495 - val_accuracy: 0.9032
Epoch 43/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3895 - accuracy: 0.8594
Epoch 00043: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3657 - accuracy: 0.8529 - val_loss: 0.2530 - val_accuracy: 0.9032
Epoch 44/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3558 - accuracy: 0.8828
Epoch 00044: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 15ms/step - loss: 0.3665 - accuracy: 0.8456 - val_loss: 0.2531 - val_accuracy: 0.9032
Epoch 45/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3942 - accuracy: 0.8359
Epoch 00045: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3636 - accuracy: 0.8456 - val_loss: 0.2495 - val_accuracy: 0.9032
Epoch 46/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4089 - accuracy: 0.7969
Epoch 00046: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 15ms/step - loss: 0.3580 - accuracy: 0.8456 - val_loss: 0.2447 - val_accuracy: 0.9032
Epoch 47/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3755 - accuracy: 0.8516
Epoch 00047: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.3523 - accuracy: 0.8566 - val_loss: 0.2421 - val_accuracy: 0.9032
Epoch 48/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3662 - accuracy: 0.8516
Epoch 00048: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3489 - accuracy: 0.8676 - val_loss: 0.2409 - val_accuracy: 0.9032
Epoch 49/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3406 - accuracy: 0.8594
Epoch 00049: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3463 - accuracy: 0.8676 - val_loss: 0.2410 - val_accuracy: 0.9032
Epoch 50/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3865 - accuracy: 0.8359
Epoch 00050: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.3437 - accuracy: 0.8640 - val_loss: 0.2433 - val_accuracy: 0.9032
Epoch 51/100
1/3 [=========>....................] - ETA: 0s - loss: 0.4340 - accuracy: 0.8203
Epoch 00051: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.3424 - accuracy: 0.8566 - val_loss: 0.2466 - val_accuracy: 0.9032
Epoch 52/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3555 - accuracy: 0.8438
Epoch 00052: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.3405 - accuracy: 0.8566 - val_loss: 0.2473 - val_accuracy: 0.9032
Epoch 53/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3355 - accuracy: 0.8516
Epoch 00053: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 20ms/step - loss: 0.3377 - accuracy: 0.8566 - val_loss: 0.2447 - val_accuracy: 0.9032
Epoch 54/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3379 - accuracy: 0.8906
Epoch 00054: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 20ms/step - loss: 0.3334 - accuracy: 0.8750 - val_loss: 0.2446 - val_accuracy: 0.9032
Epoch 55/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3043 - accuracy: 0.8984
Epoch 00055: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.3312 - accuracy: 0.8713 - val_loss: 0.2456 - val_accuracy: 0.9032
Epoch 56/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2088 - accuracy: 0.9375
Epoch 00056: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 18ms/step - loss: 0.3283 - accuracy: 0.8713 - val_loss: 0.2474 - val_accuracy: 0.9032
Epoch 57/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2963 - accuracy: 0.8906
Epoch 00057: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 21ms/step - loss: 0.3251 - accuracy: 0.8713 - val_loss: 0.2497 - val_accuracy: 0.9032
Epoch 58/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3142 - accuracy: 0.8594
Epoch 00058: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 18ms/step - loss: 0.3230 - accuracy: 0.8640 - val_loss: 0.2484 - val_accuracy: 0.9032
Epoch 59/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2813 - accuracy: 0.8906
Epoch 00059: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 23ms/step - loss: 0.3200 - accuracy: 0.8676 - val_loss: 0.2481 - val_accuracy: 0.9032
Epoch 60/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3216 - accuracy: 0.8594
Epoch 00060: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3177 - accuracy: 0.8713 - val_loss: 0.2489 - val_accuracy: 0.9032
Epoch 61/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3229 - accuracy: 0.8516
Epoch 00061: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.3150 - accuracy: 0.8676 - val_loss: 0.2485 - val_accuracy: 0.9032
Epoch 62/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3056 - accuracy: 0.8828
Epoch 00062: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 16ms/step - loss: 0.3138 - accuracy: 0.8713 - val_loss: 0.2504 - val_accuracy: 0.8710
Epoch 63/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2822 - accuracy: 0.8984
Epoch 00063: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 22ms/step - loss: 0.3121 - accuracy: 0.8750 - val_loss: 0.2565 - val_accuracy: 0.9032
Epoch 64/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2844 - accuracy: 0.8906
Epoch 00064: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3095 - accuracy: 0.8750 - val_loss: 0.2651 - val_accuracy: 0.9032
Epoch 65/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3093 - accuracy: 0.8594
Epoch 00065: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 16ms/step - loss: 0.3111 - accuracy: 0.8640 - val_loss: 0.2671 - val_accuracy: 0.9032
Epoch 66/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3609 - accuracy: 0.8203
Epoch 00066: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 21ms/step - loss: 0.3088 - accuracy: 0.8676 - val_loss: 0.2607 - val_accuracy: 0.9032
Epoch 67/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3002 - accuracy: 0.8750
Epoch 00067: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 22ms/step - loss: 0.3042 - accuracy: 0.8787 - val_loss: 0.2587 - val_accuracy: 0.9032
Epoch 68/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3041 - accuracy: 0.8828
Epoch 00068: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.3017 - accuracy: 0.8750 - val_loss: 0.2557 - val_accuracy: 0.9032
Epoch 69/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3093 - accuracy: 0.8438
Epoch 00069: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 15ms/step - loss: 0.2999 - accuracy: 0.8750 - val_loss: 0.2540 - val_accuracy: 0.9032
Epoch 70/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2851 - accuracy: 0.8828
Epoch 00070: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 20ms/step - loss: 0.2995 - accuracy: 0.8860 - val_loss: 0.2543 - val_accuracy: 0.9032
Epoch 71/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2948 - accuracy: 0.8984
Epoch 00071: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 18ms/step - loss: 0.2970 - accuracy: 0.8824 - val_loss: 0.2572 - val_accuracy: 0.9032
Epoch 72/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2739 - accuracy: 0.9062
Epoch 00072: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.2940 - accuracy: 0.8860 - val_loss: 0.2612 - val_accuracy: 0.9032
Epoch 73/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3072 - accuracy: 0.8828
Epoch 00073: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 17ms/step - loss: 0.2912 - accuracy: 0.8860 - val_loss: 0.2659 - val_accuracy: 0.9032
Epoch 74/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2727 - accuracy: 0.8906
Epoch 00074: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.2902 - accuracy: 0.8897 - val_loss: 0.2697 - val_accuracy: 0.9032
Epoch 75/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3063 - accuracy: 0.8672
Epoch 00075: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 13ms/step - loss: 0.2890 - accuracy: 0.8897 - val_loss: 0.2728 - val_accuracy: 0.9032
Epoch 76/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2961 - accuracy: 0.8984
Epoch 00076: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 16ms/step - loss: 0.2860 - accuracy: 0.8897 - val_loss: 0.2789 - val_accuracy: 0.9032
Epoch 77/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3319 - accuracy: 0.8672
Epoch 00077: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 23ms/step - loss: 0.2831 - accuracy: 0.8897 - val_loss: 0.2862 - val_accuracy: 0.9032
Epoch 78/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3309 - accuracy: 0.8750
Epoch 00078: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 13ms/step - loss: 0.2809 - accuracy: 0.8934 - val_loss: 0.2920 - val_accuracy: 0.9032
Epoch 79/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2568 - accuracy: 0.9062
Epoch 00079: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.2797 - accuracy: 0.8897 - val_loss: 0.2978 - val_accuracy: 0.9032
Epoch 80/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2684 - accuracy: 0.8750
Epoch 00080: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 23ms/step - loss: 0.2790 - accuracy: 0.8824 - val_loss: 0.3019 - val_accuracy: 0.9032
Epoch 81/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3396 - accuracy: 0.8672
Epoch 00081: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 27ms/step - loss: 0.2776 - accuracy: 0.8860 - val_loss: 0.3033 - val_accuracy: 0.9032
Epoch 82/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2730 - accuracy: 0.9141
Epoch 00082: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 22ms/step - loss: 0.2763 - accuracy: 0.8897 - val_loss: 0.3009 - val_accuracy: 0.9032
Epoch 83/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2814 - accuracy: 0.8984
Epoch 00083: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 18ms/step - loss: 0.2727 - accuracy: 0.9007 - val_loss: 0.2967 - val_accuracy: 0.9032
Epoch 84/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2463 - accuracy: 0.8984
Epoch 00084: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 22ms/step - loss: 0.2700 - accuracy: 0.8971 - val_loss: 0.2975 - val_accuracy: 0.9032
Epoch 85/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2325 - accuracy: 0.9375
Epoch 00085: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 22ms/step - loss: 0.2684 - accuracy: 0.8971 - val_loss: 0.3013 - val_accuracy: 0.9032
Epoch 86/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2426 - accuracy: 0.9219
Epoch 00086: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 24ms/step - loss: 0.2705 - accuracy: 0.8971 - val_loss: 0.3020 - val_accuracy: 0.9032
Epoch 87/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2809 - accuracy: 0.8750
Epoch 00087: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 25ms/step - loss: 0.2672 - accuracy: 0.8897 - val_loss: 0.2952 - val_accuracy: 0.9032
Epoch 88/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2474 - accuracy: 0.8984
Epoch 00088: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.2610 - accuracy: 0.8971 - val_loss: 0.2952 - val_accuracy: 0.8710
Epoch 89/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2998 - accuracy: 0.8984
Epoch 00089: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.2592 - accuracy: 0.9081 - val_loss: 0.2980 - val_accuracy: 0.8710
Epoch 90/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2042 - accuracy: 0.9375
Epoch 00090: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 13ms/step - loss: 0.2581 - accuracy: 0.9081 - val_loss: 0.2997 - val_accuracy: 0.8387
Epoch 91/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2179 - accuracy: 0.9219
Epoch 00091: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.2595 - accuracy: 0.9044 - val_loss: 0.3007 - val_accuracy: 0.8387
Epoch 92/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2114 - accuracy: 0.9297
Epoch 00092: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 14ms/step - loss: 0.2582 - accuracy: 0.9044 - val_loss: 0.3015 - val_accuracy: 0.8710
Epoch 93/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2578 - accuracy: 0.9219
Epoch 00093: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 12ms/step - loss: 0.2529 - accuracy: 0.9044 - val_loss: 0.3063 - val_accuracy: 0.9032
Epoch 94/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3013 - accuracy: 0.8828
Epoch 00094: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 24ms/step - loss: 0.2591 - accuracy: 0.8971 - val_loss: 0.3128 - val_accuracy: 0.9032
Epoch 95/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2438 - accuracy: 0.8906
Epoch 00095: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 23ms/step - loss: 0.2644 - accuracy: 0.8897 - val_loss: 0.3059 - val_accuracy: 0.9032
Epoch 96/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2465 - accuracy: 0.8984
Epoch 00096: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 24ms/step - loss: 0.2578 - accuracy: 0.8971 - val_loss: 0.2959 - val_accuracy: 0.9032
Epoch 97/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2461 - accuracy: 0.8984
Epoch 00097: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 23ms/step - loss: 0.2490 - accuracy: 0.9044 - val_loss: 0.2884 - val_accuracy: 0.9032
Epoch 98/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2629 - accuracy: 0.8828
Epoch 00098: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 21ms/step - loss: 0.2462 - accuracy: 0.9044 - val_loss: 0.2819 - val_accuracy: 0.8710
Epoch 99/100
1/3 [=========>....................] - ETA: 0s - loss: 0.2490 - accuracy: 0.8906
Epoch 00099: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 19ms/step - loss: 0.2474 - accuracy: 0.9007 - val_loss: 0.2801 - val_accuracy: 0.8710
Epoch 100/100
1/3 [=========>....................] - ETA: 0s - loss: 0.3287 - accuracy: 0.8516
Epoch 00100: val_accuracy did not improve from 0.90323
3/3 [==============================] - 0s 15ms/step - loss: 0.2475 - accuracy: 0.8971 - val_loss: 0.2837 - val_accuracy: 0.8710
import matplotlib.pyplot as plt
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(14, 4))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-v0VQR8TX-1665626635982)(output_26_0.png)]
# 加载效果最好的模型权重
model.load_weights('best_model.h5')
scores = model.evaluate(X_test, y_test, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
accuracy: 90.32%