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

Support tf.keras #74

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 12 additions & 4 deletions tasks/imdb_tcn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions tasks/multi_length_sequences.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 7 additions & 2 deletions tasks/save_reload_model.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 10 additions & 3 deletions tasks/time_series_forecasting.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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())

Expand Down
32 changes: 22 additions & 10 deletions tcn/tcn.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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:
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.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
from keras import 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,
Expand Down Expand Up @@ -44,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

Expand Down Expand Up @@ -140,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
Expand Down