神经网络_使用TensorFlow预测气温

python 复制代码
import datetime
import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras import layers
import tensorflow.keras 
import warnings
from sklearn import preprocessing
warnings.filterwarnings("ignore")
%matplotlib inline

1.数据预处理

1.1导入数据

python 复制代码
temperatures = pd.read_csv('tf_temperature.csv')
print("数据维度", temperatures.shape)
temperatures.head()
数据维度 (348, 9)

| | year | month | day | week | temp_2 | temp_1 | average | actual | friend |
| 0 | 2016 | 1 | 1 | Fri | 45 | 45 | 45.6 | 45 | 29 |
| 1 | 2016 | 1 | 2 | Sat | 44 | 45 | 45.7 | 44 | 61 |
| 2 | 2016 | 1 | 3 | Sun | 45 | 44 | 45.8 | 41 | 56 |
| 3 | 2016 | 1 | 4 | Mon | 44 | 41 | 45.9 | 40 | 53 |

4 2016 1 5 Tues 41 40 46.0 44 41

数据说明

  • temp_2 前天的最高温度值
  • temp_1 昨天的最高温度值
  • averate 历史上,每年这一天的平均最高温度值
  • actual 标签值,当天的真实的最高温度值
  • friend 朋友猜测的温度值,忽略

1.2 处理时间数据

python 复制代码
years = temperatures['year']
months = temperatures['month']
days = temperatures['day']

#转为datetime格式
temp_days = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in zip(years, months, days)]
temp_days = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in temp_days]
print(temp_days[:5])
[datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2016, 1, 2, 0, 0), datetime.datetime(2016, 1, 3, 0, 0), datetime.datetime(2016, 1, 4, 0, 0), datetime.datetime(2016, 1, 5, 0, 0)]

1.3 原始数据画图展示

python 复制代码
#画图风格
plt.style.use('fivethirtyeight')
#布局
fig,((plt1,plt2),(plt3,plt4)) = plt.subplots(nrows=2, ncols=2,figsize=(10,10))
fig.autofmt_xdate(rotation = 45)
#标签值
plt1.plot(temp_days, temperatures['actual'])
plt1.set_xlabel('')
plt1.set_ylabel('Temperature')
plt1.set_title('Max Temp')

plt2.plot(temp_days, temperatures['temp_1'])
plt2.set_xlabel('')
plt2.set_ylabel('Temperature')
plt2.set_title('Yesterday Max Temp')

plt3.plot(temp_days, temperatures['temp_2'])
plt3.set_xlabel('')
plt3.set_ylabel('Temperature')
plt3.set_title('The Day Before Yesterday Temp')

plt4.plot(temp_days, temperatures['friend'])
plt4.set_xlabel('')
plt4.set_ylabel('Temperature')
plt4.set_title('Friend Estimate')
Text(0.5, 1.0, 'Friend Estimate')

1.4 ont hot编码

python 复制代码
# one hot 编码
temperatures = pd.get_dummies(temperatures, dtype=int)
temperatures.head(5)

| | year | month | day | temp_2 | temp_1 | average | actual | friend | week_Fri | week_Mon | week_Sat | week_Sun | week_Thurs | week_Tues | week_Wed |
| 0 | 2016 | 1 | 1 | 45 | 45 | 45.6 | 45 | 29 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 2016 | 1 | 2 | 44 | 45 | 45.7 | 44 | 61 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
| 2 | 2016 | 1 | 3 | 45 | 44 | 45.8 | 41 | 56 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 3 | 2016 | 1 | 4 | 44 | 41 | 45.9 | 40 | 53 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |

4 2016 1 5 41 40 46.0 44 41 0 0 0 0 0 1 0

1.5转换格式

