- 用于回归的RNN模型,参见Chollet,Python深度学习,6.3.1温度预测问题
- 在这个例子中,我使用了随机数据,包括回归和回归
- 我使用了平均绝对误差作为损失函数和度量
- 我不理解val_loss和val_mean_absolute_error的值。它们对我来说都没有意义
代码:
import tensorflow as tf
import numpy as np
from keras.models import Sequential
from keras import layers
from keras.optimizers import Adam
import keras
我使用随机输入数据:
data_np = np.random.rand(6400,10)
target_np = np.random.rand(6400,)
规范化数据:
mean1 = data_np[:].mean(axis=0)
std1 = data_np[:].std(axis=0)
data_np -= mean1
data_np /= std1
mean2 = target_np.mean(axis=0)
std2 = target_np.std(axis=0)
target_np -= mean2
target_np /= std2
使用回顾创建RNN输入:
lookback = 7
train_data = np.array([data_np[(i-lookback):i,:] for i in range(lookback,len(data_np))])
target_data = target_np[lookback:len(data_np)]
然后设置一个简单的RNN:
model = Sequential()
model.add(layers.SimpleRNN(64,
activation = 'relu',
return_sequences=False,
input_shape=(train_data.shape[1], train_data.shape[2])))
model.add(layers.Dense(1))
opt = Adam(learning_rate=0.1)
mae = tensorflow.keras.losses.MeanAbsoluteError()
model.compile(optimizer=opt, loss=mae, metrics=[mae])
history = model.fit(train_data, target_data,
steps_per_epoch=round(0.7*len(train_data))//64,
epochs=10,
shuffle=False,
validation_split=0.3,
validation_steps = round(0.3*len(train_data))//64,
verbose=1)
输出如下:
对3495个样本进行训练,对1498个样本进行验证Epoch 1/10 54/54====================================]-2s 38ms/步-损耗:0.7955-mean_absolute_error:0.7955-val_loss:0.0428-val_mean_absolute_error:22.6301 Epoch 2/10 54/54====================================]-2s 30ms/步-损耗:0.7152-mean_absolute_error:0.7152-val_loss:0.0421-val_mean_absolute_error:22.2968
我希望val_loss和val_mean_absolute_error相同。此外,这些级别也没有多大意义。经过10个时代,我得到
Epoch 10/10 54/54〔=========================〕-2s 32ms/步-损失:0.7747-平均绝对误差:0.7747-有效损耗:0.0409-val_mean_absolute_error:21.6337
如果我手动计算平均绝对误差:
N=len(data_np)
val_data = np.array([data_np[(i-lookback):i,:] for i in range(round(0.7*N),N)])
val_target = target_np[round(0.7*N):N]
model_output = model.predict(val_data)
model_output=[output[0] for output in model_output]
np.mean(abs(model_output-val_target))
0.940300949276649
这看起来是一个可以预料的结果。然而,它甚至不接近val_loss或val_mean_absolute_error。这里怎么了?
好。我通过持续使用tensorflow.keras解决了这个问题
import tensorflow as tf
import numpy as np
from keras.models import Sequential
from keras import layers
from keras.optimizers import Adam
import keras
带有
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.optimizers import Adam
import tensorflow.keras
(并更正了原问题中的一些细节(