computer_science:machine_learning:coursera:introduction_tensorflow_artificial_intelligence_deep

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:coursera:introduction_tensorflow_artificial_intelligence_deep [2020/08/10 12:37] carlossousacomputer_science:machine_learning:coursera:introduction_tensorflow_artificial_intelligence_deep [2023/12/01 12:07] (current) – external edit 127.0.0.1
Line 12: Line 12:
  
 <code python> <code python>
-One Neuron Neural Network +The Complete Code 
-# Dense = Define a Layer of connected Neurons +import tensorflow as tf 
-# Only 1 Layer, Only 1 Unit, so a Single (1) Neuron+import numpy as np 
 +from tensorflow import keras
  
 model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])]) model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
Line 24: Line 25:
 model.fit(xs, ys, epochs=500) model.fit(xs, ys, epochs=500)
  
 +print(model.predict([17.0]))
 +print(model.predict([22.0]))
 +print(model.predict([35.0]))
  
-</code> 
  
-You've probably seen that for machine learning, you need to know and use a lot of math, calculus probability and the like.\\ +</code>
-It's really good to understand that as you want to optimize your models but the nice thing for now about TensorFlow and keras is that a lot of that math is implemented for you in functions.\\ +
-There are two function roles that you should be aware of though and these are loss functions and optimizers.\\ +
-This code defines them. I like to think about it this way.\\ +
-The neural network has no idea of the relationship between X and Y, so it makes a guess.\\ +
-Say it guesses Y equals 10X minus 10. It will then use the data that it knows about, that's the set of Xs and Ys that we've already seen to measure how good or how bad its guess was.\\ +
-The loss function measures this and then gives the data to the optimizer which figures out the next guess. So the optimizer thinks about how good or how badly the guess was done using the data from the loss function.\\ +
-Then the logic is that each guess should be better than the one before. As the guesses get better and better, an accuracy approaches 100 percent, the term convergence is used.\\ +
-In this case, the loss is mean squared error and the optimizer is SGD which stands for stochastic gradient descent. +
- +
-Our next step is to represent the known data.\\ +
-These are the Xs and the Ys that you saw earlier. The np.array is using a Python library called numpy that makes data representation particularly enlists much easier.\\ +
-So here you can see we have one list for the Xs and another one for the Ys. The training takes place in the fit command. Here we're asking the model to figure out how to fit the X values to the Y values.\\ +
-The epochs equals 500 value means that it will go through the training loop 500 times. This training loop is what we described earlier. Make a guess, measure how good or how bad the guesses with the loss function, then use the optimizer and the data to make another guess and repeat this.\\ +
-When the model has finished training, it will then give you back values using the predict method.+
  
 === The Software === === The Software ===
Line 49: Line 38:
 In the case of creating neural networks, the sample I like to use is one where it learns the relationship between two numbers. So, for example, if you were writing code for a function like this, you already know the 'rules' In the case of creating neural networks, the sample I like to use is one where it learns the relationship between two numbers. So, for example, if you were writing code for a function like this, you already know the 'rules'
 <code> <code>
 +
 ''float hw_function(float x){ ''float hw_function(float x){
     float y = (2 * x) - 1;     float y = (2 * x) - 1;
Line 55: Line 45:
  
 </code> </code>
 +
 So how would you train a neural network to do the equivalent task? Using data! By feeding it with a set of Xs, and a set of Ys, it should be able to figure out the relationship between them. So how would you train a neural network to do the equivalent task? Using data! By feeding it with a set of Xs, and a set of Ys, it should be able to figure out the relationship between them.
  
Line 69: Line 60:
 In [0]: In [0]:
  
-<code>import tensorflow as tf+<code> 
 +import tensorflow as tf
 import numpy as np import numpy as np
 from tensorflow import keras from tensorflow import keras
  
 </code> </code>
 +
 === Define and Compile the Neural Network === === Define and Compile the Neural Network ===
  
Line 80: Line 73:
 In [0]: In [0]:
  
-<code>model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])+<code> 
 +model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
  
 </code> </code>