python 复制代码
labels = np.array(temperatures['actual'])
#去掉标签特征
temperatures = temperatures.drop('actual', axis=1)
temperatures_columns = list(temperatures.columns)
temperatures = np.array(temperatures)
print("temperatures shape", temperatures.shape)
#标准化
input_temperatures = preprocessing.StandardScaler().fit_transform(temperatures)
print(input_temperatures[0])
temperatures shape (348, 14)
[ 0.         -1.5678393  -1.65682171 -1.48452388 -1.49443549 -1.3470703
 -1.98891668  2.44131112 -0.40482045 -0.40961596 -0.40482045 -0.40482045
 -0.41913682 -0.40482045]

2.模型实现

2.1常用模型参数说明

  • activation: 激活函数,一般用relu
  • kernal_initializer,bias_initializer: 权重与偏置参数的初始化方法。有时不收敛可以换个初始化方法
  • kernel_regularize,bias_regularize: 是否加入正则化
  • inputs: 输入,自己指定或者网络自动选
  • units: 神经元个数

2.2 模型实现

python 复制代码
#添加模型隐层
model = tf.keras.Sequential()
model.add(layers.Dense(16))
model.add(layers.Dense(32))
model.add(layers.Dense(1))

#配置模型
model.compile(optimizer=tf.keras.optimizers.SGD(0.001),
             loss="mean_squared_error")

#训练
model.fit(input_temperatures, labels, validation_split=0.25, epochs=10, batch_size=64)
Epoch 1/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 30ms/step - loss: 4321.0967 - val_loss: 2869.1660
Epoch 2/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 2461.2610 - val_loss: 3275.8066
Epoch 3/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 159.0226 - val_loss: 2383.7563
Epoch 4/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 104.4375 - val_loss: 1969.7244
Epoch 5/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 563.5617 - val_loss: 1057.4402
Epoch 6/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 60.7969 - val_loss: 868.3708
Epoch 7/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 133.9573 - val_loss: 622.4547
Epoch 8/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 53.3111 - val_loss: 625.0286
Epoch 9/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 43.0903 - val_loss: 393.4365
Epoch 10/10
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 125.3164 - val_loss: 294.2254





<keras.src.callbacks.history.History at 0x24426001990>
python 复制代码
#模型 loss损失比较大,需要优化,降低loss
model.summary()
复制代码
Model: "sequential_1"
复制代码
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓
┃ Layer (type)                         ┃ Output Shape                ┃         Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩
│ dense_3 (Dense)                      │ (None, 16)                  │             240 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_4 (Dense)                      │ (None, 32)                  │             544 │
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤
│ dense_5 (Dense)                      │ (None, 1)                   │              33 │
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘
复制代码
 Total params: 819 (3.20 KB)
复制代码
 Trainable params: 817 (3.19 KB)
复制代码
 Non-trainable params: 0 (0.00 B)
复制代码
 Optimizer params: 2 (12.00 B)

2.3 模型优化

2.3.1 更改初始化方法,增加训练次数

python 复制代码
#更改初始化方法,训练次数从10次到100次
model = tf.keras.Sequential()
model.add(layers.Dense(16, kernel_initializer='random_normal'))
model.add(layers.Dense(32, kernel_initializer='random_normal'))
model.add(layers.Dense(1, kernel_initializer='random_normal'))

model.compile(optimizer=tf.keras.optimizers.SGD(0.001),
             loss='mean_squared_error')
