Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
TensorFlow Machine Learning Projects

You're reading from   TensorFlow Machine Learning Projects Build 13 real-world projects with advanced numerical computations using the Python ecosystem

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789132212
Length 322 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Ankit Jain Ankit Jain
Author Profile Icon Ankit Jain
Ankit Jain
Dr. Amita Kapoor Dr. Amita Kapoor
Author Profile Icon Dr. Amita Kapoor
Dr. Amita Kapoor
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Overview of TensorFlow and Machine Learning FREE CHAPTER 2. Using Machine Learning to Detect Exoplanets in Outer Space 3. Sentiment Analysis in Your Browser Using TensorFlow.js 4. Digit Classification Using TensorFlow Lite 5. Speech to Text and Topic Extraction Using NLP 6. Predicting Stock Prices using Gaussian Process Regression 7. Credit Card Fraud Detection using Autoencoders 8. Generating Uncertainty in Traffic Signs Classifier Using Bayesian Neural Networks 9. Generating Matching Shoe Bags from Shoe Images Using DiscoGANs 10. Classifying Clothing Images using Capsule Networks 11. Making Quality Product Recommendations Using TensorFlow 12. Object Detection at a Large Scale with TensorFlow 13. Generating Book Scripts Using LSTMs 14. Playing Pacman Using Deep Reinforcement Learning 15. What is Next? 16. Other Books You May Enjoy

Logistic regression with Keras

Keras is a high-level library that is available as part of TensorFlow. In this section, we will rebuild the same model we built earlier with TensorFlow core with Keras:

  1. Keras takes data in a different format, and so we must first reformat the data using datasetslib:
x_train_im = mnist.load_images(x_train)

x_train_im, x_test_im = x_train_im / 255.0, x_test / 255.0

In the preceding code, we are loading the training images in memory before both the training and test images are scaled, which we do by dividing them by 255.

  1. Then, we build the model:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
  1. Compile the model with the sgd optimizer. Set the categorical entropy as the loss function and the accuracy as a metric to test the model:
model.compile(optimizer='sgd',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
  1. Train the model for 5 epochs with the training set of images and labels:
model.fit(x_train_im, y_train, epochs=5)

Epoch 1/5 60000/60000 [==============================] - 3s 45us/step - loss: 0.7874 - acc: 0.8095 Epoch 2/5 60000/60000 [==============================] - 3s 42us/step - loss: 0.4585 - acc: 0.8792 Epoch 3/5 60000/60000 [==============================] - 2s 42us/step - loss: 0.4049 - acc: 0.8909 Epoch 4/5 60000/60000 [==============================] - 3s 42us/step - loss: 0.3780 - acc: 0.8965 Epoch 5/5 60000/60000 [==============================] - 3s 42us/step - loss: 0.3610 - acc: 0.9012 10000/10000 [==============================] - 0s 24us/step
  1. Evaluate the model with the test data:
model.evaluate(x_test_im, nputil.argmax(y_test))

We get the following evaluation scores as output:

[0.33530342621803283, 0.9097]

Wow! Using Keras, we can achieve higher accuracy. We achieved approximately 90% accuracy. This is because Keras internally sets many optimal values for us so that we can quickly start building models.

To learn more about Keras and to look at more examples, refer to the book Mastering TensorFlow, from Packt Publications.
You have been reading a chapter from
TensorFlow Machine Learning Projects
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781789132212
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image