Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Outputarray([[0.5504593 ],[0.94999564],[0.9757515 ],[0.22519748]], dtype=float32)Now we’re talking! These are the probabilities, given our model, of those fourpoints being positive examples.Lastly, we need to go from probabilities to classes. If the probability is greater thanor equal to a threshold, it is a positive example. If it is less than the threshold, it is anegative example. Simple enough. The trivial choice of a threshold is 0.5:Equation 3.19 - From probabilities to classesBut the probability itself is just the sigmoid function applied to the logit (z):Equation 3.20 - From logits to classes, via sigmoid functionBut the sigmoid function has a value of 0.5 only when the logit (z) has a value ofzero:Equation 3.21 - From logits to classes, directlyThus, if we don’t care about the probabilities, we could use the predictions (logits)directly to get the predicted classes for the data points:Model Training | 235

Making Predictions (Classes)classes = (predictions >= 0).astype(np.int)classesOutputarray([[1],[1],[1],[0]])Clearly, the points where the logits (z) equal zero determine the boundarybetween positive and negative examples."Why 0.5? Can I choose a different threshold?"Sure, you can! Different thresholds will give you different confusion matrices and,therefore, different metrics, like accuracy, precision, and recall. We’ll get back tothat in the "Decision Boundary" section.By the way, are you still holding that thought about the "glorified linear regression?"Good!Decision BoundaryWe have just figured out that whenever z equals zero, we are in the decisionboundary. But z is given by a linear combination of features x 1 and x 2 . If we workout some basic operations, we arrive at:Equation 3.22 - Decision boundary for logistic regression with two featuresGiven our model (b, w 1 , and w 2 ), for any value of the first feature (x 1 ), we cancompute the corresponding value of the second feature (x 2 ) that sits exactly at the236 | Chapter 3: A Simple Classification Problem

Output

array([[0.5504593 ],

[0.94999564],

[0.9757515 ],

[0.22519748]], dtype=float32)

Now we’re talking! These are the probabilities, given our model, of those four

points being positive examples.

Lastly, we need to go from probabilities to classes. If the probability is greater than

or equal to a threshold, it is a positive example. If it is less than the threshold, it is a

negative example. Simple enough. The trivial choice of a threshold is 0.5:

Equation 3.19 - From probabilities to classes

But the probability itself is just the sigmoid function applied to the logit (z):

Equation 3.20 - From logits to classes, via sigmoid function

But the sigmoid function has a value of 0.5 only when the logit (z) has a value of

zero:

Equation 3.21 - From logits to classes, directly

Thus, if we don’t care about the probabilities, we could use the predictions (logits)

directly to get the predicted classes for the data points:

Model Training | 235

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!