This commit is contained in:
Morten Hjorth-Jensen
2022-10-28 08:14:43 +02:00
parent e3d02b38fe
commit 1c5b6d5130
7 changed files with 173 additions and 222 deletions
+2 -10
View File
@@ -1186,7 +1186,6 @@ train_images, test_images <span style="color: #666666">=</span> train_images <sp
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;">class_names <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;airplane&#39;</span>, <span style="color: #BA2121">&#39;automobile&#39;</span>, <span style="color: #BA2121">&#39;bird&#39;</span>, <span style="color: #BA2121">&#39;cat&#39;</span>, <span style="color: #BA2121">&#39;deer&#39;</span>,
<span style="color: #BA2121">&#39;dog&#39;</span>, <span style="color: #BA2121">&#39;frog&#39;</span>, <span style="color: #BA2121">&#39;horse&#39;</span>, <span style="color: #BA2121">&#39;ship&#39;</span>, <span style="color: #BA2121">&#39;truck&#39;</span>]
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">10</span>))
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">25</span>):
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">5</span>,<span style="color: #666666">5</span>,i<span style="color: #666666">+1</span>)
@@ -1277,8 +1276,7 @@ layer with 10 outputs and a softmax activation.
<pre style="line-height: 125%;">model<span style="color: #666666">.</span>add(layers<span style="color: #666666">.</span>Flatten())
model<span style="color: #666666">.</span>add(layers<span style="color: #666666">.</span>Dense(<span style="color: #666666">64</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;relu&#39;</span>))
model<span style="color: #666666">.</span>add(layers<span style="color: #666666">.</span>Dense(<span style="color: #666666">10</span>))
Here<span style="color: #BA2121">&#39;s the complete architecture of our model.</span>
<span style="color: #408080; font-style: italic">#Here&#39;s the complete architecture of our model.</span>
model<span style="color: #666666">.</span>summary()
</pre>
</div>
@@ -1295,7 +1293,7 @@ model<span style="color: #666666">.</span>summary()
</div>
</div>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.</p>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two dense layers.</p>
<!-- !split -->
<h2 id="compile-and-train-the-model" class="anchor">Compile and train the model </h2>
@@ -1310,7 +1308,6 @@ model<span style="color: #666666">.</span>summary()
<pre style="line-height: 125%;">model<span style="color: #666666">.</span>compile(optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;adam&#39;</span>,
loss<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>keras<span style="color: #666666">.</span>losses<span style="color: #666666">.</span>SparseCategoricalCrossentropy(from_logits<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>),
metrics<span style="color: #666666">=</span>[<span style="color: #BA2121">&#39;accuracy&#39;</span>])
history <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(train_images, train_labels, epochs<span style="color: #666666">=10</span>,
validation_data<span style="color: #666666">=</span>(test_images, test_labels))
</pre>
@@ -1345,9 +1342,7 @@ plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Accuracy&#39;</span>)
plt<span style="color: #666666">.</span>ylim([<span style="color: #666666">0.5</span>, <span style="color: #666666">1</span>])
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lower right&#39;</span>)
test_loss, test_acc <span style="color: #666666">=</span> model<span style="color: #666666">.</span>evaluate(test_images, test_labels, verbose<span style="color: #666666">=2</span>)
<span style="color: #008000">print</span>(test_acc)
</pre>
</div>
@@ -1416,8 +1411,6 @@ systems such as automatic translation and speech-to-text.
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> regularizers
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.utils</span> <span style="color: #008000; font-weight: bold">import</span> to_categorical
<span style="color: #408080; font-style: italic"># convert into dataset matrix</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">convertToMatrix</span>(data, step):
X, Y <span style="color: #666666">=</span>[], []
@@ -1457,7 +1450,6 @@ model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666"
model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666">1</span>))
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">&#39;mean_squared_error&#39;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;rmsprop&#39;</span>)
model<span style="color: #666666">.</span>summary()
model<span style="color: #666666">.</span>fit(trainX,trainY, epochs<span style="color: #666666">=100</span>, batch_size<span style="color: #666666">=16</span>, verbose<span style="color: #666666">=2</span>)
trainPredict <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(trainX)
testPredict<span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(testX)
+2 -10
View File
@@ -1141,7 +1141,6 @@ train_images, test_images = train_images / <span style="color: #B452CD">255.0</s
<div class="highlight" style="background: #eeeedd">
<pre style="font-size: 80%; line-height: 125%;">class_names = [<span style="color: #CD5555">&#39;airplane&#39;</span>, <span style="color: #CD5555">&#39;automobile&#39;</span>, <span style="color: #CD5555">&#39;bird&#39;</span>, <span style="color: #CD5555">&#39;cat&#39;</span>, <span style="color: #CD5555">&#39;deer&#39;</span>,
<span style="color: #CD5555">&#39;dog&#39;</span>, <span style="color: #CD5555">&#39;frog&#39;</span>, <span style="color: #CD5555">&#39;horse&#39;</span>, <span style="color: #CD5555">&#39;ship&#39;</span>, <span style="color: #CD5555">&#39;truck&#39;</span>]
<span style="color: #a61717; background-color: #e3d2d2"></span>
plt.figure(figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">10</span>))
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">25</span>):
plt.subplot(<span style="color: #B452CD">5</span>,<span style="color: #B452CD">5</span>,i+<span style="color: #B452CD">1</span>)
@@ -1233,8 +1232,7 @@ layer with 10 outputs and a softmax activation.
<pre style="font-size: 80%; line-height: 125%;">model.add(layers.Flatten())
model.add(layers.Dense(<span style="color: #B452CD">64</span>, activation=<span style="color: #CD5555">&#39;relu&#39;</span>))
model.add(layers.Dense(<span style="color: #B452CD">10</span>))
Here<span style="color: #CD5555">&#39;s the complete architecture of our model.</span>
<span style="color: #228B22">#Here&#39;s the complete architecture of our model.</span>
model.summary()
</pre>
</div>
@@ -1251,7 +1249,7 @@ model.summary()
</div>
</div>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.</p>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two dense layers.</p>
</section>
<section>
@@ -1267,7 +1265,6 @@ model.summary()
<pre style="font-size: 80%; line-height: 125%;">model.compile(optimizer=<span style="color: #CD5555">&#39;adam&#39;</span>,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=<span style="color: #8B008B; font-weight: bold">True</span>),
metrics=[<span style="color: #CD5555">&#39;accuracy&#39;</span>])
<span style="color: #a61717; background-color: #e3d2d2"></span>
history = model.fit(train_images, train_labels, epochs=<span style="color: #B452CD">10</span>,
validation_data=(test_images, test_labels))
</pre>
@@ -1302,9 +1299,7 @@ plt.xlabel(<span style="color: #CD5555">&#39;Epoch&#39;</span>)
plt.ylabel(<span style="color: #CD5555">&#39;Accuracy&#39;</span>)
plt.ylim([<span style="color: #B452CD">0.5</span>, <span style="color: #B452CD">1</span>])
plt.legend(loc=<span style="color: #CD5555">&#39;lower right&#39;</span>)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=<span style="color: #B452CD">2</span>)
<span style="color: #658b00">print</span>(test_acc)
</pre>
</div>
@@ -1375,8 +1370,6 @@ systems such as automatic translation and speech-to-text.
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">tensorflow.keras</span> <span style="color: #8B008B; font-weight: bold">import</span> regularizers
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">tensorflow.keras.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> to_categorical
<span style="color: #228B22"># convert into dataset matrix</span>
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">convertToMatrix</span>(data, step):
X, Y =[], []
@@ -1416,7 +1409,6 @@ model.add(Dense(<span style="color: #B452CD">8</span>, activation=<span style="c
model.add(Dense(<span style="color: #B452CD">1</span>))
model.compile(loss=<span style="color: #CD5555">&#39;mean_squared_error&#39;</span>, optimizer=<span style="color: #CD5555">&#39;rmsprop&#39;</span>)
model.summary()
model.fit(trainX,trainY, epochs=<span style="color: #B452CD">100</span>, batch_size=<span style="color: #B452CD">16</span>, verbose=<span style="color: #B452CD">2</span>)
trainPredict = model.predict(trainX)
testPredict= model.predict(testX)
+2 -10
View File
@@ -1121,7 +1121,6 @@ train_images, test_images = train_images / <span style="color: #B452CD">255.0</s
<div class="highlight" style="background: #eeeedd">
<pre style="line-height: 125%;">class_names = [<span style="color: #CD5555">&#39;airplane&#39;</span>, <span style="color: #CD5555">&#39;automobile&#39;</span>, <span style="color: #CD5555">&#39;bird&#39;</span>, <span style="color: #CD5555">&#39;cat&#39;</span>, <span style="color: #CD5555">&#39;deer&#39;</span>,
<span style="color: #CD5555">&#39;dog&#39;</span>, <span style="color: #CD5555">&#39;frog&#39;</span>, <span style="color: #CD5555">&#39;horse&#39;</span>, <span style="color: #CD5555">&#39;ship&#39;</span>, <span style="color: #CD5555">&#39;truck&#39;</span>]
<span style="color: #a61717; background-color: #e3d2d2"></span>
plt.figure(figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">10</span>))
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">25</span>):
plt.subplot(<span style="color: #B452CD">5</span>,<span style="color: #B452CD">5</span>,i+<span style="color: #B452CD">1</span>)
@@ -1212,8 +1211,7 @@ layer with 10 outputs and a softmax activation.
<pre style="line-height: 125%;">model.add(layers.Flatten())
model.add(layers.Dense(<span style="color: #B452CD">64</span>, activation=<span style="color: #CD5555">&#39;relu&#39;</span>))
model.add(layers.Dense(<span style="color: #B452CD">10</span>))
Here<span style="color: #CD5555">&#39;s the complete architecture of our model.</span>
<span style="color: #228B22">#Here&#39;s the complete architecture of our model.</span>
model.summary()
</pre>
</div>
@@ -1230,7 +1228,7 @@ model.summary()
</div>
</div>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.</p>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two dense layers.</p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="compile-and-train-the-model">Compile and train the model </h2>
@@ -1245,7 +1243,6 @@ model.summary()
<pre style="line-height: 125%;">model.compile(optimizer=<span style="color: #CD5555">&#39;adam&#39;</span>,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=<span style="color: #8B008B; font-weight: bold">True</span>),
metrics=[<span style="color: #CD5555">&#39;accuracy&#39;</span>])
<span style="color: #a61717; background-color: #e3d2d2"></span>
history = model.fit(train_images, train_labels, epochs=<span style="color: #B452CD">10</span>,
validation_data=(test_images, test_labels))
</pre>
@@ -1280,9 +1277,7 @@ plt.xlabel(<span style="color: #CD5555">&#39;Epoch&#39;</span>)
plt.ylabel(<span style="color: #CD5555">&#39;Accuracy&#39;</span>)
plt.ylim([<span style="color: #B452CD">0.5</span>, <span style="color: #B452CD">1</span>])
plt.legend(loc=<span style="color: #CD5555">&#39;lower right&#39;</span>)
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=<span style="color: #B452CD">2</span>)
<span style="color: #658b00">print</span>(test_acc)
</pre>
</div>
@@ -1351,8 +1346,6 @@ systems such as automatic translation and speech-to-text.
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">tensorflow.keras</span> <span style="color: #8B008B; font-weight: bold">import</span> regularizers
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">tensorflow.keras.utils</span> <span style="color: #8B008B; font-weight: bold">import</span> to_categorical
<span style="color: #228B22"># convert into dataset matrix</span>
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">convertToMatrix</span>(data, step):
X, Y =[], []
@@ -1392,7 +1385,6 @@ model.add(Dense(<span style="color: #B452CD">8</span>, activation=<span style="c
model.add(Dense(<span style="color: #B452CD">1</span>))
model.compile(loss=<span style="color: #CD5555">&#39;mean_squared_error&#39;</span>, optimizer=<span style="color: #CD5555">&#39;rmsprop&#39;</span>)
model.summary()
model.fit(trainX,trainY, epochs=<span style="color: #B452CD">100</span>, batch_size=<span style="color: #B452CD">16</span>, verbose=<span style="color: #B452CD">2</span>)
trainPredict = model.predict(trainX)
testPredict= model.predict(testX)
+2 -10
View File
@@ -1198,7 +1198,6 @@ train_images, test_images <span style="color: #666666">=</span> train_images <sp
<div class="highlight" style="background: #f8f8f8">
<pre style="line-height: 125%;">class_names <span style="color: #666666">=</span> [<span style="color: #BA2121">&#39;airplane&#39;</span>, <span style="color: #BA2121">&#39;automobile&#39;</span>, <span style="color: #BA2121">&#39;bird&#39;</span>, <span style="color: #BA2121">&#39;cat&#39;</span>, <span style="color: #BA2121">&#39;deer&#39;</span>,
<span style="color: #BA2121">&#39;dog&#39;</span>, <span style="color: #BA2121">&#39;frog&#39;</span>, <span style="color: #BA2121">&#39;horse&#39;</span>, <span style="color: #BA2121">&#39;ship&#39;</span>, <span style="color: #BA2121">&#39;truck&#39;</span>]
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">10</span>))
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">25</span>):
plt<span style="color: #666666">.</span>subplot(<span style="color: #666666">5</span>,<span style="color: #666666">5</span>,i<span style="color: #666666">+1</span>)
@@ -1289,8 +1288,7 @@ layer with 10 outputs and a softmax activation.
<pre style="line-height: 125%;">model<span style="color: #666666">.</span>add(layers<span style="color: #666666">.</span>Flatten())
model<span style="color: #666666">.</span>add(layers<span style="color: #666666">.</span>Dense(<span style="color: #666666">64</span>, activation<span style="color: #666666">=</span><span style="color: #BA2121">&#39;relu&#39;</span>))
model<span style="color: #666666">.</span>add(layers<span style="color: #666666">.</span>Dense(<span style="color: #666666">10</span>))
Here<span style="color: #BA2121">&#39;s the complete architecture of our model.</span>
<span style="color: #408080; font-style: italic">#Here&#39;s the complete architecture of our model.</span>
model<span style="color: #666666">.</span>summary()
</pre>
</div>
@@ -1307,7 +1305,7 @@ model<span style="color: #666666">.</span>summary()
</div>
</div>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.</p>
<p>As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two dense layers.</p>
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
<h2 id="compile-and-train-the-model">Compile and train the model </h2>
@@ -1322,7 +1320,6 @@ model<span style="color: #666666">.</span>summary()
<pre style="line-height: 125%;">model<span style="color: #666666">.</span>compile(optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;adam&#39;</span>,
loss<span style="color: #666666">=</span>tf<span style="color: #666666">.</span>keras<span style="color: #666666">.</span>losses<span style="color: #666666">.</span>SparseCategoricalCrossentropy(from_logits<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>),
metrics<span style="color: #666666">=</span>[<span style="color: #BA2121">&#39;accuracy&#39;</span>])
history <span style="color: #666666">=</span> model<span style="color: #666666">.</span>fit(train_images, train_labels, epochs<span style="color: #666666">=10</span>,
validation_data<span style="color: #666666">=</span>(test_images, test_labels))
</pre>
@@ -1357,9 +1354,7 @@ plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;Accuracy&#39;</span>)
plt<span style="color: #666666">.</span>ylim([<span style="color: #666666">0.5</span>, <span style="color: #666666">1</span>])
plt<span style="color: #666666">.</span>legend(loc<span style="color: #666666">=</span><span style="color: #BA2121">&#39;lower right&#39;</span>)
test_loss, test_acc <span style="color: #666666">=</span> model<span style="color: #666666">.</span>evaluate(test_images, test_labels, verbose<span style="color: #666666">=2</span>)
<span style="color: #008000">print</span>(test_acc)
</pre>
</div>
@@ -1428,8 +1423,6 @@ systems such as automatic translation and speech-to-text.
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras</span> <span style="color: #008000; font-weight: bold">import</span> regularizers
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">tensorflow.keras.utils</span> <span style="color: #008000; font-weight: bold">import</span> to_categorical
<span style="color: #408080; font-style: italic"># convert into dataset matrix</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">convertToMatrix</span>(data, step):
X, Y <span style="color: #666666">=</span>[], []
@@ -1469,7 +1462,6 @@ model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666"
model<span style="color: #666666">.</span>add(Dense(<span style="color: #666666">1</span>))
model<span style="color: #666666">.</span>compile(loss<span style="color: #666666">=</span><span style="color: #BA2121">&#39;mean_squared_error&#39;</span>, optimizer<span style="color: #666666">=</span><span style="color: #BA2121">&#39;rmsprop&#39;</span>)
model<span style="color: #666666">.</span>summary()
model<span style="color: #666666">.</span>fit(trainX,trainY, epochs<span style="color: #666666">=100</span>, batch_size<span style="color: #666666">=16</span>, verbose<span style="color: #666666">=2</span>)
trainPredict <span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(trainX)
testPredict<span style="color: #666666">=</span> model<span style="color: #666666">.</span>predict(testX)
Binary file not shown.
File diff suppressed because it is too large Load Diff
+2 -11
View File
@@ -745,7 +745,6 @@ To verify that the dataset looks correct, let's plot the first 25 images from th
!bc pycod
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
@@ -799,11 +798,10 @@ layer with 10 outputs and a softmax activation.
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
Here's the complete architecture of our model.
#Here's the complete architecture of our model.
model.summary()
!ec
As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two Dense layers.
As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (1024) before going through two dense layers.
!split
===== Compile and train the model =====
@@ -812,7 +810,6 @@ As you can see, our (4, 4, 64) outputs were flattened into vectors of shape (102
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
@@ -829,11 +826,8 @@ plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(test_acc)
!ec
@@ -880,8 +874,6 @@ from tensorflow.keras import optimizers
from tensorflow.keras import regularizers
from tensorflow.keras.utils import to_categorical
# convert into dataset matrix
def convertToMatrix(data, step):
X, Y =[], []
@@ -921,7 +913,6 @@ model.add(Dense(8, activation="relu"))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='rmsprop')
model.summary()
model.fit(trainX,trainY, epochs=100, batch_size=16, verbose=2)
trainPredict = model.predict(trainX)
testPredict= model.predict(testX)