Baby Steps to Machine Learning
With TensorFlow 2.0
What is Machine Learning?
Machine Learning is learning from examples.
Formulate a hypothesis
Goal
We need data ...
import pandas as pd
import numpy as np
train_df = pd.read_csv('/content/gdrive/My Drive/boston/train.csv',
index_col='ID')
train_df.head()
train_df[['rm', 'medv']].head()
Life without TF 2.0
y = train_df['medv'].values
train_df['constant'] = 1
columns = ['constant', 'rm', 'zn', 'indus']
x = train_df[columns].values
w = np.zeros((x.shape[1], 1))
y_pred = np.dot(x, w)
Measure the quality of
prediction
error = y - y_pred
print(error.shape)
squared_error = np.power(error, 2)
root_mean_squared_error = sqrt(squared_error.sum()) / y_pred.shape[0]
print(root_mean_squared_error)
Improve prediction quality
costs = []
w_0_s = []
w_1_s = []
learning_rate = 1e-3
steps = 20
for a in range(steps):
w_0 = w[0][0]
w_1 = w[1][0]
# make prediction
y_pred = np.dot(x, w)
error = y - y_pred
error_squared = np.power(error, 2)
# cost function is Least Mean Squares
LMS = error_squared.sum() / (2 * y.shape[0])
costs.append(LMS)
w_0_s.append(w_0)
w_1_s.append(w_1)
# update
w_0 = w_0 + learning_rate/y.shape[0] * error.sum()
w_1 = w_1 + learning_rate/y.shape[0] * (error * x[1]).sum()
w[0][0] = w_0
w[1][0] = w_1
cost_df = pd.DataFrame({'cost': pd.Series(costs), 'w_0':
pd.Series(w_0_s), 'w_1': pd.Series(w_1_s)})
cost_df['cost'].plot()
Did the model improve?
_w = [w_0, w_1]
_w = np.asarray(_w)
_x = train_df[['constant', 'rm']].values
y_pred = np.dot(_x, _w)
_p = pd.DataFrame(dict(actual=train_df['medv'].values,
predicted=y_pred.reshape(-1)))
_p.head()
That was a lot of work for
something basic
class Model(object):
def __init__(self):
self.W = None
self.b = None
def __call__(self, x):
if self.W == None:
self.W = tf.Variable(tf.random.normal(shape=(1, x.shape[1])))
if self.b == None:
self.b = tf.Variable(tf.random.normal(shape=(x.shape[0], 1)))
return tf.matmul(x, self.W, transpose_b=True) + self.b
model = Model()
output = model(tf.constant([3.0, 3.1, 1.9, 2.0, 2.5, 2.9],
shape=(3,2)))
print(output)
@tf.function
def loss(y_pred, y):
return tf.reduce_mean(tf.square(y-y_pred))
This is the interesting part
def train(model, x, y, alpha):
x = tf.convert_to_tensor(x, np.float32)
y = tf.convert_to_tensor(y, np.float32)
with tf.GradientTape() as t:
t.watch(x)
current_loss = loss(model(x), y)
#print(current_loss)
dW, db = t.gradient(current_loss, [model.W, model.b])
#print(dW, db)
model.W.assign_sub(alpha * dW)
model.b.assign_sub(alpha * db)
train_df = df.sample(frac=0.8,random_state=0)
test_df = df.drop(train_df.index)
columns = ['nox', 'rm', 'chas', 'dis', 'ptratio', 'lstat', 'rad']
X_train = train_df[columns].values
X_test = test_df[columns].values
y_train = train_df[['medv']].values
y_test = test_df[['medv']].values
epochs = 10
model = Model()
for i in range(epochs):
train(model, X_train, y_train, alpha=0.1)
print(model.W)
Could it be simpler?
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential([
keras.layers.Dense(50, input_shape=(7,), activation='relu'),
keras.layers.Dense(50, activation='relu'),
keras.layers.Dense(50, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(1)
])
print(model.summary())
adam = keras.optimizers.Adam(0.001)
model.compile(optimizer=adam, loss='mse')
model.fit(X_train, y_train, epochs=2000, validation_split=0.1)
Let’s Engineer More Features
# we need a new way of getting data into the model
def df_to_dataset(df, columns, shuffle=True, batch_size=64):
df = df.copy()
labels = df.pop('medv')
features_df = df[columns]
ds = tf.data.Dataset.from_tensor_slices( (dict(features_df), labels) )
if shuffle:
ds = ds.shuffle(buffer_size=len(df))
ds = ds.batch(batch_size)
return ds
from sklearn.model_selection import train_test_split
train, val = train_test_split(df, test_size=0.1)
train_ds = df_to_dataset(train, columns)
val_ds = df_to_dataset(val, columns)
feature_columns = []
# numeric columns
for _col in columns:
feature_columns.append(tf.feature_column.numeric_column(_col))
# bucketize number of rooms
rm_buckets =
tf.feature_column.bucketized_column(tf.feature_column.numeric_column('rm
'), boundaries=[1, 2, 3, 4, 5, 6, 7, 8, 9])
rad_buckets =
tf.feature_column.bucketized_column(tf.feature_column.numeric_column('rad
'), boundaries=[1, 5, 10])
nox_buckets =
tf.feature_column.bucketized_column(tf.feature_column.numeric_column('nox
'), boundaries=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
feature_columns.append(rm_buckets)
feature_columns.append(rad_buckets)
feature_columns.append(nox_buckets)
featuresLayer = keras.layers.DenseFeatures(feature_columns)
model = keras.Sequential([
featuresLayer,
keras.layers.Dense(50, activation='relu'),
keras.layers.Dense(50, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(1)
])
model.compile(optimizer='adam', loss='mse')
model.fit(train_ds, epochs=50, validation_data=val_ds)
Neural Networks and Deep Learning
http://neuralnetworksanddeeplearning.com/
Learn More
Machine Learning Crash Course
https://developers.google.com/machine-lear
ning/crash-course
TensorFlow
https://www.tensorflow.org/
Robert John
@robert_thas
GDE: ML & Cloud
Baby Steps to Machine Learning

Baby Steps to Machine Learning at DevFest Lagos 2019

  • 1.
    Baby Steps toMachine Learning With TensorFlow 2.0
  • 2.
    What is MachineLearning? Machine Learning is learning from examples.
  • 3.
  • 4.
  • 5.
    import pandas aspd import numpy as np train_df = pd.read_csv('/content/gdrive/My Drive/boston/train.csv', index_col='ID') train_df.head()
  • 7.
  • 8.
  • 9.
    y = train_df['medv'].values train_df['constant']= 1 columns = ['constant', 'rm', 'zn', 'indus'] x = train_df[columns].values w = np.zeros((x.shape[1], 1)) y_pred = np.dot(x, w)
  • 10.
    Measure the qualityof prediction
  • 11.
    error = y- y_pred print(error.shape) squared_error = np.power(error, 2) root_mean_squared_error = sqrt(squared_error.sum()) / y_pred.shape[0] print(root_mean_squared_error)
  • 12.
  • 13.
    costs = [] w_0_s= [] w_1_s = [] learning_rate = 1e-3 steps = 20
  • 14.
    for a inrange(steps): w_0 = w[0][0] w_1 = w[1][0] # make prediction y_pred = np.dot(x, w) error = y - y_pred error_squared = np.power(error, 2)
  • 15.
    # cost functionis Least Mean Squares LMS = error_squared.sum() / (2 * y.shape[0]) costs.append(LMS) w_0_s.append(w_0) w_1_s.append(w_1) # update w_0 = w_0 + learning_rate/y.shape[0] * error.sum() w_1 = w_1 + learning_rate/y.shape[0] * (error * x[1]).sum() w[0][0] = w_0 w[1][0] = w_1
  • 16.
    cost_df = pd.DataFrame({'cost':pd.Series(costs), 'w_0': pd.Series(w_0_s), 'w_1': pd.Series(w_1_s)}) cost_df['cost'].plot()
  • 17.
    Did the modelimprove?
  • 18.
    _w = [w_0,w_1] _w = np.asarray(_w) _x = train_df[['constant', 'rm']].values y_pred = np.dot(_x, _w) _p = pd.DataFrame(dict(actual=train_df['medv'].values, predicted=y_pred.reshape(-1))) _p.head()
  • 20.
    That was alot of work for something basic
  • 22.
    class Model(object): def __init__(self): self.W= None self.b = None def __call__(self, x): if self.W == None: self.W = tf.Variable(tf.random.normal(shape=(1, x.shape[1]))) if self.b == None: self.b = tf.Variable(tf.random.normal(shape=(x.shape[0], 1))) return tf.matmul(x, self.W, transpose_b=True) + self.b
  • 23.
    model = Model() output= model(tf.constant([3.0, 3.1, 1.9, 2.0, 2.5, 2.9], shape=(3,2))) print(output)
  • 24.
    @tf.function def loss(y_pred, y): returntf.reduce_mean(tf.square(y-y_pred))
  • 25.
    This is theinteresting part
  • 26.
    def train(model, x,y, alpha): x = tf.convert_to_tensor(x, np.float32) y = tf.convert_to_tensor(y, np.float32) with tf.GradientTape() as t: t.watch(x) current_loss = loss(model(x), y) #print(current_loss) dW, db = t.gradient(current_loss, [model.W, model.b]) #print(dW, db) model.W.assign_sub(alpha * dW) model.b.assign_sub(alpha * db)
  • 27.
    train_df = df.sample(frac=0.8,random_state=0) test_df= df.drop(train_df.index) columns = ['nox', 'rm', 'chas', 'dis', 'ptratio', 'lstat', 'rad'] X_train = train_df[columns].values X_test = test_df[columns].values y_train = train_df[['medv']].values y_test = test_df[['medv']].values
  • 28.
    epochs = 10 model= Model() for i in range(epochs): train(model, X_train, y_train, alpha=0.1) print(model.W)
  • 29.
    Could it besimpler?
  • 30.
    import tensorflow astf from tensorflow import keras model = keras.Sequential([ keras.layers.Dense(50, input_shape=(7,), activation='relu'), keras.layers.Dense(50, activation='relu'), keras.layers.Dense(50, activation='relu'), keras.layers.Dropout(0.5), keras.layers.Dense(1) ]) print(model.summary())
  • 32.
    adam = keras.optimizers.Adam(0.001) model.compile(optimizer=adam,loss='mse') model.fit(X_train, y_train, epochs=2000, validation_split=0.1)
  • 33.
  • 34.
    # we needa new way of getting data into the model def df_to_dataset(df, columns, shuffle=True, batch_size=64): df = df.copy() labels = df.pop('medv') features_df = df[columns] ds = tf.data.Dataset.from_tensor_slices( (dict(features_df), labels) ) if shuffle: ds = ds.shuffle(buffer_size=len(df)) ds = ds.batch(batch_size) return ds
  • 35.
    from sklearn.model_selection importtrain_test_split train, val = train_test_split(df, test_size=0.1) train_ds = df_to_dataset(train, columns) val_ds = df_to_dataset(val, columns)
  • 36.
    feature_columns = [] #numeric columns for _col in columns: feature_columns.append(tf.feature_column.numeric_column(_col)) # bucketize number of rooms rm_buckets = tf.feature_column.bucketized_column(tf.feature_column.numeric_column('rm '), boundaries=[1, 2, 3, 4, 5, 6, 7, 8, 9])
  • 37.
    rad_buckets = tf.feature_column.bucketized_column(tf.feature_column.numeric_column('rad '), boundaries=[1,5, 10]) nox_buckets = tf.feature_column.bucketized_column(tf.feature_column.numeric_column('nox '), boundaries=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) feature_columns.append(rm_buckets) feature_columns.append(rad_buckets) feature_columns.append(nox_buckets)
  • 38.
    featuresLayer = keras.layers.DenseFeatures(feature_columns) model= keras.Sequential([ featuresLayer, keras.layers.Dense(50, activation='relu'), keras.layers.Dense(50, activation='relu'), keras.layers.Dropout(0.5), keras.layers.Dense(1) ]) model.compile(optimizer='adam', loss='mse') model.fit(train_ds, epochs=50, validation_data=val_ds)
  • 39.
    Neural Networks andDeep Learning http://neuralnetworksanddeeplearning.com/ Learn More Machine Learning Crash Course https://developers.google.com/machine-lear ning/crash-course TensorFlow https://www.tensorflow.org/
  • 40.
    Robert John @robert_thas GDE: ML& Cloud Baby Steps to Machine Learning