Skip to content

Instantly share code, notes, and snippets.

View analyticsindiamagazine's full-sized avatar
:octocat:

Analytics India Magazine analyticsindiamagazine

:octocat:
View GitHub Profile
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#The base image - The container is built on top of this image --# reference: https://hub.docker.com/_/ubuntu/
FROM ubuntu:18.04
# Adds metadata to the image as a key value pair
LABEL version="1.0"
# Set environment variables
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
# Create empty directory to attach volume
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape = 26),
tf.keras.layers.Dense(100, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(1),
])
model.compile(
optimizer=’adam’,
loss='mean_squared_logarithmic_error',
metrics=['RootMeanSquaredError'],
#The hyperparameters & their values to be tested are stored in aspecial type called HParam
HP_NUM_UNITS = hp.HParam('num_units', hp.Discrete([50, 100, 200]))
HP_DROPOUT = hp.HParam('dropout', hp.RealInterval(0.1, 0.3))
HP_OPTIMIZER = hp.HParam('optimizer', hp.Discrete(['adam', 'sgd','nadam']))
#Settinf the Metric to RMSE
METRIC_RMSE = 'RootMeanSquaredError'
#Clear any logs from previous runs
!rm -rf ./logs/
#A function that trains and validates the model and returns the rmse
def train_test_model(hparams):
#Keras sequential model with Hyperparameters passed from the argument
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape = 26),
tf.keras.layers.Dense(hparams[HP_NUM_UNITS], activation=tf.nn.relu, kernel_initializer = 'uniform'),
tf.keras.layers.Dropout(hparams[HP_DROPOUT]),
tf.keras.layers.Dense(1),
])
#A function to log the training process
def run(run_dir, hparams):
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams)
rmse = train_test_model(hparams)
tf.summary.scalar(METRIC_RMSE, rmse, step=10)
# Training the model for each combination of the hyperparameters.
x_train = X_train
x_test, y_test = X_val , y_val
#A unique number for each training session
session_num = 0
#Nested for loop training with all possible combinathon of hyperparameters
for num_units in HP_NUM_UNITS.domain.values:
train_InputExamples = train.apply(lambda x: bert.run_classifier.InputExample(guid=None,
text_a = x[DATA_COLUMN],
text_b = None,
label = x[LABEL_COLUMN]), axis = 1)
val_InputExamples = val.apply(lambda x: bert.run_classifier.InputExample(guid=None,
text_a = x[DATA_COLUMN],
text_b = None,
label = x[LABEL_COLUMN]), axis = 1)