{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jqb8fofZjG_M"
},
"source": [
"## Autoencoder and Variational Autoencoder\n",
"\n",
"本章節我們將介紹並帶各位同學實作非監督式學習中的自編碼器及其變形。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z3vqtjr_jG_N"
},
"source": [
"## 1: 匯入所需套件"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "aYfycVtJjG_T"
},
"outputs": [],
"source": [
"# Import some useful packages\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from ipywidgets import interact, IntSlider, FloatSlider\n",
"\n",
"import tensorflow as tf\n",
"import tensorflow.keras.backend as K\n",
"\n",
"# Layers for FNN\n",
"from tensorflow.keras.models import Sequential, Model\n",
"from tensorflow.keras.layers import Input, Lambda, concatenate\n",
"from tensorflow.keras.layers import Dense\n",
"\n",
"# Optimizers for training\n",
"from tensorflow.keras.optimizers import SGD, Adam\n",
"from tensorflow.keras import metrics\n",
"\n",
"# Losses for training\n",
"from tensorflow.keras import losses\n",
"\n",
"# For data preprocessing\n",
"from tensorflow.keras import datasets\n",
"from tensorflow.keras.utils import to_categorical"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HKVG73cPcuJf",
"outputId": "d99a5a3e-9933-4d73-f49d-e13a7424afc4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.15.0\n"
]
}
],
"source": [
"print(tf.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eG_sFPkvjG_V"
},
"source": [
"## 2: 下載並整理資料集"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "FJTYgrxPjG_W",
"outputId": "ebff1b64-ddfd-47e6-9960-0c795816884f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz\n",
"11490434/11490434 [==============================] - 0s 0us/step\n"
]
}
],
"source": [
"# Load dataset\n",
"(X_train, y_train0), (X_test, y_test0) = datasets.mnist.load_data()\n",
"\n",
"# Reshape size\n",
"X_train = X_train.reshape(-1, 28*28)\n",
"X_test = X_test.reshape(-1, 28*28)\n",
"\n",
"# Normalize the range of featurs\n",
"X_train = X_train / X_train.max()\n",
"X_test = X_test / X_test.max()\n",
"\n",
"# One-hot encoding\n",
"y_train = to_categorical(y_train0, 10)\n",
"y_test = to_categorical(y_test0, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_ZqHFLVdjG_a"
},
"source": [
"## 3 Autoencoder"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_M1iP6fQjG_b"
},
"source": [
"Autoencoder,又稱為自編碼器,是一個將資料壓縮再還原的模型,通常由一編碼器及一解碼器所組成。\n",
"\n",
"
\n",
"
\n",
"
\n",
"
\n",
"
inBetween
def inBetween(t)
<no docstring>