model.fit(input_temperatures, labels, validation_split=0.25, epochs=100, batch_size=64)
Epoch 1/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 27ms/step - loss: 4341.7227 - val_loss: 2871.0271
Epoch 2/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 4269.7217 - val_loss: 2792.2722
Epoch 3/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 4259.1123 - val_loss: 2687.3899
Epoch 4/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 4066.6692 - val_loss: 2498.6802
Epoch 5/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 3462.5059 - val_loss: 2466.5791
Epoch 6/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 581.6287 - val_loss: 1843.6437
Epoch 7/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 90.2633 - val_loss: 1232.4553
Epoch 8/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 79.9981 - val_loss: 881.9412
Epoch 9/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 41.1886 - val_loss: 674.9135
Epoch 10/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 42.3882 - val_loss: 481.0506
Epoch 11/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 44.9377 - val_loss: 391.7140
Epoch 12/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 38.8932 - val_loss: 378.5648
Epoch 13/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 34.8383 - val_loss: 303.6538
Epoch 14/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 38.4258 - val_loss: 220.1120
Epoch 15/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 30.6331 - val_loss: 177.5809
Epoch 16/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 25.3459 - val_loss: 151.5260
Epoch 17/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 31.5611 - val_loss: 190.2681
Epoch 18/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 33.8859 - val_loss: 146.4823
Epoch 19/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 31.4742 - val_loss: 118.5758
Epoch 20/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 28.0468 - val_loss: 64.5958
Epoch 21/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 28.4419 - val_loss: 163.6804
Epoch 22/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 98.4275 - val_loss: 64.5287
Epoch 23/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 75.8126 - val_loss: 137.2301
Epoch 24/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 59.2577 - val_loss: 98.7127
Epoch 25/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - loss: 38.4441 - val_loss: 124.6540
Epoch 26/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 71.7989 - val_loss: 36.2800
Epoch 27/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 33.7251 - val_loss: 33.0628
Epoch 28/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 37.3204 - val_loss: 42.4540
Epoch 29/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 65.5749 - val_loss: 54.9257
Epoch 30/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 52.5856 - val_loss: 37.0988
Epoch 31/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 29.4801 - val_loss: 71.2360
Epoch 32/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 108.5365 - val_loss: 35.8163
Epoch 33/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 54.8995 - val_loss: 27.9930
Epoch 34/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.7711 - val_loss: 42.6019
Epoch 35/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 41.1184 - val_loss: 34.6733
Epoch 36/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 5ms/step - loss: 27.6766 - val_loss: 31.9773
Epoch 37/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 28.4529 - val_loss: 19.7460
Epoch 38/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 35.8389 - val_loss: 35.2612
Epoch 39/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 5ms/step - loss: 28.3160 - val_loss: 42.6479
Epoch 40/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 106.6452 - val_loss: 24.0267
Epoch 41/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 23.6601 - val_loss: 19.3733
Epoch 42/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 5ms/step - loss: 28.4145 - val_loss: 20.0657
Epoch 43/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 30.2289 - val_loss: 22.5946
Epoch 44/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 31.1034 - val_loss: 17.4016
Epoch 45/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 5ms/step - loss: 31.8360 - val_loss: 82.5532
Epoch 46/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 107.9492 - val_loss: 21.9323
Epoch 47/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 27.2920 - val_loss: 21.0179
Epoch 48/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 25.8970 - val_loss: 22.3299
Epoch 49/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.3085 - val_loss: 37.8563
Epoch 50/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 40.0604 - val_loss: 18.3307
Epoch 51/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 25.8000 - val_loss: 21.0737
Epoch 52/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 25.8438 - val_loss: 43.8438
Epoch 53/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 9ms/step - loss: 33.8743 - val_loss: 33.6241
Epoch 54/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 62.6123 - val_loss: 20.5208
Epoch 55/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 31.7541 - val_loss: 42.6262
Epoch 56/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 28.6584 - val_loss: 24.4605
Epoch 57/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 31.3697 - val_loss: 22.8914
Epoch 58/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.2317 - val_loss: 42.9988
Epoch 59/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 51.6604 - val_loss: 42.7188
Epoch 60/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 42.3546 - val_loss: 27.8267
Epoch 61/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 32.0020 - val_loss: 32.2499
Epoch 62/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 36.6703 - val_loss: 18.0478
Epoch 63/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 33.0255 - val_loss: 41.6100
Epoch 64/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 52.2897 - val_loss: 64.4359
Epoch 65/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 81.0884 - val_loss: 30.0917
Epoch 66/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 27.2542 - val_loss: 20.2524
Epoch 67/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 29.1652 - val_loss: 54.8341
Epoch 68/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 67.4783 - val_loss: 22.0776


