Keras:具有类权重的LSTM [英] Keras: LSTM with class weights
问题描述
我的问题与此问题密切相关,但也超出了此范围.
my question is quite closely related to this question but also goes beyond it.
我正在尝试在Keras其中
I am trying to implement the following LSTM in Keras where
- 时间步长为
nb_tsteps=10
- 输入要素的数量为
nb_feat=40
- 每个时间步的LSTM单元数为
120
- LSTM层之后是TimeDistributedDense层
- the number of timesteps be
nb_tsteps=10
- the number of input features is
nb_feat=40
- the number of LSTM cells at each time step is
120
- the LSTM layer is followed by TimeDistributedDense layers
根据上面提到的问题,我知道我必须将输入数据表示为
From the question referenced above I understand that I have to present the input data as
nb_samples, 10, 40
通过在形状为(5932720, 40)
的原始时间序列上滚动一个长度为nb_tsteps=10
的窗口来得到nb_samples
.因此,代码是
where I get nb_samples
by rolling a window of length nb_tsteps=10
across the original timeseries of shape (5932720, 40)
. The code is hence
model = Sequential()
model.add(LSTM(120, input_shape=(X_train.shape[1], X_train.shape[2]),
return_sequences=True, consume_less='gpu'))
model.add(TimeDistributed(Dense(50, activation='relu')))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(20, activation='relu')))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(10, activation='relu')))
model.add(Dropout(0.2))
model.add(TimeDistributed(Dense(3, activation='relu')))
model.add(TimeDistributed(Dense(1, activation='sigmoid')))
现在是我的问题(假设以上内容是正确的):
二进制响应(0/1)严重失衡,我需要将cw = {0: 1, 1: 25}
这样的class_weight
字典传递给model.fit()
.但是我得到一个异常class_weight not supported for 3+ dimensional targets
.这是因为我将响应数据显示为(nb_samples, 1, 1)
.如果将其整形为2D数组(nb_samples, 1)
,则会出现异常Error when checking model target: expected timedistributed_5 to have 3 dimensions, but got array with shape (5932720, 1)
.
Now to my question (assuming the above is correct so far):
The binary responses (0/1) are heavily imbalanced and I need to pass a class_weight
dictionary like cw = {0: 1, 1: 25}
to model.fit()
. However I get an exception class_weight not supported for 3+ dimensional targets
. This is because I present the response data as (nb_samples, 1, 1)
. If I reshape it into a 2D array (nb_samples, 1)
I get the exception Error when checking model target: expected timedistributed_5 to have 3 dimensions, but got array with shape (5932720, 1)
.
非常感谢您的帮助!
推荐答案
我认为您应该将sample_weight
与sample_weight_mode='temporal'
一起使用.
I think you should use sample_weight
with sample_weight_mode='temporal'
.
从Keras文档中:
sample_weight:用于训练样本的权重的数字数组,用于 用于缩放损失函数(仅在训练期间).你可以 通过与输入样本长度相同的平面(1D)Numpy数组 (权重和样本之间的1:1映射),或者在时间上 数据,您可以传递具有形状(样本,sequence_length)的2D数组, 对每个样本的每个时间步施加不同的权重.在这个 在这种情况下,您应确保在以下位置指定sample_weight_mode ="temporal" compile().
sample_weight: Numpy array of weights for the training samples, used for scaling the loss function (during training only). You can either pass a flat (1D) Numpy array with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array with shape (samples, sequence_length), to apply a different weight to every timestep of every sample. In this case you should make sure to specify sample_weight_mode="temporal" in compile().
在您的情况下,您需要提供形状与标签相同的2D阵列.
In your case you would need to supply a 2D array with the same shape as your labels.
这篇关于Keras:具有类权重的LSTM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!