Answers.Body: <p>Yes, the values over 0.5 mean the output should have &quot;1&quot; label.</p>
  <p>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 <code>0.8</code>, you can minus the output then clamp the minimum cap of it to 0. {out = max(out-(thresh-0.5), 0)}</p>
  <p>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 <a href="https://www.tensorflow.org/api_docs/python/tf/keras/losses/BinaryCrossentropy" rel="nofollow noreferrer">Keras documents</a>) can be a good start.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-01T05:39:46.693Z
Answers.Text: 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?
Answers.UserDisplayName: Allen Ye
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-01T05:47:47.573Z
Answers.Text: maybe in this case. you can try it, by the method I said above
Answers.UserDisplayName: CuCaRot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-01T04:35:17.233Z
Answers.LastActivityDate: 2022-04-01T04:35:17.233Z
Answers.UserDisplayName: CuCaRot
Body: <p>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.</p>
  <pre><code>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 = &quot;binary_crossentropy&quot;, optimizer = opt, metrics=['accuracy'])
  model.summary()
  </code></pre>
  <p>An example of the outputs is</p>
  <pre><code>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]])
  </code></pre>
  <p>Do the values over 0.5 mean my model classified the data as a &quot;1&quot; 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?</p>
  
Tags: <deep-learning><classification><long-short-term-memory><keras><sigmoid>
Title: Do the values over 0.5 mean my model classified the data as a "1" label and vice versa?
UserDisplayName: Allen Ye
ThreadText: # Do the values over 0.5 mean my model classified the data as a "1" label and vice versa?
  
  (Asked by: username_0 on 2022-04-01)
  
  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?
  
  **Tags:** deep-learning, classification, long-short-term-memory, keras, sigmoid
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-04-01. Score: 1 | Accepted answer
  
  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](https://www.tensorflow.org/api_docs/python/tf/keras/losses/BinaryCrossentropy)) can be a good start.
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_1**: maybe in this case. you can try it, by the method I said above  
  
  ---
  
  