<jupyter_start><jupyter_text>Linear Regression in Keras  1. Import libraries<jupyter_code>import tensorflow as tf
from tensorflow import keras as K
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>2. Hyperparameters<jupyter_code>NUM_HIDDEN_UNITS = 64
EPOCHS = 500<jupyter_output><empty_output><jupyter_text>3. Load the data<jupyter_code># Load the Boston Housing Prices dataset
boston_housing = K.datasets.boston_housing
(train_data, train_labels), (test_data, test_labels) = boston_housing.load_data()

# Shuffle the training set
order = np.argsort(np.random.random(train_labels.shape))
train_data = train_data[order]
train_labels = train_labels[order]

print("Training set: {}".format(train_data.shape))  # 404 examples, 13 features
print("Testing set:  {}".format(test_data.shape))  # 102 examples, 13 features<jupyter_output>Training set: (404, 13)
Testing set:  (102, 13)<jupyter_text>4. Normalize the data<jupyter_code># Normalize features
# Test data is *not* used when calculating the mean and std
mean = train_data.mean(axis=0)
std = train_data.std(axis=0)
train_data = (train_data - mean) / std
test_data = (test_data - mean) / std<jupyter_output><empty_output><jupyter_text>5. Create the model (i.e. Graph)<jupyter_code># Build the model
model = K.Sequential()
model.add(K.layers.Dense(NUM_HIDDEN_UNITS, activation='relu', input_shape=(train_data.shape[1],)))
model.add(K.layers.Dense(1, activation='linear'))
model.compile(loss='mse',
              optimizer=tf.train.RMSPropOptimizer(learning_rate=0.001),
              metrics=['mae'])
model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 64)                896       
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 65        
=================================================================
Total params: 961
Trainable params: 961
Non-trainable params: 0
_________________________________________________________________<jupyter_text>6. Train<jupyter_code># Train the model
# Display training progress by printing a single dot for each completed epoch
class PrintDot(K.callbacks.Callback):
    def on_epoch_end(self, epoch, logs):
        if epoch % 50 == 0:
            print('Epoch #{}'.format(epoch))


# The patience parameter is the amount of epochs to check for improvement
early_stop = K.callbacks.EarlyStopping(monitor='val_loss', patience=20)
history = model.fit(train_data, train_labels, epochs=EPOCHS,
                    validation_split=0.2, verbose=0,
                    callbacks=[early_stop, PrintDot()])
plt.figure()
plt.xlabel('Epoch')
plt.ylabel('Mean Abs Error [1000$]')
plt.plot(history.epoch, np.array(history.history['mean_absolute_error']), label='Train Loss')
plt.plot(history.epoch, np.array(history.history['val_mean_absolute_error']), label='Val loss')
plt.legend()
plt.ylim([0, 5])<jupyter_output><empty_output><jupyter_text>7. Test<jupyter_code>[loss, mae] = model.evaluate(test_data, test_labels, verbose=0)
print("\n Testing set Mean Abs Error: ${:7.2f}".format(mae * 1000))
# Predict
test_predictions = model.predict(test_data).flatten()

plt.figure()
plt.scatter(test_labels, test_predictions)
plt.xlabel('True Values [1000$]')
plt.ylabel('Predictions [1000$]')
plt.axis('equal')
plt.xlim(plt.xlim())
plt.ylim(plt.ylim())
plt.plot([-100, 100], [-100, 100])
plt.figure()
error = test_predictions - test_labels
plt.hist(error, bins=50)
plt.xlabel("Prediction Error [1000$]")
plt.ylabel("Count")
plt.show()
print()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15637423800689107, 'fraction_numerical': 0.027564272462231646, 'mean_word_length': 5.4512820512820515, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '284162', 'license': 'MIT', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1195, 'n_words': 258, 'path': 'notebooks/1_3_basic_regression_keras.ipynb', 'repo_name': 'easy-tensorflow/TensorFlow_for_Biomedical_Data_Analysis', 'size': '3773'}