0
$\begingroup$

I am doing binary classification using an LSTM and my output layer is 1 neuron with a sigmoid function. My labels are either 0 or 1.

from tensorflow.keras.optimizers import SGD
model = Sequential()
model.add(LSTM(64,activation='sigmoid', input_shape=(PERCENT_DATA,1)))
model.add(Dropout(0.2))
model.add(Dense(1,activation='sigmoid'))
opt = SGD(lr=0.001)
model.compile(loss = "binary_crossentropy", optimizer = opt, metrics=['accuracy'])
model.summary()

An example of the outputs is

array([[0.9854203 ],
       [0.94532275],
       [0.946043  ],
       [0.5212766 ],
       [0.45969874],
       [0.53517556],
       [0.88838553],
       [0.05345109],
       [0.06565621],
       [0.5552153 ],
       [0.07443756],
       [0.62434113]])

Do the values over 0.5 mean my model classified the data as a "1" label and vice versa? Or should I try to find an optimal threshold from these values that will result in the highest accuracy? Or do these values mean something else like the confidence?

$\endgroup$

1 Answer 1

1
$\begingroup$

Yes, the values over 0.5 mean the output should have "1" label.

As I know with Keras you cannot set the optimal threshold, but you still can use the trick if you want, for example, the expected threshold is 0.8, you can minus the output then clamp the minimum cap of it to 0. {out = max(out-(thresh-0.5), 0)}

However, finding the optimal threshold for a model is not recommended in a real project since it may adapt to your validation set only, try to work more with data. I am not familiar with NLP data, but I am sure there are some methods to make data more variant. Label smoothing (as Keras documents) can be a good start.

$\endgroup$
2
  • $\begingroup$ Thanks for your help. I found that 0.5 isn't a good threshold for predictions, and have calculated a better one by testing other decimal numbers. Would this work? $\endgroup$
    – Allen Ye
    Apr 1, 2022 at 5:39
  • $\begingroup$ maybe in this case. you can try it, by the method I said above $\endgroup$
    – CuCaRot
    Apr 1, 2022 at 5:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .