当前,我正在使用LSTM进行回归。我正在使用较小的批处理量,并且具有相当大的时间步长(但比我拥有的时间步长少得多)。
我正在尝试以较少的时间步长过渡到较大的批次,但启用了有状态,以允许使用大量的生成的训练数据。
但是,我目前使用的是基于sqrt(timestep)的正则化,(经过消融测试,有助于收敛速度,由于问题的统计性质,它可以正常工作,预期误差减少了sqrt(时间步长))。通过使用tf.range
来生成损失函数中适当大小的列表来执行此操作。启用有状态功能后,这种方法将是不正确的,因为它将计数错误的时间步数(此批次中的时间步数,而不是到目前为止看到的全部时间)。
是否可以将偏移量或整数列表或浮点数传递给损失函数?最好不要修改模型,但我知道可能需要这种性质的破解。
def create_model():
inputs = Input(shape=(None,input_nodes))
next_input = inputs
for i in range(dense_layers):
dense = TimeDistributed(Dense(units=dense_nodes,
activation='relu',
kernel_regularizer=l2(regularization_weight),
activity_regularizer=l2(regularization_weight)))\
(next_input)
next_input = TimeDistributed(Dropout(dropout_dense))(dense)
for i in range(lstm_layers):
prev_input = next_input
next_input = LSTM(units=lstm_nodes,
dropout=dropout_lstm,
recurrent_dropout=dropout_lstm,
kernel_regularizer=l2(regularization_weight),
recurrent_regularizer=l2(regularization_weight),
activity_regularizer=l2(regularization_weight),
stateful=True,
return_sequences=True)\
(prev_input)
next_input = add([prev_input, next_input])
outputs = TimeDistributed(Dense(output_nodes,
kernel_regularizer=l2(regularization_weight),
activity_regularizer=l2(regularization_weight)))\
(next_input)
model = Model(inputs=inputs, outputs=outputs)
def loss_function(y_true, y_pred):
length = K.shape(y_pred)[1]
seq = K.ones(shape=(length,))
if use_sqrt_loss_scaling:
seq = tf.range(1, length+1, dtype='int32')
seq = K.sqrt(tf.cast(seq, tf.float32))
seq = K.reshape(seq, (-1, 1))
if separate_theta_phi:
angle_loss = phi_loss_weight * phi_metric(y_true, y_pred, angle_loss_fun)
angle_loss += theta_loss_weight * theta_metric(y_true, y_pred, angle_loss_fun)
else:
angle_loss = angle_loss_weight * total_angle_metric(y_true, y_pred, angle_loss_fun)
norm_loss = norm_loss_weight * norm_loss_fun(y_true, y_pred)
energy_loss = energy_loss_weight * energy_metric(y_true, y_pred)
stability_loss = stability_loss_weight * stab_loss_fun(y_true, y_pred)
act_loss = act_loss_weight * act_loss_fun(y_true, y_pred)
return K.sum(K.dot(0
+ angle_loss
+ norm_loss
+ energy_loss
+ stability_loss
+ act_loss
, seq))
(计算损失函数的函数不应该与超级骗子相关。简单地说,它们也是损失函数。)
答案 0 :(得分:1)
为此,您可以使用sample_weight
方法的fit
参数并将sample_weight_mode='temporal'
传递给compile
方法,以便可以为每个样本的每个时间步分配权重在批处理中:
model.compile(..., sample_weight_mode='temporal')
model.fit(..., sample_weight=sample_weight)
sample_weight
应该是形状为(num_samples, num_timesteps)
的数组。
请注意,如果使用输入数据生成器或Sequence
的实例,则需要将样本权重作为生成器或Sequence
实例中生成的元组/列表的第三个元素传递