import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import polars as pl # Excel requires: pip install "polars[excel]"
from io import BytesIO
from urllib.request import urlopen
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input
from sklearn.model_selection import train_test_split
def format_output(data):
y1 = data['Y1'].to_numpy()
y2 = data['Y2'].to_numpy()
return y1, y2
def norm(x):
return x.select([
(pl.col(name) - train_stats['mean'][name]) / train_stats['std'][name]
for name in FEATURES
]).to_numpy()
def plot_diff(y_true, y_pred, title=''):
plt.scatter(y_true, y_pred)
plt.title(title)
plt.xlabel('True Values')
plt.ylabel('Predictions')
plt.axis('equal')
plt.axis('square')
plt.xlim(plt.xlim())
plt.ylim(plt.ylim())
plt.plot([-100, 100], [-100, 100])
plt.show()
def plot_metrics(metric_name, title, ylim=5):
plt.title(title)
plt.ylim(0, ylim)
plt.plot(history.history[metric_name], color='blue', label=metric_name)
plt.plot(history.history['val_' + metric_name], color='green', label='val_' + metric_name)
plt.show()
# Get the data from UCI dataset
URL = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00242/ENB2012_data.xlsx'
# Read a bounded workbook stream with the calamine Excel engine.
with urlopen(URL, timeout=30) as response:
workbook = response.read(2 * 1024 * 1024 + 1)
if len(workbook) > 2 * 1024 * 1024:
raise ValueError("Excel workbook exceeds the 2 MiB limit")
df = pl.read_excel(BytesIO(workbook), engine="calamine")
df = df[np.random.permutation(df.height).tolist()]
# Split the data into train and test with 80 train / 20 test
train_rows, test_rows = train_test_split(range(df.height), test_size=0.2)
train, test = df[train_rows], df[test_rows]
FEATURES = [f'X{i}' for i in range(1, 9)]
# Keyed, training-only statistics skip null/NaN and use sample standard deviation.
train_stats = {
'mean': train.select(pl.col(FEATURES).fill_nan(None).mean()).row(0, named=True),
'std': train.select(pl.col(FEATURES).fill_nan(None).std(ddof=1)).row(0, named=True),
}
# Select Y1 and Y2 as separate NumPy outputs without mutating features
train_Y = format_output(train)
test_Y = format_output(test)
# Normalize the training and test data
norm_train_X = norm(train)
norm_test_X = norm(test)
# Define model layers.
input_layer = Input(shape=(len(FEATURES),))
first_dense = Dense(units='128', activation='relu')(input_layer)
second_dense = Dense(units='128', activation='relu')(first_dense)
# Y1 output will be fed directly from the second dense
y1_output = Dense(units='1', name='y1_output')(second_dense)
third_dense = Dense(units='64', activation='relu')(second_dense)
# Y2 output will come via the third dense
y2_output = Dense(units='1', name='y2_output')(third_dense)
# Define the model with the input layer and a list of output layers
model = Model(inputs=input_layer, outputs=[y1_output, y2_output])
print(model.summary())
# Specify the optimizer, and compile the model with loss functions for both outputs
optimizer = tf.keras.optimizers.SGD(lr=0.001)
model.compile(optimizer=optimizer,
loss={'y1_output': 'mse', 'y2_output': 'mse'},
metrics={'y1_output': tf.keras.metrics.RootMeanSquaredError(),
'y2_output': tf.keras.metrics.RootMeanSquaredError()})
# Train the model for 2000 epochs
history = model.fit(norm_train_X, train_Y,
epochs=2000, batch_size=10, validation_data=(norm_test_X, test_Y))
# Test the model and print loss and mse for both outputs
loss, Y1_loss, Y2_loss, Y1_rmse, Y2_rmse = model.evaluate(x=norm_test_X, y=test_Y)
print("Loss = {}, Y1_loss = {}, Y1_mse = {}, Y2_loss = {}, Y2_mse = {}".format(loss, Y1_loss, Y1_rmse, Y2_loss, Y2_rmse))
# Plot the loss and mse
Y_pred = model.predict(norm_test_X)
plot_diff(test_Y[0], Y_pred[0], title='Y1')
plot_diff(test_Y[1], Y_pred[1], title='Y2')
plot_metrics(metric_name='y1_output_root_mean_squared_error', title='Y1 RMSE', ylim=6)
plot_metrics(metric_name='y2_output_root_mean_squared_error', title='Y2 RMSE', ylim=7)