Epoch 69/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 32.1612 - val_loss: 27.9906
Epoch 70/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 9ms/step - loss: 34.0457 - val_loss: 23.1129
Epoch 71/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 24.4963 - val_loss: 26.8693
Epoch 72/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 25.6235 - val_loss: 18.3104
Epoch 73/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.7223 - val_loss: 30.3030
Epoch 74/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 41.1724 - val_loss: 22.1834
Epoch 75/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 25.4155 - val_loss: 18.0785
Epoch 76/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 37.8443 - val_loss: 22.2262
Epoch 77/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.2023 - val_loss: 24.7404
Epoch 78/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 29.4313 - val_loss: 28.2004
Epoch 79/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 32.5010 - val_loss: 34.4478
Epoch 80/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 28.1699 - val_loss: 19.9688
Epoch 81/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 33.1135 - val_loss: 30.1685
Epoch 82/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 33.0771 - val_loss: 33.2978
Epoch 83/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 28.9822 - val_loss: 36.3891
Epoch 84/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 100.3202 - val_loss: 18.8064
Epoch 85/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.1813 - val_loss: 42.5619
Epoch 86/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 50.4812 - val_loss: 33.3420
Epoch 87/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 36.7241 - val_loss: 44.3068
Epoch 88/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 37.9590 - val_loss: 23.1378
Epoch 89/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 26.4826 - val_loss: 21.8839
Epoch 90/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 31.0949 - val_loss: 23.2860
Epoch 91/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 44.1823 - val_loss: 33.2644
Epoch 92/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 36.3967 - val_loss: 26.2339
Epoch 93/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 32.2672 - val_loss: 27.6349
Epoch 94/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 26.5576 - val_loss: 21.4126
Epoch 95/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.7782 - val_loss: 38.7811
Epoch 96/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 41.9597 - val_loss: 36.5568
Epoch 97/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 72.6424 - val_loss: 42.6189
Epoch 98/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 39.2912 - val_loss: 28.3757
Epoch 99/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 30.9735 - val_loss: 23.0405
Epoch 100/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 27.3346 - val_loss: 16.2927





<keras.src.callbacks.history.History at 0x24428576cd0>

验证集loss从294降到16.29

2.3.2 加入正则化惩罚项

python 复制代码
model = tf.keras.Sequential()
model.add(layers.Dense(16, kernel_initializer='random_normal', kernel_regularizer=tf.keras.regularizers.l2(0.03)))
model.add(layers.Dense(32, kernel_initializer='random_normal', kernel_regularizer=tf.keras.regularizers.l2(0.03)))
model.add(layers.Dense(1, kernel_initializer='random_normal', kernel_regularizer=tf.keras.regularizers.l2(0.03)))
model.compile(optimizer=tf.keras.optimizers.SGD(0.001),
             loss='mean_squared_error')