Line 86: Line 80:
 Now we compile our Neural Network. When we do so, we have to specify 2 functions, a loss and an optimizer. Now we compile our Neural Network. When we do so, we have to specify 2 functions, a loss and an optimizer.
  
-If you've seen lots of math for machine learning, here's where it's usually used, but in this case it's nicely encapsulated in functions for you. But what happens here — let's explain...+If you've seen lots of math for machine learning, here's where it's usually used, but in this case it's nicely encapsulated in functions for you. But what happens here — let's explain
  
 We know that in our function, the relationship between the numbers is y=2x-1. We know that in our function, the relationship between the numbers is y=2x-1.
  
-When the computer is trying to 'learn' that, it makes a guess...maybe y=10x+10. The LOSS function measures the guessed answers against the known correct answers and measures how well or how badly it did.+When the computer is trying to 'learn' that, it makes a guessmaybe y=10x+10. The LOSS function measures the guessed answers against the known correct answers and measures how well or how badly it did.
  
 It then uses the OPTIMIZER function to make another guess. Based on how the loss function went, it will try to minimize the loss. At that point maybe it will come up with somehting like y=5x+5, which, while still pretty bad, is closer to the correct result (i.e. the loss is lower) It then uses the OPTIMIZER function to make another guess. Based on how the loss function went, it will try to minimize the loss. At that point maybe it will come up with somehting like y=5x+5, which, while still pretty bad, is closer to the correct result (i.e. the loss is lower)
Line 100: Line 94:
 In [0]: In [0]:
  
-<code>model.compile(optimizer='sgd', loss='mean_squared_error')+<code> 
 +model.compile(optimizer='sgd', loss='mean_squared_error')
  
 </code> </code>
 +
 === Providing the Data === === Providing the Data ===
  
Line 111: Line 107:
 In [0]: In [0]:
  
-<code>xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)+<code> 
 +xs = np.array([-1.0,  0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
 ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float) ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
  
 </code> </code>
 +
 === Training the Neural Network === === Training the Neural Network ===
  
Line 121: Line 119:
 In [0]: In [0]:
  
-<code>model.fit(xs, ys, epochs=500)+<code> 
 +model.fit(xs, ys, epochs=500)
  
 </code> </code>
Line 129: Line 128:
 In [0]: In [0]:
  
-<code>print(model.predict([10.0]))+<code> 
 +print(model.predict([10.0]))
  
 </code> </code>
Line 139: Line 139:
 As you work with neural networks, you'll see this pattern recurring. You will almost always deal with probabilities, not certainties, and will do a little bit of coding to figure out what the result is based on the probabilities, particularly when it comes to classification. As you work with neural networks, you'll see this pattern recurring. You will almost always deal with probabilities, not certainties, and will do a little bit of coding to figure out what the result is based on the probabilities, particularly when it comes to classification.
  
 +==== Week 1 Quiz ====
 +
 +**The diagram for traditional programming had Rules and Data In, but what came out?** Answers
 +
 +**The diagram for Machine Learning had Answers and Data In, but what came out?** Rules
 +
 +**When I tell a computer what the data represents (i.e. this data is for walking, this data is for running), what is that process called? **Labelling the Data
 +
 +**What is a Dense? **A layer of connected neurons
 +
 +**What does a Loss function do?** Measures how good the urrent 'guess' is
 +
 +**What does the optimizer do?** Generates a new and improved guess
 +
 +**What is Convergence?** The process of getting very close to the correct answer
 +
 +**What does model.fit do?** It trains the neural network to fit one set of values to another
 ===== External References: ===== ===== External References: =====
  
  • computer_science/machine_learning/coursera/introduction_tensorflow_artificial_intelligence_deep.1597063059.txt.gz
  • Last modified: 2023/12/01 12:07
  • (external edit)