<jupyter_start><jupyter_text>The Basics - Training Your First ModelThe problem we will solve is to `convert from Celsius to Fahrenheit`, where the approximate formula is:`𝑓=𝑐×1.8+32`<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

%matplotlib inline

print(tf.__version__)
print(tf.keras.__version__)
celsius_q    = np.array([-40, -10,  0,  8, 15, 22,  38],  dtype=float)
fahrenheit_a = np.array([-40,  14, 32, 46, 59, 72, 100],  dtype=float)

for i,c in enumerate(celsius_q):
  print("{:5} degrees Celsius = {:5} degrees Fahrenheit".format(c, fahrenheit_a[i]))<jupyter_output>-40.0 degrees Celsius = -40.0 degrees Fahrenheit
-10.0 degrees Celsius =  14.0 degrees Fahrenheit
  0.0 degrees Celsius =  32.0 degrees Fahrenheit
  8.0 degrees Celsius =  46.0 degrees Fahrenheit
 15.0 degrees Celsius =  59.0 degrees Fahrenheit
 22.0 degrees Celsius =  72.0 degrees Fahrenheit
 38.0 degrees Celsius = 100.0 degrees Fahrenheit<jupyter_text>Build the model<jupyter_code>layer = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([layer])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 1)                 2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Compile the model<jupyter_code>model.compile(loss='mean_squared_error',
              optimizer=tf.keras.optimizers.Adam(0.1))<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>history = model.fit(celsius_q, fahrenheit_a, epochs=500, verbose=False)
print("Finished training the model")<jupyter_output>Finished training the model<jupyter_text>Display the result<jupyter_code>import matplotlib.pyplot as plt
plt.xlabel('Epoch Number')
plt.ylabel("Loss Magnitude")
plt.plot(history.history['loss'])<jupyter_output><empty_output><jupyter_text>PredictThe correct answer is 100×1.8+32=212, so our model is doing really well.<jupyter_code>print(model.predict([100.0]))<jupyter_output>[[211.28845]]<jupyter_text>Looking at the layer weightsformula: `𝑓=1.8𝑐+32.`<jupyter_code>print("These are the layer variables: {}".format(layer.get_weights()))<jupyter_output>These are the layer variables: [array([[1.827267]], dtype=float32), array([28.561745], dtype=float32)]
{'fraction_non_alphanumeric': 0.16004627844195912, 'fraction_numerical': 0.05052063247204011, 'mean_word_length': 5.862433862433862, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '205730', 'license': 'MIT', 'n_tokens_mistral': 945, 'n_tokens_neox': 851, 'n_words': 190, 'path': 'TensorFlow/keras/00_the_basics_training_first_model.ipynb', 'repo_name': 'kaka-lin/ML-Notes', 'size': '2593'}