From 74e4bb7339d79b4841f27209f2e7e1e853d2869b Mon Sep 17 00:00:00 2001 From: Yu ISHIKAWA Date: Thu, 12 Sep 2019 21:44:37 -0700 Subject: [PATCH 1/4] Support tf.keras --- tcn/tcn.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/tcn/tcn.py b/tcn/tcn.py index e60df70..b3a7526 100644 --- a/tcn/tcn.py +++ b/tcn/tcn.py @@ -1,13 +1,23 @@ from typing import List, Tuple -import keras.backend as K -import keras.layers -from keras import optimizers -from keras.engine.topology import Layer -from keras.layers import Activation, Lambda -from keras.layers import Conv1D, SpatialDropout1D -from keras.layers import Dense, BatchNormalization -from keras.models import Input, Model +try: + import tensorflow.keras.backend as K + import tensorflow.keras.layers + from tensorflow.keras import optimizers + from tensorflow.keras.engine.topology import Layer + from tensorflow.keras.layers import Activation, Lambda + from tensorflow.keras.layers import Conv1D, SpatialDropout1D + from tensorflow.keras.layers import Dense, BatchNormalization + from tensorflow.keras.models import Input, Model +except: + import keras.backend as K + import keras.layers + from keras import optimizers + from keras.engine.topology import Layer + from keras.layers import Activation, Lambda + from keras.layers import Conv1D, SpatialDropout1D + from keras.layers import Dense, BatchNormalization + from keras.models import Input, Model def residual_block(x, training, dilation_rate, nb_filters, kernel_size, padding, activation='relu', dropout_rate=0, From 30832aadd07fb6eae8db2e16c97c56efdc7c325a Mon Sep 17 00:00:00 2001 From: Yu ISHIKAWA Date: Fri, 13 Sep 2019 00:39:54 -0700 Subject: [PATCH 2/4] Modify the namespace to Layer --- tcn/tcn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tcn/tcn.py b/tcn/tcn.py index b3a7526..19e6e91 100644 --- a/tcn/tcn.py +++ b/tcn/tcn.py @@ -4,7 +4,7 @@ import tensorflow.keras.backend as K import tensorflow.keras.layers from tensorflow.keras import optimizers - from tensorflow.keras.engine.topology import Layer + from tensorflow.python.keras.engine import Layer from tensorflow.keras.layers import Activation, Lambda from tensorflow.keras.layers import Conv1D, SpatialDropout1D from tensorflow.keras.layers import Dense, BatchNormalization From 081c9284120986fcd9aa676025e1e6f7704ec7e7 Mon Sep 17 00:00:00 2001 From: Yu ISHIKAWA Date: Fri, 20 Sep 2019 12:00:47 -0700 Subject: [PATCH 3/4] Modify TCN module --- tcn/tcn.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tcn/tcn.py b/tcn/tcn.py index 19e6e91..54ad011 100644 --- a/tcn/tcn.py +++ b/tcn/tcn.py @@ -1,17 +1,19 @@ from typing import List, Tuple try: - import tensorflow.keras.backend as K - import tensorflow.keras.layers - from tensorflow.keras import optimizers + from tensorflow.python import keras + from tensorflow.python.keras import backend as k + from tensorflow.python.keras import layers + from tensorflow.python.keras import optimizers from tensorflow.python.keras.engine import Layer - from tensorflow.keras.layers import Activation, Lambda - from tensorflow.keras.layers import Conv1D, SpatialDropout1D - from tensorflow.keras.layers import Dense, BatchNormalization - from tensorflow.keras.models import Input, Model + from tensorflow.python.keras.layers import Activation, Lambda + from tensorflow.python.keras.layers import Conv1D, SpatialDropout1D + from tensorflow.python.keras.layers import Dense, BatchNormalization + from tensorflow.python.keras.models import Input, Model except: + import keras import keras.backend as K - import keras.layers + from keras import layers from keras import optimizers from keras.engine.topology import Layer from keras.layers import Activation, Lambda @@ -54,7 +56,7 @@ def residual_block(x, training, dilation_rate, nb_filters, kernel_size, padding, # 1x1 conv to match the shapes (channel dimension). prev_x = Conv1D(nb_filters, 1, padding='same')(prev_x) - res_x = keras.layers.add([prev_x, x]) + res_x = layers.add([prev_x, x]) res_x = Activation(activation)(res_x) return res_x, x @@ -150,7 +152,7 @@ def __call__(self, inputs, training=None): use_batch_norm=self.use_batch_norm) skip_connections.append(skip_out) if self.use_skip_connections: - x = keras.layers.add(skip_connections) + x = layers.add(skip_connections) if not self.return_sequences: x = Lambda(lambda tt: tt[:, -1, :])(x) return x From cc29d301e38b334e17d68cf5db3186b5af802e5a Mon Sep 17 00:00:00 2001 From: Yu ISHIKAWA Date: Fri, 20 Sep 2019 12:01:01 -0700 Subject: [PATCH 4/4] Modify the tasks --- tasks/imdb_tcn.py | 16 ++++++++++++---- tasks/multi_length_sequences.py | 9 +++++++-- tasks/save_reload_model.py | 9 +++++++-- tasks/time_series_forecasting.py | 13 ++++++++++--- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/tasks/imdb_tcn.py b/tasks/imdb_tcn.py index 9580749..cb5a0ab 100644 --- a/tasks/imdb_tcn.py +++ b/tasks/imdb_tcn.py @@ -5,10 +5,18 @@ Based on: https://github.com/keras-team/keras/blob/master/examples/imdb_bidirectional_lstm.py """ import numpy as np -from keras import Model, Input -from keras.datasets import imdb -from keras.layers import Dense, Dropout, Embedding -from keras.preprocessing import sequence +try: + from tensorflow.python import keras + from tensorflow.python.keras import Model, Input + from tensorflow.python.keras.datasets import imdb + from tensorflow.python.keras.layers import Dense, Dropout, Embedding + from tensorflow.python.keras.preprocessing import sequence +except: + import keras + from keras import Model, Input + from keras.datasets import imdb + from keras.layers import Dense, Dropout, Embedding + from keras.preprocessing import sequence from tcn import TCN diff --git a/tasks/multi_length_sequences.py b/tasks/multi_length_sequences.py index cb8c94f..6706c29 100644 --- a/tasks/multi_length_sequences.py +++ b/tasks/multi_length_sequences.py @@ -1,6 +1,11 @@ import numpy as np -from keras.layers import Dense -from keras.models import Input, Model + +try: + from tensorflow.python.keras.layers import Dense + from tensorflow.python.keras.models import Input, Model +except: + from keras.layers import Dense + from keras.models import Input, Model from tcn import TCN diff --git a/tasks/save_reload_model.py b/tasks/save_reload_model.py index 68dd541..8907670 100644 --- a/tasks/save_reload_model.py +++ b/tasks/save_reload_model.py @@ -1,8 +1,13 @@ import os import numpy as np -from keras import Model, Input -from keras.layers import Dense, Dropout, Embedding + +try: + from tensorflow.python.keras import Model, Input + from tensorflow.python.keras.layers import Dense, Dropout, Embedding +except: + from keras import Model, Input + from keras.layers import Dense, Dropout, Embedding from tcn import TCN diff --git a/tasks/time_series_forecasting.py b/tasks/time_series_forecasting.py index 9479d60..a6235da 100644 --- a/tasks/time_series_forecasting.py +++ b/tasks/time_series_forecasting.py @@ -1,9 +1,15 @@ # https://datamarket.com/data/set/22ox/monthly-milk-production-pounds-per-cow-jan-62-dec-75#!ds=22ox&display=line +import os import matplotlib.pyplot as plt import numpy as np import pandas as pd -from keras import Input, Model -from keras.layers import Dense + +try: + from tensorflow.python.keras import Input, Model + from tensorflow.python.keras.layers import Dense +except: + from keras import Input, Model + from keras.layers import Dense from tcn import TCN @@ -14,7 +20,8 @@ # - The model is simple. ## -milk = pd.read_csv('monthly-milk-production-pounds-p.csv', index_col=0, parse_dates=True) +filepath = os.path.join(os.path.dirname(__file__), 'monthly-milk-production-pounds-p.csv') +milk = pd.read_csv(filepath, index_col=0, parse_dates=True) print(milk.head())