Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
computer_science:machine_learning:udacity:intro_to_tensorflow_for_deep_learning:basics_training_your_first_model [2020/08/11 14:32] – created carlossousa | computer_science:machine_learning:udacity:intro_to_tensorflow_for_deep_learning:basics_training_your_first_model [2024/08/16 12:54] (current) – removed carlossousa | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== The Basics: Training your first model ====== | ||
- | |||
- | The problem we will solve is to convert from Celsius to Fahrenheit, where the approximate formula is: | ||
- | |||
- | f=c×1.8+32 | ||
- | |||
- | Of course, it would be simple enough to create a conventional Python function that directly performs this calculation, | ||
- | |||
- | Instead, we will give TensorFlow some sample Celsius values (0, 8, 15, 22, 38) and their corresponding Fahrenheit values (32, 46, 59, 72, 100). Then, we will train a model that figures out the above formula through the training process. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Import dependencies ===== | ||
- | |||
- | First, import TensorFlow. Here, we're calling it '' | ||
- | |||
- | Next, import [[http:// | ||
- | < | ||
- | import tensorflow as tf # Import Tensorflow | ||
- | import numpy as np # Import NumPy | ||
- | import logging | ||
- | logger = tf.get_logger() | ||
- | logger.setLevel(logging.ERROR) | ||
- | |||
- | |||
- | </ | ||
- | |||
- | ===== Set up Training Data ===== | ||
- | |||
- | We create two lists '' | ||
- | < | ||
- | fahrenheit_a = np.array([-40, | ||
- | |||
- | for i,c in enumerate(celsius_q): | ||
- | print(" | ||
- | |||
- | |||
- | </ | ||
- | |||
- | **Some Machine Learning terminology** | ||
- | |||
- | * **Feature** | ||
- | * **Labels** | ||
- | * **Example** | ||
- | Next, create the model. We will use the simplest possible model we can, a Dense network. Since the problem is straightforward, | ||
- | |||
- | ===== Create the model ===== | ||
- | |||
- | We'll call the layer '' | ||
- | |||
- | * '' | ||
- | * '' | ||
- | < | ||
- | model = tf.keras.Sequential([l0]) | ||
- | |||
- | |||
- | </ | ||
- | |||
- | ===== Compile the model with loss and optimizer functions ===== | ||
- | |||
- | Before training, the model has to be compiled. When compiled for training, the model is given: | ||
- | |||
- | * **Loss function** | ||
- | * **Optimizer function** | ||
- | < | ||
- | | ||
- | |||
- | |||
- | </ | ||
- | |||
- | These are used during training ('' | ||
- | |||
- | During training, the optimizer function is used to calculate adjustments to the model' | ||
- | |||
- | TensorFlow uses numerical analysis to perform this tuning, and all this complexity is hidden from you so we will not go into the details here. What is useful to know about these parameters are: | ||
- | |||
- | The loss function ([[https:// | ||
- | |||
- | One part of the Optimizer you may need to think about when building your own models is the learning rate ('' | ||
- | |||
- | ===== Train the Model ===== | ||
- | |||
- | Train the model by calling the '' | ||
- | |||
- | During training, the model takes in Celsius values, performs a calculation using the current internal variables (called " | ||
- | |||
- | This cycle of calculate, compare, adjust is controlled by the '' | ||
- | < | ||
- | history = model.fit(celsius_q, | ||
- | print(" | ||
- | |||
- | |||
- | </ | ||
- | |||
- | ===== Display Training Statistics ===== | ||
- | |||
- | The '' | ||
- | |||
- | We'll use [[https:// | ||
- | < | ||
- | import matplotlib.pyplot as plt | ||
- | plt.xlabel(' | ||
- | plt.ylabel(" | ||
- | plt.plot(history.history[' | ||
- | |||
- | |||
- | </ | ||
- | |||
- | ===== Use the Model to Predict Values ===== | ||
- | |||
- | Now you have a model that has been trained to learn the relationship between '' | ||
- | |||
- | So, for example, if the Celsius value is 100, what do you think the Fahrenheit result will be? Take a guess before you run this code. | ||
- | |||
- | < | ||
- | |||
- | |||
- | </ | ||
- | |||
- | The correct answer is 100×1.8+32=212, | ||
- | |||
- | ===== To review ===== | ||
- | |||
- | * We created a model with a Dense layer | ||
- | * We trained it with 3500 examples (7 pairs, over 500 epochs). | ||
- | |||
- | Our model tuned the variables (weights) in the Dense layer until it was able to return the correct Fahrenheit value for any Celsius value. (Remember, 100 Celsius was not part of our training data.) | ||
- | |||
- | ===== Looking at the layer weights ===== | ||
- | |||
- | Finally, let's print the internal variables of the Dense layer. | ||
- | |||
- | < | ||
- | |||
- | |||
- | </ | ||
- | |||
- | The first variable is close to ~1.8 and the second to ~32. These values (1.8 and 32) are the actual variables in the real conversion formula. | ||
- | |||
- | This is really close to the values in the conversion formula. But for a single neuron with a single input and a single output, the internal math looks the same as [[https:// | ||
- | |||
- | Since the form is the same, the variables should converge on the standard values of 1.8 and 32, which is exactly what happened. | ||
- | |||
- | With additional neurons, additional inputs, and additional outputs, the formula becomes much more complex, but the idea is the same. | ||
- | |||
- | ===== A little experiment ===== | ||
- | |||
- | Just for fun, what if we created more Dense layers with different units, which therefore also has more variables? | ||
- | < | ||
- | l0 = tf.keras.layers.Dense(units=4, | ||
- | l1 = tf.keras.layers.Dense(units=4) | ||
- | l2 = tf.keras.layers.Dense(units=1) | ||
- | model = tf.keras.Sequential([l0, | ||
- | model.compile(loss=' | ||
- | model.fit(celsius_q, | ||
- | print(" | ||
- | print(model.predict([100.0])) | ||
- | print(" | ||
- | print(" | ||
- | print(" | ||
- | print(" | ||
- | |||
- | |||
- | </ | ||
- | |||
- | As you can see, this model is also able to predict the corresponding Fahrenheit value really well. But when you look at the variables (weights) in the '' | ||
- | |||
- | ===== The complete code and output ===== | ||
- | |||
- | The Code: | ||
- | |||
- | < | ||
- | import numpy as np # Import NumPy | ||
- | import logging | ||
- | import matplotlib.pyplot as plt | ||
- | logger = tf.get_logger() | ||
- | logger.setLevel(logging.ERROR) | ||
- | |||
- | celsius_q | ||
- | fahrenheit_a = np.array([-40, | ||
- | |||
- | for i,c in enumerate(celsius_q): | ||
- | print(" | ||
- | |||
- | l0 = tf.keras.layers.Dense(units=1, | ||
- | model = tf.keras.Sequential([l0]) | ||
- | |||
- | model.compile(loss=' | ||
- | | ||
- | |||
- | history = model.fit(celsius_q, | ||
- | print(" | ||
- | |||
- | print(model.predict([50.0])) | ||
- | print(model.predict([100.0])) | ||
- | |||
- | plt.xlabel(' | ||
- | plt.ylabel(" | ||
- | plt.plot(history.history[' | ||
- | |||
- | print(" | ||
- | |||
- | |||
- | </ | ||
- | |||
- | The Output: | ||
- | |||
- | < | ||
- | -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 | ||
- | [[75.84763|]] | ||
- | [[151.69527|]] | ||
- | Finished training the model | ||
- | [[121.66063|]] | ||
- | [[211.69658|]] | ||
- | These are the layer variables: [array([[1.8007188|]], | ||
- | |||
- | |||
- | </ | ||