model.fit(input_temperatures, labels, validation_split=0.25, epochs=100, batch_size=64)
Epoch 1/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m1s[0m 31ms/step - loss: 4329.9487 - val_loss: 2871.9626
Epoch 2/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 4307.8306 - val_loss: 2789.5034
Epoch 3/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 4179.3872 - val_loss: 2676.4529
Epoch 4/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 4030.3188 - val_loss: 2476.9949
Epoch 5/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 3222.8850 - val_loss: 2195.1760
Epoch 6/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 263.0312 - val_loss: 1695.6211
Epoch 7/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 68.0448 - val_loss: 1369.0785
Epoch 8/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 54.3513 - val_loss: 1032.3079
Epoch 9/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 39.0651 - val_loss: 831.0013
Epoch 10/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 37.9228 - val_loss: 672.9735
Epoch 11/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 35.9693 - val_loss: 450.8900
Epoch 12/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 54.7455 - val_loss: 481.6994
Epoch 13/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 35.8592 - val_loss: 321.1757
Epoch 14/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 32.7608 - val_loss: 283.5308
Epoch 15/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 194.1068 - val_loss: 287.6764
Epoch 16/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 51.2676 - val_loss: 251.6622
Epoch 17/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 61.9756 - val_loss: 218.9387
Epoch 18/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 62.7216 - val_loss: 159.8604
Epoch 19/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 50.4749 - val_loss: 119.2429
Epoch 20/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 11ms/step - loss: 31.9662 - val_loss: 146.9869
Epoch 21/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 32.3689 - val_loss: 133.1835
Epoch 22/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 12ms/step - loss: 42.9962 - val_loss: 172.4340
Epoch 23/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 51.8156 - val_loss: 150.9075
Epoch 24/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 28.9330 - val_loss: 160.4308
Epoch 25/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 51.3750 - val_loss: 100.7717
Epoch 26/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 64.6685 - val_loss: 58.6360
Epoch 27/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 28.2559 - val_loss: 77.3267
Epoch 28/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 30.2373 - val_loss: 74.5407
Epoch 29/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 39.5093 - val_loss: 44.6559
Epoch 30/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 44.0398 - val_loss: 64.8625
Epoch 31/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 59.9598 - val_loss: 52.8985
Epoch 32/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 37.3139 - val_loss: 44.8724
Epoch 33/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 32.5650 - val_loss: 56.2387
Epoch 34/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 11ms/step - loss: 32.8806 - val_loss: 54.2845
Epoch 35/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 29.2826 - val_loss: 71.4181
Epoch 36/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 43.0233 - val_loss: 52.3515
Epoch 37/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 53.5211 - val_loss: 63.1684
Epoch 38/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 35.4584 - val_loss: 46.4926
Epoch 39/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 40.3554 - val_loss: 41.1063
Epoch 40/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.9332 - val_loss: 57.6292
Epoch 41/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 33.9667 - val_loss: 33.8120
Epoch 42/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 26.6114 - val_loss: 43.4933
Epoch 43/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 30.9945 - val_loss: 39.8859
Epoch 44/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 31.4712 - val_loss: 44.3439
Epoch 45/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 30.3691 - val_loss: 28.9878
Epoch 46/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 31.7159 - val_loss: 29.2529
Epoch 47/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 29.2444 - val_loss: 52.1457
Epoch 48/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 45.8093 - val_loss: 29.0608
Epoch 49/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 37.3103 - val_loss: 25.5083
Epoch 50/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.3065 - val_loss: 22.4198
Epoch 51/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 26.5020 - val_loss: 75.6966
Epoch 52/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 108.3367 - val_loss: 26.7037
Epoch 53/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.1255 - val_loss: 26.3644
Epoch 54/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 26.4255 - val_loss: 21.8528
Epoch 55/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 30.5198 - val_loss: 30.5720
Epoch 56/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 75.6487 - val_loss: 22.3380
Epoch 57/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 27.7026 - val_loss: 47.1917
Epoch 58/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 91.9721 - val_loss: 20.5868
Epoch 59/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 6ms/step - loss: 29.8157 - val_loss: 20.3234
Epoch 60/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 5ms/step - loss: 25.5071 - val_loss: 21.0482
Epoch 61/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 30.9963 - val_loss: 26.6644
Epoch 62/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 36.8300 - val_loss: 53.5873
Epoch 63/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 121.0377 - val_loss: 23.9243
Epoch 64/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.9519 - val_loss: 38.1576
Epoch 65/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 58.6323 - val_loss: 25.9526
Epoch 66/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 33.2245 - val_loss: 28.1261
Epoch 67/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 32.8570 - val_loss: 26.6537
Epoch 68/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 31.3758 - val_loss: 24.2550


Epoch 69/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 33.5456 - val_loss: 26.4282
Epoch 70/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 24.6720 - val_loss: 25.8680
Epoch 71/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 39.6415 - val_loss: 30.0976
Epoch 72/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 3ms/step - loss: 31.5295 - val_loss: 22.4324
Epoch 73/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 31.7014 - val_loss: 28.8245
Epoch 74/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 28.5994 - val_loss: 19.5470
Epoch 75/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 25.5453 - val_loss: 20.3241
Epoch 76/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.2719 - val_loss: 30.4617
Epoch 77/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 37.2063 - val_loss: 28.1975
Epoch 78/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 27.9328 - val_loss: 25.9083
Epoch 79/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 26.2157 - val_loss: 21.1073
Epoch 80/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 26.5412 - val_loss: 26.4064
Epoch 81/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 32.7264 - val_loss: 22.4093
Epoch 82/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 39.1839 - val_loss: 28.2290
Epoch 83/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 25.7481 - val_loss: 23.3083
Epoch 84/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 30.1190 - val_loss: 29.3318
Epoch 85/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 29.4902 - val_loss: 21.6280
Epoch 86/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 34.3956 - val_loss: 18.1713
Epoch 87/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 29.2233 - val_loss: 19.9103
Epoch 88/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 38.5457 - val_loss: 22.7399
Epoch 89/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 27.8864 - val_loss: 26.5859
Epoch 90/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 33.6624 - val_loss: 24.8328
Epoch 91/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 38.2470 - val_loss: 17.1834
Epoch 92/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 7ms/step - loss: 30.2064 - val_loss: 25.3695
Epoch 93/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 32.6475 - val_loss: 46.5841
Epoch 94/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 9ms/step - loss: 58.2158 - val_loss: 23.7842
Epoch 95/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 22.8116 - val_loss: 37.8584
Epoch 96/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 89.6664 - val_loss: 27.8412
Epoch 97/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 33.9813 - val_loss: 29.5030
Epoch 98/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 9ms/step - loss: 27.7755 - val_loss: 41.9249
Epoch 99/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 4ms/step - loss: 37.6500 - val_loss: 23.3591
Epoch 100/100
[1m5/5[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 8ms/step - loss: 30.2891 - val_loss: 20.3465





<keras.src.callbacks.history.History at 0x2442da3e610>

3.预测

3.1 预测结果

python 复制代码
predict_result = model.predict(input_temperatures)
print("predict result shape ",predict_result.shape)
print(predict_result[0:5])
[1m11/11[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m0s[0m 2ms/step 
predict result shape  (348, 1)
[[49.465336]
 [51.12283 ]
 [47.591335]
 [48.294342]
 [45.045162]]

3.2 展示预测结果

python 复制代码
#创建真实数据的展示
true_data = pd.DataFrame(data={'date': temp_days, 'actual': labels})

#解析日期,取出模型的预测值
predict_data = pd.DataFrame(data={'date': temp_days, 'prediction': predict_result.reshape(-1)})

plt.plot(true_data['date'], true_data['actual'], 'b-', label = 'actual')
plt.plot(predict_data['date'], predict_data['prediction'], 'ro', label = 'prediction')
plt.xticks(rotation = 60)
plt.legend()
plt.xlabel('Date')
plt.ylabel('Max Temperature')
plt.title('Actual And Predict Value')
Text(0.5, 1.0, 'Actual And Predict Value')
相关推荐
shymoy8 分钟前
Radix Sorts
数据结构·算法·排序算法
小嗷犬15 分钟前
【论文笔记】VCoder: Versatile Vision Encoders for Multimodal Large Language Models
论文阅读·人工智能·语言模型·大模型·多模态
风影小子16 分钟前
注册登录学生管理系统小项目
算法
黑龙江亿林等保18 分钟前
深入探索哈尔滨二级等保下的负载均衡SLB及其核心算法
运维·算法·负载均衡
Struart_R20 分钟前
LVSM: A LARGE VIEW SYNTHESIS MODEL WITH MINIMAL 3D INDUCTIVE BIAS 论文解读
人工智能·3d·transformer·三维重建
lucy1530275107921 分钟前
【青牛科技】GC5931:工业风扇驱动芯片的卓越替代者
人工智能·科技·单片机·嵌入式硬件·算法·机器学习
杜杜的man37 分钟前
【go从零单排】迭代器(Iterators)
开发语言·算法·golang
幻风_huanfeng1 小时前
线性代数中的核心数学知识
人工智能·机器学习
小沈熬夜秃头中୧⍤⃝1 小时前
【贪心算法】No.1---贪心算法(1)
算法·贪心算法
volcanical1 小时前
LangGPT结构化提示词编写实践
人工智能