Table Of Contents
A One-To-One Model
In [24]:
import tensorflow as tf
import matplotlib.pyplot as plt
print(f'TF VERSION: {tf.__version__}')Mock some data
In [2]:
indVarTensor = tf.range(-100,100,4)
indVarTensorOut [2]:
In [3]:
depVarTensor = indVarTensor * 10
depVarTensorOut [3]:
In [4]:
plt.scatter(indVarTensor, depVarTensor)Out [4]:
Split Data Into 3
- Training: model learns from this data, typically 7080% of the data
- Validation: model gets "tuned" on this, typically 10-15% of data
- Test: the model's learning gets evaluated on this, typically 10-15% of data
Here, we'll split the data into training & validation. the Test data will be manual at the end.
In [5]:
len(depVarTensor)Out [5]:
In [6]:
x_train = indVarTensor[:40] #first 40
y_train = depVarTensor[:40] #last 10
x_test = indVarTensor[40:]
y_test = depVarTensor[40:]In [7]:
plt.figure(figsize=(10, 7))
# TRAINING
plt.scatter(x_train, y_train, c='b', label='Training data')
# TESTING
plt.scatter(x_test, y_test, c='g', label='Testing data')
plt.legend();Create & Visualize a model
In [8]:
# increase repeatability
tf.random.set_seed(42)
m = tf.keras.Sequential()
# layers
mL1 = tf.keras.layers.Dense(1, input_shape=[1])
# mL2 = tf.keras.layers.Dense(1)
m.add(mL1)
# m.add(mL2)
m.compile(loss=tf.keras.losses.mae,
# optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
optimizer=tf.keras.optimizers.SGD(),
metrics=["mae"])Sumarizing the model
In [9]:
# https://www.tensorflow.org/api_docs/python/tf/keras/Model#summary
m.summary()Fit The Model To The Data
In [10]:
m.fit(tf.expand_dims(x_train, axis=-1), y_train, epochs=100, verbose=1)Out [10]:
In [11]:
## Build some prediction data
predictions = m.predict(x_test)
predictionsOut [11]:
Evaluating A Model's Predictions
Plot It
Plot 3 datasets: train data, test data, and prediction data.
In [12]:
def plot_predictions(train_data=x_train,
train_labels=y_train,
test_data=x_test,
test_labels=y_test,
predictions=predictions):
"""
Plots training data, test data and compares predictions.
"""
plt.figure(figsize=(10, 7))
# Plot training data in blue
plt.scatter(train_data, train_labels, c="b", label="Training data")
# Plot test data in green
plt.scatter(test_data, test_labels, c="g", label="Testing data")
# Plot the predictions in red (predictions were made on the test data)
plt.scatter(test_data, predictions, c="r", label="Predictions")
# Show the legend
plt.legend();
plot_predictions()Regression Evaluation Metrics
Different evaluation metrics exist. Regressions commonly use MAE and
- MAE (mean avg error): on average how "wrong" each prediction is. a great "starter metric" for any regression
- MSE (mean squared error): square the errors, then find the avg. Useful when larger errors are more significant than smaller errors
In [13]:
m.evaluate(x_test, y_test)Out [13]:
In [21]:
# get err of EACH test input
# NOTE: the tensors have to be the same "shape", and the predictions are NOT the same shape
# as the test data
squeezedPredictions = tf.squeeze(predictions)
# y_test.shape, predictions.shape, squeezedPredictions.shape
myMas = tf.metrics.mean_absolute_error(y_true=y_test,
y_pred=squeezedPredictions)
myMasOut [21]:
In [23]:
myMse = tf.metrics.mean_squared_error(y_true=y_test,
y_pred=squeezedPredictions)
myMseOut [23]: