Member-only story
How to Implement Logistic Regression with TensorFlow
…something not as hard as you may think
TL; DR
If you are here for a quick solution that just works, then here it is in just 5 lines of code:
model = tf.keras.models.Sequential([ tf.keras.layers.Dense(1, activation='sigmoid')])model.compile(loss='bce')model.fit(x_train, y_train, epochs=100)
The long way
Now, if you’re still with me it means that you don’t want just to copy + paste 5 lines of code, but to see how you can actually implement this method yourself from scratch.
TensorFlow is a rich library; it has many APIs that you can use. Among them is the Keras API which can be used to build a logistic regression model very quickly, as you can see above. And there’s nothing wrong with that. If you have to implement a complex deep learning model, that perhaps you saw in a new paper, Keras saves you a lot of time; it lets you focus on what’s important, and don’t have to care about each math operation that has to be done.
But, if your purpose is to learn a basic machine learning technique, like logistic regression, it is worth it using the core math functions…