# Correct way to create a logistic regression NN

**URL:** <https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963>\
**Category:** Advanced Learning Algorithms\
**Tags:** week-module-2\
**Created:** [September 16, 2023, 11:51am UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963 "2023-09-16T11:51:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Misra1](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/amit_misra1/32/48206_2.png) [@Amit\_Misra1](https://community.deeplearning.ai/u/Amit_Misra1)\
**Post date:** [September 16, 2023, 11:51am UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963/1 "2023-09-16T11:51:30Z")

</div>

Would it be correct that the following implementation of logistic regression is better than just specifying “sigmoid” as the activation function for the output layer as Dr. Ng said?

 ![image](https://global.discourse-cdn.com/dlai/original/3X/b/2/b2b81e24061a744c18a5f805546512b9452d0fa0.jpeg)

---

<div class="post-metadata">

**Author:** ![Amit\_Misra1](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/amit_misra1/32/48206_2.png) [@Amit\_Misra1](https://community.deeplearning.ai/u/Amit_Misra1)\
**Post date:** [September 16, 2023, 11:52am UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963/2 "2023-09-16T11:52:04Z")

</div>

If so, are there any situations where using the Sigmoid activation function would be advantageous?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [September 16, 2023, 3:48pm UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963/3 "2023-09-16T15:48:45Z")

</div>

It depends on what you mean by “better”.

Andrew recommends using a linear output with “from\_logits = True” when you have multiple labels, because it does a couple of things.

- Automatically applies softmax.
- It has some mathematical or computational efficiencies.

> [@Amit\_Misra1](#):
>
> are there any situations where using the Sigmoid activation function would be advantageous

Yes. For one, when you have only two labels or a true/false result.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [September 16, 2023, 7:55pm UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963/4 "2023-09-16T19:55:21Z")

</div>

One other point to make here: just to be accurate, the network you have implemented is _not_ Logistic Regression. It is a Fully Connected network with 3 layers which does binary classification. Logistic Regression is essentially a trivial Neural Network with only the “output” layer and does binary classification.

I would also state the case differently: you’re using “sigmoid” at the output layer either way. It’s just a question of whether you explicitly include the “sigmoid” activation or whether you let it be handled internally within the cross entropy loss function (the `from_logits = True` mode). Of course if you don’t explicitly add the “sigmoid” in the output layer, then you also have to add it explicitly in your “predict” logic.

So maybe you could argue that the one case in which explicitly adding “sigmoid” in the output layer is better is that it makes your predict logic simpler, if that goal is more important to you than the improved numerical accuracy gained by the other method. You could also try it both ways in a given case to see if the predictions are actually affected by any accuracy differences. It’s possible that in any given case it ends up not mattering that much to the results of training.

[Here’s a thread](https://community.deeplearning.ai/t/week-3-assignment-compute-total-loss-try-to-set-from-logits-false/243049/2) which discusses why the `from_logits = True` method is preferred. [Here’s a thread from Raymond](https://community.deeplearning.ai/t/why-is-from-logits-true-plus-activation-linear-more-stable/431907) that goes into some depth in showing why the latter method is more accurate.

---

<div class="post-metadata">

**Author:** ![Amit\_Misra1](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/amit_misra1/32/48206_2.png) [@Amit\_Misra1](https://community.deeplearning.ai/u/Amit_Misra1)\
**Post date:** [September 17, 2023, 9:25pm UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963/5 "2023-09-17T21:25:40Z")

</div>

> [@paulinpaloalto](#):
>
> So maybe you could argue that the one case in which explicitly adding “sigmoid” in the output layer is better is that it makes your predict logic simpler

Thanks Paul. Is it correct to say that if performing binary classification, _sigmoid_ needs to be specified in the output layer or the predict line of code? And utilizing the latter with a linear output layer, it’s more numerically stable?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [September 17, 2023, 10:25pm UTC](https://community.deeplearning.ai/t/correct-way-to-create-a-logistic-regression-nn/446963/6 "2023-09-17T22:25:14Z")

</div>

> [@Amit\_Misra1](#):
>
> if performing binary classification, _sigmoid_ needs to be specified in the output layer or the predict line of code?

In the general simple case, yes to both.

> [@Amit\_Misra1](#):
>
> And utilizing the latter with a linear output layer, it’s more numerically stable?

More stable than what?

The stability argument only applies for TensorFlow and an NN and the situation where you might think about using softmax() in the output layer.
