If you have not already, you might want to make sure you know how to:
XOR is a classic example that cannot be solved using linear techniques. We put this to the test in this example, where we try to solve XOR using logistic regression, and then solve it using TensorFlow Neural Network method. The data we use is the XOR problem (4 rows of data), repeated multiple times.
For more information on the XOR problem, please visit here.
#!pip install -q seaborn
#pip install -U scikit-learn
Initiate TensorFlow (tf), and load other libraries that we will need later (e.g. matplotlib, pandas, seaborn)
from __future__ import absolute_import, division, print_function, unicode_literals
import pathlib
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
print(tf.__version__)
First we import some sample data. In this case I have taken the typical XOR example, and repeated it 10 times it in a csv file (there are 4 patterns, so 40 total rows). Why 10? Just an arbitrary number to get multiple full-patterns to run NN and LR. Note that I will be using a split model development & model testing sample, and selecting randomly (this is why can't just work with the 4 rows in the XOR example).
Remember in order to execute a 'cell' like the one below, you can 1) click on it and run it using the run button above or 2) click in the cell and hit shift+enter.
import pandas as pd
wsmpl = pd.read_csv(r'C:\Users\muzay\Desktop\XOR_Sample.csv')
print(wsmpl.shape) #get (numer of rows, number of columns or 'features')
#Choose columns to keep (numeric only)
wsmpl_n = wsmpl[['XOR', 'Inp1', 'Inp2']] #XOR is the dependent variable
wsmpl_n.head()
wsmpl_n.describe() #get some basic stats on the dataset
See if there is missing data:
wsmpl_n.isna().sum()
There is no missing data. Good! Let's proceed to split the data into a random 70% for training, and remainder for testing. Remember we did a similar split for the linear regression example using the boston house price dataset.
x_train = wsmpl_n.sample(frac=0.7,random_state=0)
x_test = wsmpl_n.drop(x_train.index)
#specify labels for train/test datasets
y_train = x_train.pop('XOR')
y_test = x_test.pop('XOR')
We apply logistic regression to the XOR dataset and get performance metrics:
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial').fit(x_train, y_train)
clf.score(x_train, y_train)
clf.score(x_test, y_test)
from sklearn.metrics import confusion_matrix
y_train_true = pd.DataFrame(y_train)
y_train_pred = pd.DataFrame(clf.predict(x_train))
#do the same thing with test dataset
y_test_true = pd.DataFrame(y_test)
y_test_pred = pd.DataFrame(clf.predict(x_test))
pd.DataFrame(confusion_matrix(y_train_true, y_train_pred))
That is a pretty bad result! On the training data no-less. No point going to testing data, logistic regression simply can't solve the XOR example. However, for completeness - let's measure on testing data as well, below. Essentially logistic regression classifies every example in the '1' category.
pd.DataFrame(confusion_matrix(y_test_true, y_test_pred))
Let's look at some statistics on the train dataset.
train_stats = x_train.describe()
train_stats = train_stats.transpose()
train_stats
train_stats['std'] = np.where(train_stats['std']==0,1,train_stats['std'])
train_stats
Look again at the train_stats block above and note how different the ranges of each feature are.
It is good practice to normalize features that use different scales and ranges. Although the model might converge without feature normalization, it makes training more difficult, and it makes the resulting model dependent on the choice of units used in the input.
Note: Although we intentionally generate these statistics from only the training dataset, these statistics will also be used to normalize the test dataset. We need to do that to project the test dataset into the same distribution that the model has been trained on.
def norm(x):
return (x - train_stats['mean']) / train_stats['std']
normed_train_data = norm(x_train)
normed_test_data = norm(x_test)
This normalized data is what we will use to train the model.
Caution: The statistics used to normalize the inputs here (mean and standard deviation) need to be applied to any other data that is fed to the model, along with the one-hot encoding that we did earlier. That includes the test set as well as live data when the model is used in production.
normed_test_data.head()
Let's build our model. Here, we'll use a Sequential model with two densely connected hidden layers, and an output layer that returns a single, continuous value. The model building steps are wrapped in a function, build_model, since we'll create a second model, later on.
def build_model():
model = keras.Sequential([
layers.Dense(64, activation='relu', input_shape=[len(x_train.keys())]),
layers.Dense(64, activation='relu'),
layers.Dense(1)
])
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae', 'mse'])
return model
model = build_model();
Use the .summary method to print a simple description of the model
model.summary()
Now try out the model. Take a batch of 10 examples from the training data and call model.predict on it.
example_batch = normed_train_data[:10]
example_result = model.predict(example_batch)
example_result
It seems to be working, and it produces a result of the expected shape and type.
Train the model for 1000 epochs, and record the training and validation accuracy in the history object.
Note the validation_split set to use 20% of the training data as validation set and the remainder as calibration. Important to note that this is separate from the testing data that we do not touch in the model-training.
# Display training progress by printing a single dot for each completed epoch
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch % 100 == 0: print('')
print('.', end='')
EPOCHS = 1000
history = model.fit(
normed_train_data, y_train,
epochs=EPOCHS, validation_split = 0.2, verbose=0,
callbacks=[PrintDot()])
Visualize the model's training progress using the stats stored in the history object.
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
hist.tail()
def plot_history(history):
hist = pd.DataFrame(history.history)
hist['epoch'] = history.epoch
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Abs Error [XOR]')
plt.plot(hist['epoch'], hist['mean_absolute_error'],
label='Train Error')
plt.plot(hist['epoch'], hist['val_mean_absolute_error'],
label = 'Val Error')
plt.ylim([0,5])
plt.legend()
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Square Error [$XOR^2$]')
plt.plot(hist['epoch'], hist['mean_squared_error'],
label='Train Error')
plt.plot(hist['epoch'], hist['val_mean_squared_error'],
label = 'Val Error')
plt.ylim([0,20])
plt.legend()
plt.show()
plot_history(history)
This graph shows little improvement in the validation error after about 100 epochs. Let's update the model.fit call to automatically stop training when the validation score doesn't improve further. We'll use an EarlyStopping callback that tests a training condition for every epoch. If a set amount of epochs elapses without showing improvement, then automatically stop the training.
You can learn more about this callback here.
model = build_model()
# The patience parameter is the amount of epochs to check for improvement
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10)
history = model.fit(normed_train_data, y_train, epochs=EPOCHS,
validation_split = 0.2, verbose=0, callbacks=[early_stop, PrintDot()])
Let's re-plot the history to hopefully see the model training stopping before things get worse for the validation data.
plot_history(history)
The graph shows that on the validation set, the average error is pretty minimal.
Let's see how well the model generalizes by using the test set, which we did not use at all when training the model. This tells us how well we can expect the model to predict when we use it in the real world.
loss, mae, mse = model.evaluate(normed_test_data, y_test, verbose=2)
print("Testing set Mean Abs Error: {:5.2f} XOR".format(mae))
Finally, we predict XOR values using data in the testing set (and also training, which we will use in the next step to compute more error metrics):
ypred_test = model.predict(normed_test_data)
ypred_train = model.predict(normed_train_data)
plt.scatter(y_test, ypred_test)
plt.xlabel('True Values [XOR]')
plt.ylabel('Predictions [XOR]')
plt.axis('equal')
plt.axis('square')
plt.xlim([0,plt.xlim()[1]])
plt.ylim([0,plt.ylim()[1]])
_ = plt.plot([-100, 100], [-100, 100])
from sklearn import metrics
count=0
while (count<7):
count = count + 1
#Training data
ypred_train = pd.DataFrame(model.predict(normed_train_data), columns=['y_pred'])
ypred_train['y_pred'][ypred_train['y_pred'] >= count/10] = 1
ypred_train['y_pred'][ypred_train['y_pred'] < count/10] = 0
#Test data
ypred_test = pd.DataFrame(model.predict(normed_test_data), columns=['y_pred'])
ypred_test['y_pred'][ypred_test['y_pred'] >= count/10] = 1
ypred_test['y_pred'][ypred_test['y_pred'] < count/10] = 0
tn, fp, fn, tp = metrics.confusion_matrix(y_test, ypred_test).ravel()
#pd.DataFrame(metrics.confusion_matrix(train_labels, train_pred))
print(f'Count: {count}, train score: {metrics.accuracy_score(y_train, ypred_train)}, \
test score: {metrics.accuracy_score(y_test, ypred_test)}, \
ratio = {tp/(tp+fp)}')
pd.DataFrame(metrics.confusion_matrix(y_train, ypred_train))
pd.DataFrame(metrics.confusion_matrix(y_test, ypred_test))
#In this code, we denorm testing examples (X), attach the true Y, and then our predictions, and then output that data
data1=x_test
data1['XOR']=y_test
data1['XOR_Pred']=model.predict(normed_test_data)
data1
The graph above looks pretty, pretty, pretty, good! (pardon the Curb reference!). To get more than a visual understanding of the error, let's compute some error metrics.
We start with developing an empirical distribution of the error term (this is a very useful piece of code!).
error = data1['XOR'] - data1['XOR_Pred']
plt.hist(error, bins = 25)
plt.xlabel("Prediction Error XOR")
_ = plt.ylabel("Count")
Just like we did in the OLS example, let's calculate the mean-squared error, mean absolute error, and the r-squared error on training and testing. This is useful as we can see the extent to which performance degrades from training to testing data (note: we expect this degradation).
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
mse = mean_squared_error(y_test, ypred_test)
print('Mean Squared Error: ',mse)
mae = mean_absolute_error(y_test, ypred_test)
print('Mean Absolute Error: ',mae)
rsq = r2_score(y_train, ypred_train) #R-Squared on the training data
print('R-square, Training: ',rsq)
rsq = r2_score(y_test, ypred_test) #R-Squared on the testing data
print('R-square, Testing: ',rsq)
If you have ideas on how to improve this post, please let me know: https://predictivemodeler.com/feedback/