Skip to content

Commit

Permalink
version 0.1.1, supported serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
eliorc committed Jun 2, 2019
1 parent 1ab06a1 commit 6117c5d
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
.. image:: https://img.shields.io/badge/Python-3.5%20%7C%203.6%20%7C%203.7-blue.svg
:alt: Supported Python versions

.. image:: https://img.shields.io/badge/tensorflow-2.0.0--alpha0-orange.svg
:alt: Supported TensorFlow versions

.. image:: https://codecov.io/gh/eliorc/tavolo/branch/master/graph/badge.svg
:target: https://codecov.io/gh/eliorc/tavolo
:alt: Code test coverage
Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from setuptools import setup

VERSION = '0.1.0'
VERSION = '0.1.1'

setup(name='tavolo',
version=VERSION,
description='Collection of deep learning modules and layers for the TensorFlow framework',
url='https://github.com/eliorc/tavolo',
author='Elior Cohen',
classifiers=[
'License :: OSI Approved :: MIT License',
],
classifiers=['License :: OSI Approved :: MIT License'],
packages=['tavolo'],
install_requires=[
'numpy',
Expand Down
2 changes: 1 addition & 1 deletion tavolo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__name__ = 'tavolo'
__version__ = '0.1.0'
__version__ = '0.1.1'

from . import embeddings
from . import normalization
Expand Down
20 changes: 18 additions & 2 deletions tavolo/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def __init__(self,
"""
super().__init__(name=name, **kwargs)

self.max_sequence_length = max_sequence_length
self.embedding_dim = embedding_dim
self.normalize_factor = normalize_factor

# Error checking
if max_sequence_length < 1:
raise ValueError(
Expand Down Expand Up @@ -108,9 +112,21 @@ def call(self, inputs,
mask: Optional[tf.Tensor] = None,
**kwargs) -> tf.Tensor:


output = inputs + self.positional_encoding
if mask is not None:
output = tf.where(tf.tile(tf.expand_dims(mask, axis=-1), multiples=[1, 1, inputs.shape[-1]]), output, inputs)
output = tf.where(tf.tile(tf.expand_dims(mask, axis=-1), multiples=[1, 1, inputs.shape[-1]]), output,
inputs)

return output # shape=(batch_size, time_steps, channels)

def get_config(self):
base_config = super().get_config()
base_config['max_sequence_length'] = self.max_sequence_length
base_config['embedding_dim'] = self.embedding_dim
base_config['normalize_factor'] = self.normalize_factor

return base_config

@classmethod
def from_config(cls, config):
return cls(**config)
10 changes: 10 additions & 0 deletions tavolo/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ def call(self, inputs,
normalized = (inputs - mean) / ((variance + self.epsilon) ** .5) # shape=(batch_size, channels)

return self.gamma * normalized + self.beta # shape=(batch_size, channels)

def get_config(self):
base_config = super().get_config()
base_config['epsilon'] = self.epsilon

return base_config

@classmethod
def from_config(cls, config):
return cls(**config)
14 changes: 14 additions & 0 deletions tavolo/seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self,

self.n_heads = n_heads
self.n_units = n_units
self.dropout_rate = dropout_rate
self.causality = causality
self.Q = None
self.K = None
Expand Down Expand Up @@ -204,3 +205,16 @@ def call(self, inputs,
axis=2) # shape=(batch_size, time_steps, n_units)

return outputs

def get_config(self):
base_config = super().get_config()
base_config['n_heads'] = self.n_heads
base_config['n_units'] = self.n_units
base_config['dropout_rate'] = self.dropout_rate
base_config['causality'] = self.causality

return base_config

@classmethod
def from_config(cls, config):
return cls(**config)
10 changes: 10 additions & 0 deletions tavolo/seq2vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,13 @@ def call(self, inputs,
output = tf.reduce_sum(inputs * tf.expand_dims(alphas, axis=-1), axis=1) # shape=(batch_size, channels)

return output

def get_config(self):
base_config = super().get_config()
base_config['n_units'] = self.n_units

return base_config

@classmethod
def from_config(cls, config):
return cls(**config)
10 changes: 10 additions & 0 deletions tests/embeddings/positional_encoding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ def test_logic():

# Assert output correctness
assert tf.reduce_sum(positional_encoding(inputs_3d) - positional_encoding.positional_encoding).numpy() == 0


def test_serialization():
""" Test layer serialization (get_config, from_config) """

simple = PositionalEncoding(max_sequence_length=100,
embedding_dim=24)
restored = PositionalEncoding.from_config(simple.get_config())

assert restored.get_config() == simple.get_config()
9 changes: 9 additions & 0 deletions tests/normalization/layer_norm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ def test_logic():

# Assert output correctness
assert tf.reduce_sum(layer_norm_2d(inputs_2d)).numpy() == 0


def test_serialization():
""" Test layer serialization (get_config, from_config) """

simple = LayerNorm()
restored = LayerNorm.from_config(simple.get_config())

assert restored.get_config() == simple.get_config()
9 changes: 9 additions & 0 deletions tests/seq2seq/multi_headed_self_attention_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ def test_causality():
# Assert last step is different but the rest not affected
assert (result[:, :-1, :].numpy() == result_[:, :-1, :].numpy()).all() # Without last step
assert not (result.numpy() == result_.numpy()).all()


def test_serialization():
""" Test layer serialization (get_config, from_config) """

simple = MultiHeadedSelfAttention()
restored = MultiHeadedSelfAttention.from_config(simple.get_config())

assert restored.get_config() == simple.get_config()
9 changes: 9 additions & 0 deletions tests/seq2vec/yang_attention_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ def test_masking():
result = yang_attention(masking_layer(masked_input))

assert result.shape == (input_shape_3d[0], input_shape_3d[-1])


def test_serialization():
""" Test layer serialization (get_config, from_config) """

simple = YangAttention(n_units=56)
restored = YangAttention.from_config(simple.get_config())

assert restored.get_config() == simple.get_config()

0 comments on commit 6117c5d

Please sign in to comment.