# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="AtWSVIVcV4j6" colab_type="text"
# # Celcius to Farenheit equation

# + [markdown] colab_type="text" id="F8YVA_634OFk"
# $$ f = c \times 1.8 + 32 $$

# + [markdown] id="Nl3Ho_drWD-t" colab_type="text"
# # Import TensorFlow 2.x.

# + colab_type="code" id="-ZMgCvSRFqxE" colab={}
try:
  # %tensorflow_version 2.x
except Exception:
  pass

import tensorflow as tf
import tensorflow.keras.layers as layers
import tensorflow.keras.models as models

import numpy as np
np.random.seed(7)

import matplotlib.pyplot as plot

print(tf.__version__)


# + [markdown] id="BS2p8F70WISp" colab_type="text"
# # Compute Farenheit using Celcius.

# + id="uHuT5KfSU6Gy" colab_type="code" colab={}
def celsius_to_fahrenheit(celsius_value):
  fahrenheit_value = celsius_value * 1.8 + 32
  return(fahrenheit_value)


# + [markdown] id="0nmpmaxSWQlb" colab_type="text"
# # Generate dataset for converting Celcius to Farenheit.

# + id="tQlqUK-BWSn0" colab_type="code" colab={}
def generate_dataset(number_of_samples=100):

  celsius_values = []
  fahrenheit_values = []

  value_range = number_of_samples

  for sample in range(number_of_samples):    
    celsius_value = np.random.randint(-1*value_range, +1*value_range)
    fahrenheit_value = celsius_to_fahrenheit(celsius_value)

    celsius_values.append(celsius_value)
    fahrenheit_values.append(fahrenheit_value)

  return(celsius_values, fahrenheit_values)


# + colab_type="code" id="gg4pn6aI1vms" colab={}
celsius_values, fahrenheit_values = generate_dataset(number_of_samples=100)
for index, celsius_value in enumerate(celsius_values):
  print("{} degrees Celsius = {} degrees Fahrenheit".format(celsius_value, fahrenheit_values[index]))

# + [markdown] id="o7nHcURZWgR0" colab_type="text"
# # Create artificial neural network model.

# + colab_type="code" id="pRllo2HLfXiu" colab={}
model = models.Sequential(name='model')
model.add(layers.Dense(units=1, input_shape=[1], name='dense_layer'))
model.summary()

# + [markdown] id="N_AcNn6ZWvdq" colab_type="text"
# # Create the optimizer.

# + id="_vkNG0zxRLgj" colab_type="code" colab={}
from tensorflow.keras.optimizers import Adam

learning_rate = 0.2
optimizer = Adam(learning_rate=learning_rate)

# + [markdown] id="A8-yJCSyW5YC" colab_type="text"
# # Train the model.

# + colab_type="code" id="m8YQN1H41L-Y" colab={}
model.compile(loss='mean_squared_error', optimizer=optimizer)

# + id="oygrqQUQXSkK" colab_type="code" colab={}
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir="./logs")

# + id="GoYNprorSLr2" colab_type="code" colab={}
epochs = 500

# + colab_type="code" id="lpRrl7WK10Pq" colab={}
history = model.fit(celsius_values, 
                    fahrenheit_values, 
                    epochs=epochs, 
                    callbacks=[tensorboard_callback],
                    verbose=True)

# + [markdown] id="oRYYeXMTW9V5" colab_type="text"
# # Predict fahrenheit value using celsius value.

# + colab_type="code" id="oxNzL4lS2Gui" colab={}
celsius_value = 100.0
fahrenheit_value = model.predict([celsius_value])
fahrenheit_value = fahrenheit_value[0][0]
print("{} degrees Celsius = {} degrees Fahrenheit".format(celsius_value, fahrenheit_value))

# + [markdown] id="2crsTW1GXHX_" colab_type="text"
# # Print the model weights.

# + colab_type="code" id="kmIkVdkbnZJI" colab={}
print("Layer variables: {}".format(model.get_layer(index=-1).get_weights()))

# + [markdown] id="GCwod_RsYEkx" colab_type="text"
# # Visualize the training graphs.

# + id="ZGpFdfEbYH9T" colab_type="code" colab={}
# %reload_ext tensorboard
# %tensorboard --logdir logs

StarcoderdataJupyterScriptsDedupFiltered