computer_science:machine_learning:udacity:intro_to_tensorflow_for_deep_learning:basics_training_your_first_model

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
computer_science:machine_learning:udacity:intro_to_tensorflow_for_deep_learning:basics_training_your_first_model [2020/08/11 12:35] carlossousacomputer_science:machine_learning:udacity:intro_to_tensorflow_for_deep_learning:basics_training_your_first_model [2023/12/01 12:07] (current) – external edit 127.0.0.1
Line 15: Line 15:
 ---- ----
  
-==== Import dependencies ====+===== Import dependencies =====
  
 First, import TensorFlow. Here, we're calling it ''tf'' for ease of use. We also tell it to only display errors. First, import TensorFlow. Here, we're calling it ''tf'' for ease of use. We also tell it to only display errors.
Line 30: Line 30:
 </code> </code>
  
-==== Set up Training Data ====+===== Set up Training Data =====
  
 We create two lists ''celsius_q'' and ''fahrenheit_a'' that we can use to train our model. We create two lists ''celsius_q'' and ''fahrenheit_a'' that we can use to train our model.
Line 43: Line 43:
 </code> </code>
  
-**Some Machine Learning terminology**+====   Some Machine Learning terminology   ====
  
-   * **Feature**  — The input(s) to our model. In this case, a single value — the degrees in Celsius.+  * **Feature**  — The input(s) to our model. In this case, a single value — the degrees in Celsius.
   * **Labels**  — The output our model predicts. In this case, a single value — the degrees in Fahrenheit.   * **Labels**  — The output our model predicts. In this case, a single value — the degrees in Fahrenheit.
   * **Example**  — A pair of inputs/outputs used during training. In our case a pair of values from ''celsius_q''  and ''fahrenheit_a''  at a specific index, such as ''(22,72)''.   * **Example**  — A pair of inputs/outputs used during training. In our case a pair of values from ''celsius_q''  and ''fahrenheit_a''  at a specific index, such as ''(22,72)''.
 Next, create the model. We will use the simplest possible model we can, a Dense network. Since the problem is straightforward, this network will require only a single layer, with a single neuron. Next, create the model. We will use the simplest possible model we can, a Dense network. Since the problem is straightforward, this network will require only a single layer, with a single neuron.
  
-==== Create the model ====+===== Create the model =====
  
 We'll call the layer ''l0''  and create it by instantiating ''tf.keras.layers.Dense''  with the following configuration: We'll call the layer ''l0''  and create it by instantiating ''tf.keras.layers.Dense''  with the following configuration:
Line 63: Line 63:
 </code> </code>
  
-==== Compile the model with loss and optimizer functions ====+===== Compile the model with loss and optimizer functions =====
  
 Before training, the model has to be compiled. When compiled for training, the model is given: Before training, the model has to be compiled. When compiled for training, the model is given:
Line 86: Line 86:
 One part of the Optimizer you may need to think about when building your own models is the learning rate (''0.1''  in the code above). This is the step size taken when adjusting values in the model. If the value is too small, it will take too many iterations to train the model. Too large, and accuracy goes down. Finding a good value often involves some trial and error, but the range is usually within 0.001 (default), and 0.1 One part of the Optimizer you may need to think about when building your own models is the learning rate (''0.1''  in the code above). This is the step size taken when adjusting values in the model. If the value is too small, it will take too many iterations to train the model. Too large, and accuracy goes down. Finding a good value often involves some trial and error, but the range is usually within 0.001 (default), and 0.1
  
-==== Train the Model ====+===== Train the Model =====
  
 Train the model by calling the ''fit''  method. Train the model by calling the ''fit''  method.
Line 101: Line 101:
 </code> </code>
  
-==== Display Training Statistics ====+===== Display Training Statistics =====
  
 The ''fit''  method returns a history object. We can use this object to plot how the loss of our model goes down after each training epoch. A high loss means that the Fahrenheit degrees the model predicts is far from the corresponding value in ''fahrenheit_a''. The ''fit''  method returns a history object. We can use this object to plot how the loss of our model goes down after each training epoch. A high loss means that the Fahrenheit degrees the model predicts is far from the corresponding value in ''fahrenheit_a''.
Line 116: Line 116:
 </code> </code>
  
-==== Use the Model to Predict Values ====+===== Use the Model to Predict Values =====
  
 Now you have a model that has been trained to learn the relationship between ''celsius_q''  and ''fahrenheit_a''. You can use the predict method to have it calculate the Fahrenheit degrees for a previously unknown Celsius degrees. Now you have a model that has been trained to learn the relationship between ''celsius_q''  and ''fahrenheit_a''. You can use the predict method to have it calculate the Fahrenheit degrees for a previously unknown Celsius degrees.
Line 155: Line 155:
 With additional neurons, additional inputs, and additional outputs, the formula becomes much more complex, but the idea is the same. With additional neurons, additional inputs, and additional outputs, the formula becomes much more complex, but the idea is the same.
  
-==== A little experiment ====+===== A little experiment =====
  
 Just for fun, what if we created more Dense layers with different units, which therefore also has more variables? Just for fun, what if we created more Dense layers with different units, which therefore also has more variables?
Line 178: Line 178:
 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 ''l0''  and ''l1''  layers, they are nothing even close to ~1.8 and ~32. The added complexity hides the "simple" form of the conversion equation. 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 ''l0''  and ''l1''  layers, they are nothing even close to ~1.8 and ~32. The added complexity hides the "simple" form of the conversion equation.
  
-==== The complete code and output ====+===== The complete code and output =====
  
 The Code: The Code:
  • computer_science/machine_learning/udacity/intro_to_tensorflow_for_deep_learning/basics_training_your_first_model.1597149303.txt.gz
  • Last modified: 2023/12/01 12:07
  • (external edit)