{
"cells": [
{
"cell_type": "markdown",
"id": "ba021202",
"metadata": {
"id": "ba021202"
},
"source": [
"# **自定義類別物件(Custom class object)**\n",
"此份程式碼會介紹如何使用 class 物件,自定義 Loss、Layer 或者 Model。\n",
"\n",
"## 本章節內容大綱\n",
"* ### [Custom Loss](#Loss)\n",
"* ### [Custom Layer](#Layer)\n",
"* ### [Custom Model](#Model)\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "1713f048",
"metadata": {
"id": "1713f048"
},
"source": [
"## 匯入套件"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eb30f987",
"metadata": {
"id": "eb30f987"
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"from tensorflow.keras import layers"
]
},
{
"cell_type": "markdown",
"id": "55b83671",
"metadata": {
"id": "55b83671"
},
"source": [
"\n",
"## Custom Loss"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "45ca1165",
"metadata": {
"id": "45ca1165"
},
"outputs": [],
"source": [
"y_true = tf.random.normal((10, 4))\n",
"y_pred = tf.random.normal((10, 4))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b04bffaa",
"metadata": {
"id": "b04bffaa"
},
"outputs": [],
"source": [
"# build loss by tf.keras\n",
"mse_loss = keras.losses.MeanSquaredError()\n",
"mse_loss(y_true, y_pred)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "79d05a6d",
"metadata": {
"id": "79d05a6d"
},
"outputs": [],
"source": [
"class my_mse(keras.losses.Loss): # build loss object by custom class\n",
" def call(self, y_true, y_pred):\n",
" return tf.reduce_mean(tf.math.square(y_pred - y_true), axis=-1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4d77c0af",
"metadata": {
"id": "4d77c0af"
},
"outputs": [],
"source": [
"my_mse_loss = my_mse()\n",
"my_mse_loss(y_true, y_pred)"
]
},
{
"cell_type": "markdown",
"id": "50ec579c",
"metadata": {
"id": "50ec579c"
},
"source": [
"\n",
"## Custom Layer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce12852d",
"metadata": {
"id": "ce12852d"
},
"outputs": [],
"source": [
"x = tf.random.normal((10, 5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5177b88",
"metadata": {
"id": "d5177b88"
},
"outputs": [],
"source": [
"# build layer by tf.keras\n",
"dense_layer = keras.layers.Dense(\n",
" 4,\n",
" input_shape=x[0].shape,\n",
" kernel_initializer=tf.random_normal_initializer(seed=17)) # 初始化參數"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17644d65",
"metadata": {
"id": "17644d65"
},
"outputs": [],
"source": [
"dense_layer(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aaa644f2",
"metadata": {
"id": "aaa644f2"
},
"outputs": [],
"source": [
"class my_dense(keras.layers.Layer): # build layer object by custom class\n",
" def __init__(self, units=4, input_dim=5):\n",
" super(my_dense, self).__init__()\n",
" self.w = self.add_weight(\n",
" shape=(input_dim, units),\n",
" initializer=tf.random_normal_initializer(seed=17), # 初始化參數\n",
" trainable=True)\n",
" self.b = self.add_weight(\n",
" shape=(units,),\n",
" initializer=tf.zeros_initializer(), # 初始化參數\n",
" trainable=True)\n",
"\n",
" def call(self, inputs):\n",
" return tf.matmul(inputs, self.w) + self.b"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b537d78",
"metadata": {
"id": "6b537d78"
},
"outputs": [],
"source": [
"my_dense_layer = my_dense()\n",
"my_dense_layer(x)"
]
},
{
"cell_type": "markdown",
"id": "db06cb92",
"metadata": {
"id": "db06cb92"
},
"source": [
"\n",
"## Custom Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "33d759c9",
"metadata": {
"id": "33d759c9"
},
"outputs": [],
"source": [
"num_classes = 4"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e9e0632",
"metadata": {
"id": "7e9e0632"
},
"outputs": [],
"source": [
"# build model by tf.keras\n",
"keras.backend.clear_session()\n",
"model = keras.models.Sequential()\n",
"model.add(layers.Dense(\n",
" 16, # 神經元個數\n",
" kernel_initializer=tf.random_normal_initializer(seed=17), # 初始化參數\n",
" input_shape=x[0].shape)) # 輸入形狀\n",
"model.add(layers.Dense(\n",
" 32, # 神經元個數\n",
" kernel_initializer=tf.random_normal_initializer(seed=17))) # 初始化參數\n",
"model.add(layers.Dense(\n",
" num_classes,\n",
" kernel_initializer=tf.random_normal_initializer(seed=17))) # 初始化參數"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "845edda5",
"metadata": {
"id": "845edda5"
},
"outputs": [],
"source": [
"model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "127d3416",
"metadata": {
"id": "127d3416"
},
"outputs": [],
"source": [
"model(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2dc3d997",
"metadata": {
"id": "2dc3d997"
},
"outputs": [],
"source": [
"class my_net(keras.Model): # build model object by custom class\n",
" def __init__(self, num_classes=4):\n",
" super(my_net, self).__init__()\n",
" keras.backend.clear_session() # 重置 keras 的所有狀態\n",
" self.input_layer = layers.Input(shape=(5,))\n",
" self.hidden_layer_1 = layers.Dense(\n",
" 16, # 神經元個數\n",
" kernel_initializer=tf.random_normal_initializer(seed=17)) # 初始化參數\n",
" self.hidden_layer_2 = layers.Dense(\n",
" 32, # 神經元個數\n",
" kernel_initializer=tf.random_normal_initializer(seed=17)) # 初始化參數\n",
" self.output_layer = layers.Dense(\n",
" num_classes,\n",
" kernel_initializer=tf.random_normal_initializer(seed=17)) # 初始化參數\n",
" self.out = self.call(self.input_layer)\n",
"\n",
" def call(self, inputs):\n",
" x = self.hidden_layer_1(inputs)\n",
" x = self.hidden_layer_2(x)\n",
" outputs = self.output_layer(x)\n",
" return outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02616ea1",
"metadata": {
"id": "02616ea1"
},
"outputs": [],
"source": [
"my_model = my_net()\n",
"my_model.build(input_shape=(None, 5))\n",
"my_model.summary()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ae97735a",
"metadata": {
"id": "ae97735a"
},
"outputs": [],
"source": [
"my_model(x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c37b4fdb",
"metadata": {
"id": "c37b4fdb"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
},
"colab": {
"provenance": []
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"nbformat": 4,
"nbformat_minor": 5
}