Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding TCN Layers + Tutorial to TFA Layers #692

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4bb44df
Adding files, modify README, BUILD, __init__
shun-lin Nov 8, 2019
a8c0e84
remove nb_ in variable naming as do not confine to tensorflow naming …
shun-lin Nov 8, 2019
96eb25e
clean equal sign format
shun-lin Nov 10, 2019
bdbf308
improve documentation
shun-lin Nov 10, 2019
9290146
simplify padding check
shun-lin Nov 10, 2019
4db6f1e
delete non-used code and changed default behavior of TCN
shun-lin Nov 10, 2019
dfe3828
changed back the dilations
shun-lin Nov 11, 2019
8c60445
added basic ipynb tutorial and update get_config
shun-lin Nov 11, 2019
011f2d5
update tutorial
shun-lin Nov 11, 2019
700d011
added a simple base test, update tcn docs, update example codelab
shun-lin Nov 12, 2019
88f9caf
fix tcn_test.py
shun-lin Nov 12, 2019
1998ef0
delete tensorflow.keras.layers imports to use tf.keras.layers directly
shun-lin Nov 16, 2019
7736b5a
update residual block's __init__ and build so that we initiailze laye…
shun-lin Nov 18, 2019
0e3568b
update tcn layer's __init__ and build method so that we initialize la…
shun-lin Nov 18, 2019
ea6050f
sort imports, BUILD, README.md, and some formatting on tcn.py
shun-lin Nov 19, 2019
ec0494d
change one of the subtitle from compute the model to compile the model
shun-lin Nov 19, 2019
4d977a0
changed the variable name for lambda layer and initilize it only when…
shun-lin Nov 19, 2019
ede2428
added test for serialization and deserialization
shun-lin Nov 19, 2019
b8664d7
fix lambda styling error
shun-lin Nov 19, 2019
82e90e2
Remove env/bin/Tensorboard
shun-lin Nov 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 310 additions & 0 deletions docs/tutorials/temporal_convolutional_network.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Copyright 2019 The TensorFlow Authors."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TensorFlow Addons Layers: Temporal Convolutional Network"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://www.tensorflow.org/addons/tutorials/temporal_convolutional_network\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/addons/blob/master/docs/tutorials/temporal_convolutional_network.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
" </td>\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://github.com/tensorflow/addons/blob/master/docs/tutorials/temporal_convolutional_network.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://storage.googleapis.com/tensorflow_docs/addons/docs/tutorials/temporal_convolutional_network.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"This notebook will demonstrate how to use Temporal Convolutional Network in TensorFlow Addons."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" # %tensorflow_version only exists in Colab.\n",
" %tensorflow_version 2.x\n",
"except Exception:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import tensorflow as tf\n",
"import tensorflow_addons as tfa\n",
"\n",
"import tensorflow.keras as keras\n",
"from tensorflow.keras.datasets import imdb\n",
"from tensorflow.keras.models import Model\n",
"from tensorflow.keras import Input\n",
"from tensorflow.keras.layers import Dense, Dropout, Embedding\n",
"from tensorflow.keras.preprocessing import sequence"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Global Configurations"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"max_features = 20000\n",
"# cut texts after this number of words\n",
"# (among top max_features most common words)\n",
"maxlen = 100\n",
"batch_size = 32"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import IMDB data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# the data, split between train and test sets\n",
"(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)\n",
"\n",
"# pad the training data\n",
"x_train = sequence.pad_sequences(x_train, maxlen=maxlen)\n",
"x_test = sequence.pad_sequences(x_test, maxlen=maxlen)\n",
"y_train = np.array(y_train)\n",
"y_test = np.array(y_test)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build Simple TCN Model"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# build the model using functional API\n",
"i = Input(shape=(maxlen,))\n",
"x = Embedding(max_features, 128)(i)\n",
"x = tfa.layers.TCN()(x)\n",
"x = Dropout(0.2)(x)\n",
"x = Dense(1, activation='sigmoid')(x)\n",
"\n",
"model = Model(inputs=[i], outputs=[x])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Summary of the model"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"model\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"input_1 (InputLayer) [(None, 100)] 0 \n",
"_________________________________________________________________\n",
"embedding (Embedding) (None, 100, 128) 2560000 \n",
"_________________________________________________________________\n",
"tcn (TCN) (None, 64) 148800 \n",
"_________________________________________________________________\n",
"dropout (Dropout) (None, 64) 0 \n",
"_________________________________________________________________\n",
"dense (Dense) (None, 1) 65 \n",
"=================================================================\n",
"Total params: 2,708,865\n",
"Trainable params: 2,708,865\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"model.summary()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compile the model"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"model.compile(optimizer='adam',\n",
" loss = 'binary_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fit the model"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Train on 25000 samples, validate on 25000 samples\n",
"Epoch 1/3\n",
"25000/25000 [==============================] - 100s 4ms/sample - loss: 0.5734 - accuracy: 0.6722 - val_loss: 0.4147 - val_accuracy: 0.8041\n",
"Epoch 2/3\n",
"25000/25000 [==============================] - 95s 4ms/sample - loss: 0.2852 - accuracy: 0.8811 - val_loss: 0.4136 - val_accuracy: 0.8128\n",
"Epoch 3/3\n",
"25000/25000 [==============================] - 97s 4ms/sample - loss: 0.1372 - accuracy: 0.9485 - val_loss: 0.4949 - val_accuracy: 0.8190\n"
]
},
{
"data": {
"text/plain": [
"<tensorflow.python.keras.callbacks.History at 0x109a5b4e0>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.fit(x_train, y_train,\n",
" batch_size=batch_size,\n",
" epochs=3,\n",
" validation_data=(x_test, y_test))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
14 changes: 14 additions & 0 deletions tensorflow_addons/layers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ py_library(
"optical_flow.py",
"poincare.py",
"sparsemax.py",
"tcn.py",
"wrappers.py",
],
data = [
Expand Down Expand Up @@ -101,3 +102,16 @@ py_test(
":layers",
],
)

py_test(
name = "tcn_test",
size = "small",
srcs = [
"tcn_test.py",
],
main = "tcn_test.py",
srcs_version = "PY2AND3",
deps = [
":layers",
],
)
4 changes: 3 additions & 1 deletion tensorflow_addons/layers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| opticalflow | @fsx950223 | [email protected] |
| poincare | @rahulunair | [email protected] |
| sparsemax | @AndreasMadsen | [email protected] |
| tcn | @shun-lin | [email protected] |
| wrappers | @seanpmorgan | [email protected] |

## Components
Expand All @@ -20,7 +21,8 @@
| normalizations | InstanceNormalization | https://arxiv.org/abs/1607.08022 |
| opticalflow | CorrelationCost | https://arxiv.org/abs/1504.06852 |
| poincare | PoincareNormalize | https://arxiv.org/abs/1705.08039 |
| sparsemax| Sparsemax | https://arxiv.org/abs/1602.02068 |
| sparsemax | Sparsemax | https://arxiv.org/abs/1602.02068 |
| tcn | TCN (Temporal Convolutional Network) | https://arxiv.org/pdf/1803.01271 |
| wrappers | WeightNormalization | https://arxiv.org/abs/1602.07868 |

## Contribution Guidelines
Expand Down
11 changes: 5 additions & 6 deletions tensorflow_addons/layers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
# ==============================================================================
"""Additional layers that conform to Keras API."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import, division, print_function

from tensorflow_addons.layers.gelu import GeLU
from tensorflow_addons.layers.maxout import Maxout
from tensorflow_addons.layers.normalizations import GroupNormalization
from tensorflow_addons.layers.normalizations import InstanceNormalization
from tensorflow_addons.layers.normalizations import (GroupNormalization,
InstanceNormalization)
from tensorflow_addons.layers.optical_flow import CorrelationCost
from tensorflow_addons.layers.poincare import PoincareNormalize
from tensorflow_addons.layers.sparsemax import Sparsemax
from tensorflow_addons.layers.wrappers import WeightNormalization
from tensorflow_addons.layers.tcn import TCN
from tensorflow_addons.layers.wrappers import WeightNormalization
Loading