=== “Walking” - If less than 4 miles an hour, then they are walking.
1
<details class="details details--success" data-variant="success"><summary>Pseudocode</summary>
1
2
if speed < 4.0:
print("Walking")
</details> === “Running”
1
- If more than 4 miles an hour, then they are running.
1
<details class="details details--success" data-variant="success"><summary>Pseudocode</summary>
1
2
3
4
if speed < 4.0:
print("Walking")
else:
print("Running")
</details> === “Biking”
1
2
3
- If more than 4 miles an hour but less than 12,
then they are running.
- Otherwise, they are biking
1
<details class="details details--success" data-variant="success"><summary>Pseudocode</summary>
1
2
3
4
5
6
if speed < 4.0:
print("Walking")
elif speed < 12.0:
print("Running")
else
print("Biking")
</details> === “Playing soccer”
1
- ???
1
<details class="details details--default" data-variant="default"><summary>Failure: Pseudocode</summary>
1
2
3
4
5
6
if speed < 4.0:
print("Walking")
elif speed < 12.0:
print("Running")
else
print("Biking")
</details>
low speed + long distance or short distance + along road + few stops = walking low speed + long distance + on a field + regular stops = golfing low or high speed + long distance + enclosed area = soccer loss python linenums="1" conda create -n tf python=3.12 conda activate tf pip install --upgrade pip pip install tensorflow pip install opencv-python scipy pooch matplotlib jupyter ipykernel
w and b to observe the loss valuepython linenums="1" --8<-- "docs/csc581/lectures/codes/02-ml-paradigm/exploring_loss.py"
python linenums="1" --8<-- "docs/csc581/lectures/codes/02-ml-paradigm/loss_calculation.py"
$J = \frac{1}{n}\sum(actual-predicted)^2$
$J = \frac{1}{n}\sum^{n}_{i=0} (y_i - (mx_i+c))^2$
python linenums="1" --8<-- "docs/csc581/lectures/codes/02-ml-paradigm/3d_loss.py"
File and select New notebook in Drive python linenums="1" import sys import numpy as np import tensorflow as tf
```python linenums=”1” model = tf.keras.Sequential([tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer=’sgd’, loss=’mean_squared_error’)
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)
model.fit(xs, ys, epochs=500)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
- Line 1 defines a very simple neural network model.
- `units= 1`: Dimensionality of the output space
- `input_shape=[1]`: Dimensionality of the input data
- We're training a neural network on single x's to predict single y's.
<details class="details details--default" data-variant="default"><summary>Example: A neural network</summary>
<figure>
<picture>
<!-- Auto scaling with imagemagick -->
<!--
See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
-->
<source class="responsive-img-srcset" srcset="/assets/img/courses/csc574/02-ml-paradigm/nn-480.webp 480w,/assets/img/courses/csc574/02-ml-paradigm/nn-800.webp 800w,/assets/img/courses/csc574/02-ml-paradigm/nn-1400.webp 1400w," type="image/webp" sizes="95vw" />
<img src="/assets/img/courses/csc574/02-ml-paradigm/nn.jpg" width="50%" height="auto" data-zoomable="" loading="lazy" onerror="this.onerror=null; $('.responsive-img-srcset').remove();" />
</picture>
</figure>
<ul>
<li>Hidden Layer 1: <code class="language-plaintext highlighter-rouge">units=4</code></li>
<li>Hidden Layer 2: <code class="language-plaintext highlighter-rouge">units=4</code></li>
<li>Output Layer: <code class="language-plaintext highlighter-rouge">units=1</code></li>
</ul>
</details>
- Line 2 compiles the model
- Optimizer is defined as `sgd` - Stochastic Gradient Descent.
- Loss function is `mean_squared_error` - MSE.
- Lines 5 and 6 define the X and Y arrays to train the models.
- The fitting process runs 500 times (500 epochs)
- Each epoch is a step:
- Guess
- Measure the loss
- Optimize and repeat
```python linenums="1"
print(model.predict(np.array([10.0])))
python linenums="1" print(model.predict(np.array([10.0, 11.0])))
SHAPE and LOSS with relevance values for the following segment of code, then run it in a new cell```python linenums=”1” import sys
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense
predictions = [] class myCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): predictions.append(model.predict(xs)) callbacks = myCallback()
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)
SHAPE = #YOUR CODE HERE# LOSS = #YOUR CODE HERE#
model = Sequential([Dense(units=1, input_shape=SHAPE)]) model.compile(optimizer=’sgd’, loss=LOSS) model.fit(xs, ys, epochs=300, callbacks=[callbacks], verbose=2) EPOCH_NUMBERS=[1,25,50,150,300] # Update me to see other Epochs plt.plot(xs,ys,label = “Ys”) for EPOCH in EPOCH_NUMBERS: plt.plot(xs,predictions[EPOCH-1],label = “Epoch = “ + str(EPOCH)) plt.legend() plt.show() ```