Hands-on guide to solving real-world Machine Learning problems with Scikit-Learn, TensorFlow 2, and Keras
This book is for sale at http://leanpub.com/Hackers-Guide-to-Machine-Learning-with-Python
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
TensorFlow 2 and Keras - Quick Start Guide
TL;DR Learn how to use Tensors, build a Linear Regression model and a simple Neural Network
TensorFlow 2.0 (final) was released at the end of September. Oh boy, it looks much cooler than the 1.x series. Why is it so much better for you, the developer?
- One high-level API for building models (that you know and love) - Keras. The good news is that most of your old Keras code should work automagically after changing a couple of imports.
- Eager execution - all your code looks much more like normal Python programs. Old-timers might remember the horrible
Session
experiences. You shouldnt need any of that, in day-to-day use.
There are tons of other improvements, but the new developer experience is something that will make using TensorFlow 2 sweeter. What about PyTorch? PyTorch is still great and easy to use. But it seems like TensorFlow is catching up, or is it?
Youll learn:
- How to install TensorFlow 2
- What is a Tensor
- Doing Tensor math
- Using probability distributions and sampling
- Build a Simple Linear Regression model
- Build a Simple Neural Network model
- Save/restore a model
Run the complete code in your browser
Setup
Lets install the GPU-supported version and set up the environment:
1
!pip install tensorflow-gpu
Check the installed version:
1
import
tensorflow
as
tf
2
3
tf
.
__version__
1
2
.0.0
And specify a random seed, so our results are reproducible:
1
RANDOM_SEED
=
42
2
3
tf
.
random
.
set_seed
(
RANDOM_SEED
)
Tensors
TensorFlow allows you to define and run operations on Tensors. Tensors are data-containers that can be of arbitrary dimension - scalars, vectors, matrices, etc. You can put numbers (floats and ints) and strings into Tensors.
Lets create a simple Tensor:
1
x
=
tf
.
constant
(
1
)
2
print
(
x
)
1
tf.Tensor(
1
, shape
=()
, dtype
=
int32)
It seems like our first Tensor contains the number 1, it is of type int32 and is shapeless. To obtain the value we can do:
1
x
.
numpy
()
1
1
Lets create a simple matrix:
1
m
=
tf
.
constant
([[
1
,
2
,
1
],
[
3
,
4
,
2
]])
2
print
(
m
)
1
tf.Tensor(
2
[[
1
2
1
]
3
[
3
4
2
]]
, shape
=(
2
, 3
)
, dtype
=
int32)
This shape thingy seems to specify rows x columns. In general, the shape array shows how many elements are in every dimension of the Tensor.
Helpers
TensorFlow offers a variety of helper functions for creating Tensors. Lets create a matrix full of ones:
1
ones
=
tf
.
ones
([
3
,
3
])
2
print
(
ones
)
1
tf.Tensor(
2
[[
1
. 1
. 1
.]
3
[
1
. 1
. 1
.]
4
[
1
. 1
. 1
.]]
, shape
=(
3
, 3
)
, dtype
=
float32)
and zeros:
1
zeros
=
tf
.
zeros
([
2
,
3
])
2
print
(
zeros
)
1
tf.Tensor(
2
[[
0
. 0
. 0
.]
3
[
0
. 0
. 0
.]]
, shape
=(
2
, 3
)
, dtype
=
float32)
We have two rows and three columns. What if we want to turn it into three rows and two columns:
1
tf
.
reshape
(
zeros
,
[
3
,
2
])
1
tf.Tensor(
2
[[
0
. 0
.]
3
[
0
. 0
.]
4
[
0
. 0
.]]
, shape
=(
3
, 2
)
, dtype
=
float32)
You can use another helper function to replace rows and columns (transpose):
1
tf
.
transpose
(
zeros
)
1
tf.Tensor(
2
[[
0
. 0
.]
3
[
0
. 0
.]
4
[
0
. 0
.]]
, shape
=(
3
, 2
)
, dtype
=
float32)
Tensor Math
Naturally, you would want to do something with your data. Lets start with adding numbers:
1
a
=