提交 035e297f 编写于 作者: A Amirsina Torfi

MLP

上级 0d50ab34
......@@ -257,36 +257,21 @@ Basic Machine Learning
.. _ipythonlinearreg: codes/ipython/basics_in_machine_learning/linearRegressionOneVariable.ipynb
.. _tutoriallinearreg: https://www.machinelearningmindset.com/linear-regression-with-tensorflow/
.. _ipythonimageclassification: codes/ipython/basics_in_machine_learning/linearRegressionOneVariable.ipynb
.. |lr| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/basics_in_machine_learning/linearRegressionOneVariable.ipynb
.. |ic| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/basics_in_machine_learning/ImageClassification.ipynb
+----+-----------------------------------+--------------------------+-----------------------------------------------+-------------------------------------------+
| # | topic | Run | Source Code | More |
+====+===================================+==========================+===============================================+===========================================+
| 1 | Linear Regression | |lr| | `Notebook <ipythonlinearreg_>`_ | `Tutorial <tutoriallinearreg_>`_ |
+----+-----------------------------------+--------------------------+-----------------------------------------------+-------------------------------------------+
| 1 | Image Classification | |ic| | `Notebook <ipythonimageclassification_>`_ | |
+----+-----------------------------------+--------------------------+-----------------------------------------------+-------------------------------------------+
.. _tutoriallinearreg: https://www.machinelearningmindset.com/linear-regression-with-tensorflow/
.. +----+----------------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
.. | # | topic | Source Code | |
.. +====+============================+========================================================================================+==============================================+
.. | 4 | *Linear Models* |`Linear Regression`_ / `IPython <LinearRegressionipython_>`_ | `Tutorial <Documentationlr_>`_ |
.. +----+----------------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
.. | 5 | *Predictive Models* | `Logistic Regression`_ / `IPython <LogisticRegressionipython_>`_ | `Documentation <LogisticRegDOC_>`_ |
.. +----+----------------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
.. | 6 | *Support Vector Machines* | `Linear SVM`_ / `IPython <LinearSVMipython_>`_ | |
.. +----+----------------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
.. | 7 | *Support Vector Machines* |`MultiClass Kernel SVM`_ / `IPython <MultiClassKernelSVMipython_>`_ | |
.. +----+----------------------------+----------------------------------------------------------------------------------------+----------------------------------------------+
==========================
......@@ -301,15 +286,20 @@ Neural Networks
:alt: alternate text
:align: right
+----+-----------------------------------+-----------------------------------------------------------------------------------------------+----------------------------------------------+
| # | topic | Source Code | |
+====+===================================+===============================================================================================+==============================================+
| 8 | *Multi Layer Perceptron* |`Simple Multi Layer Perceptron`_ / `IPython <MultiLayerPerceptronipython_>`_ | |
+----+-----------------------------------+-----------------------------------------------------------------------------------------------+----------------------------------------------+
| 9 | *Convolutional Neural Network* | `Simple Convolutional Neural Networks`_ | `Documentation <Documentationcnn_>`_ |
+----+-----------------------------------+-----------------------------------------------------------------------------------------------+----------------------------------------------+
| 10 | *Recurrent Neural Network* | `RNN`_ / `IPython <RNNIpython_>`_ | |
+----+-----------------------------------+-----------------------------------------------------------------------------------------------+----------------------------------------------+
.. _ipythonmlp: https://github.com/instillai/TensorFlow-Course/blob/master/codes/ipython/neural_networks/mlp.ipynb
.. |mlp| image:: https://colab.research.google.com/assets/colab-badge.svg
:target: https://colab.research.google.com/github/instillai/TensorFlow-Course/blob/master/codes/ipython/basics_in_machine_learning/ImageClassification.ipynb
+----+-----------------------------------+--------------------------+-----------------------------------------------+
| # | topic | Run | Source Code |
+====+===================================+==========================+===============================================+
| 1 | *Multi Layer Perceptron* | |mlp| | `Notebook <ipythonmlp_>`_ |
+----+-----------------------------------+--------------------------+-----------------------------------------------+
.. ~~~~~~~~~~~~
.. **Welcome**
......
此差异已折叠。
......@@ -3,7 +3,7 @@
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "ImageClassification.ipynb",
"name": "mlp.ipynb",
"provenance": [],
"collapsed_sections": []
},
......@@ -16,7 +16,7 @@
{
"cell_type": "code",
"metadata": {
"id": "CKfkSFu7nlVg"
"id": "x1lvvYoqh_B8"
},
"source": [
"import tensorflow as tf"
......@@ -27,8 +27,8 @@
{
"cell_type": "code",
"metadata": {
"id": "pznVa656noFn",
"outputId": "3fd800b7-db88-4d48-e512-4f7b793a798b",
"id": "NljIzKkyiBzz",
"outputId": "8e5542ef-854f-4396-a017-2c392702d754",
"colab": {
"base_uri": "https://localhost:8080/"
}
......@@ -50,6 +50,8 @@
{
"output_type": "stream",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11493376/11490434 [==============================] - 0s 0us/step\n",
"Data type: float64\n",
"Data type: uint8\n"
],
......@@ -60,8 +62,8 @@
{
"cell_type": "code",
"metadata": {
"id": "zSZB8zVGq46i",
"outputId": "884d07cd-ecf3-43b3-b1b0-5440875f4cc8",
"id": "gMeQ7B8hiDPp",
"outputId": "ef12890e-d3d1-43c2-d32c-dfc92133bde1",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 268
......@@ -105,20 +107,11 @@
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6cjdho1KupbS"
},
"source": [
"## Training"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Z9X1PzYstWKe",
"outputId": "635afa44-86dd-495f-b673-5eb4929f0392",
"id": "u-_cXLqJiEgq",
"outputId": "0d69795b-a262-458f-f5e8-885005740045",
"colab": {
"base_uri": "https://localhost:8080/"
}
......@@ -146,25 +139,25 @@
"output_type": "stream",
"text": [
"Epoch 1/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.2438 - accuracy: 0.9317\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.2459 - accuracy: 0.9304\n",
"Epoch 2/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0995 - accuracy: 0.9707\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.1022 - accuracy: 0.9690\n",
"Epoch 3/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0692 - accuracy: 0.9793\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0701 - accuracy: 0.9789\n",
"Epoch 4/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0507 - accuracy: 0.9837\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0516 - accuracy: 0.9843\n",
"Epoch 5/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0378 - accuracy: 0.9885\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0389 - accuracy: 0.9880\n",
"Epoch 6/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0284 - accuracy: 0.9910\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0300 - accuracy: 0.9903\n",
"Epoch 7/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0229 - accuracy: 0.9931\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0227 - accuracy: 0.9930\n",
"Epoch 8/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0188 - accuracy: 0.9938\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0191 - accuracy: 0.9941\n",
"Epoch 9/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0149 - accuracy: 0.9953\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0138 - accuracy: 0.9958\n",
"Epoch 10/10\n",
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0130 - accuracy: 0.9960\n"
"1875/1875 [==============================] - 4s 2ms/step - loss: 0.0140 - accuracy: 0.9956\n"
],
"name": "stdout"
},
......@@ -172,7 +165,7 @@
"output_type": "execute_result",
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x7fdf61bfeda0>"
"<tensorflow.python.keras.callbacks.History at 0x7f4308281f28>"
]
},
"metadata": {
......@@ -182,20 +175,11 @@
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oD2sc_rNurqN"
},
"source": [
"## Evaluation"
]
},
{
"cell_type": "code",
"metadata": {
"id": "EfO3wC__tdj5",
"outputId": "4d43dbd0-54cf-4904-ae42-ea2adfb97b99",
"id": "druwNSZQiGJi",
"outputId": "70890649-f7d5-4472-e4af-b7c4a34adf10",
"colab": {
"base_uri": "https://localhost:8080/"
}
......@@ -204,13 +188,13 @@
"eval_loss, eval_acc = model.evaluate(x_test, y_test, verbose=1)\n",
"print('Eval accuracy percentage: {:.2f}'.format(eval_acc * 100))\n"
],
"execution_count": 11,
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"text": [
"313/313 [==============================] - 0s 1ms/step - loss: 0.0781 - accuracy: 0.9793\n",
"Eval accuracy percentage: 97.93\n"
"313/313 [==============================] - 0s 1ms/step - loss: 0.0882 - accuracy: 0.9781\n",
"Eval accuracy percentage: 97.81\n"
],
"name": "stdout"
}
......@@ -219,7 +203,7 @@
{
"cell_type": "code",
"metadata": {
"id": "6gf9IHenvlTH"
"id": "q5aSajJaiIaM"
},
"source": [
""
......
# -*- coding: utf-8 -*-
"""CNNs.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ibfKtpxC_hIhZlPbefCoqpAS7jTdyiFw
"""
import tensorflow as tf
# Load MNIST data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# Preprocessing
x_train = x_train / 255.0
x_test = x_test / 255.0
# Add one domention to make 3D images
x_train = x_train[...,tf.newaxis]
x_test = x_test[...,tf.newaxis]
# Track the data type
dataType, dataShape = x_train.dtype, x_train.shape
print(f"Data type and shape x_train: {dataType} {dataShape}")
labelType, labelShape = y_train.dtype, y_train.shape
print(f"Data type and shape y_train: {labelType} {labelShape}")
im_list = []
n_samples_to_show = 16
c = 0
for i in range(n_samples_to_show):
im_list.append(x_train[i])
# Visualization
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(figsize=(4., 4.))
# Ref: https://matplotlib.org/3.1.1/gallery/axes_grid1/simple_axesgrid.html
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(4, 4), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
)
# Show image grid
for ax, im in zip(grid, im_list):
# Iterating over the grid returns the Axes.
ax.imshow(im[:,:,0], 'gray')
plt.show()
"""## Training"""
# Model building
NUM_CLASSES = 10
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(NUM_CLASSES, activation='sigmoid')]
)
# Compiling the model with the high-level keras
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
# Model training
model.fit(x_train, y_train, epochs=5)
"""## Evaluation"""
eval_loss, eval_acc = model.evaluate(x_test, y_test, verbose=1)
print('Eval accuracy percentage: {:.2f}'.format(eval_acc * 100))
# -*- coding: utf-8 -*-
"""mlp.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1SFWk7Ap06ZkvP2HmLhXLiyyqo-ei35M1
"""
import tensorflow as tf
# Load MNIST data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# Preprocessing
x_train = x_train / 255.0
x_test = x_test / 255.0
# Track the data type
dataType = x_train.dtype
print(f"Data type: {dataType}")
labelType = y_test.dtype
print(f"Data type: {labelType}")
im_list = []
n_samples_to_show = 16
c = 0
for i in range(n_samples_to_show):
im_list.append(x_train[i])
# Visualization
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
fig = plt.figure(figsize=(4., 4.))
# Ref: https://matplotlib.org/3.1.1/gallery/axes_grid1/simple_axesgrid.html
grid = ImageGrid(fig, 111, # similar to subplot(111)
nrows_ncols=(4, 4), # creates 2x2 grid of axes
axes_pad=0.1, # pad between axes in inch.
)
# Show image grid
for ax, im in zip(grid, im_list):
# Iterating over the grid returns the Axes.
ax.imshow(im, 'gray')
plt.show()
# Model building
NUM_CLASSES = 10
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dense(NUM_CLASSES, activation='sigmoid')
])
# Compiling the model with the high-level keras
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['accuracy'])
# Model training
model.fit(x_train, y_train, epochs=10)
eval_loss, eval_acc = model.evaluate(x_test, y_test, verbose=1)
print('Eval accuracy percentage: {:.2f}'.format(eval_acc * 100))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册