我正在使用LSTM进行分类。目前有两个标签。 在将标签从int更改为一个热编码器之前,我的模型运行良好。
旧代码(有效):
if i%2 == 0 :
targets=targets.append({'seq_id':counter,'val':1},ignore_index=True)
else :
targets=targets.append({'seq_id':counter,'val':-1},ignore_index=True)
...
model.add(LSTM(int(num_features*4),input_shape=(num_rows,num_cols), return_sequences=True))
model.add(LSTM(int(num_features*4), dropout=0.5, recurrent_dropout=0.5))
model.add(Dense(1, activation='sigmoid'))
新代码(不起作用-停留在val_acc = 0.5)
if i%2 == 0 :
targets=targets.append({'seq_id':counter,'val':'bic'},ignore_index=True)
else :
targets=targets.append({'seq_id':counter,'val':'reno'},ignore_index=True)
...
targets = targets.values[:,1]
encoder = LabelEncoder()
encoder.fit(targets)
encoded_Y = encoder.transform(targets)
targets = np_utils.to_categorical(encoded_Y)
...
model.add(LSTM(int(num_features*4),input_shape=(num_rows,num_cols), return_sequences=True))
model.add(LSTM(int(num_features*4), dropout=0.5, recurrent_dropout=0.5))
model.add(Dense(2, activation='sigmoid')) #That line Changed
答案 0 :(得分:0)
如果您要进行一次热编码,则应将最后一个Dense层的激活更改为softmax。
答案 1 :(得分:0)
我将损失函数更改为categorical_crossentropy,它起作用了。