Skip to content

Commit

Permalink
Merge branch 'develop' into prune_ranks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bihaqo committed May 17, 2018
2 parents ac1da72 + 9e74c68 commit 2a9c68e
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ env:
- TF_VERSION=1.2
- TF_VERSION=1.3
- TF_VERSION=1.4
- TF_VERSION=1.6
- TF_VERSION=1.7
# command to install dependencies
install:
# Python 2.7 has very old pip by default that doesn't have tensorflow.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ TensorFlow implementation of the Tensor Train (TT) -Toolbox.
API is available via [readthedocs](https://t3f.readthedocs.io/en/latest/).

# Installation
T3f assumes you have a working TensorFlow installation, supported versions are from 1.0 to 1.4 (see [here](https://www.tensorflow.org/versions/r1.4/install/) for TF 1.4 installation instructions).
T3f assumes you have Python 2.7, 3.4, 3.5, or 3.6 and a working TensorFlow installation, supported versions are from 1.0 to 1.7 (see [here](https://www.tensorflow.org/versions/r1.7/install/) for TF 1.7 installation instructions).
We don't include it into pip requirements since the installation of TensorFlow varies depending on your setup.
Then simply run
```bash
Expand Down
11 changes: 9 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
contain the root `toctree` directive.
Welcome to t3f documentation!
===============================
=============================

Module contents
---------------
Expand All @@ -24,10 +24,17 @@ t3f\.utils module
:show-inheritance:

t3f\.kronecker module
-----------------
---------------------

.. automodule:: t3f.kronecker
:members:
:undoc-members:
:show-inheritance:

t3f\.approximate module
-----------------------

.. automodule:: t3f.approximate
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion docs/tf_requirement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# the docs with readthedocs.org: they fetch a fresh version of the
# library on each build and it doesn't import properly without tensorflow
# being installed.
tensorflow>=1.0
tensorflow>=1.0,<=1.5
16 changes: 16 additions & 0 deletions t3f/shapes_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import tensorflow as tf

from t3f import initializers
from t3f import shapes


class ShapesTest(tf.test.TestCase):

def testLazyShapeOverflow(self):
large_shape = [10] * 20
tensor = initializers.random_matrix_batch([large_shape, large_shape], batch_size=5)
self.assertAllEqual([5, 10 ** 20, 10 ** 20], shapes.lazy_shape(tensor))


if __name__ == "__main__":
tf.test.main()
15 changes: 0 additions & 15 deletions t3f/tensor_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ class TensorTrain(TensorTrainBase):
"""Represents a Tensor Train object (a TT-tensor or TT-matrix).
t3f represents a Tensor Train object as a tuple of TT-cores.
```
@@__init__
@@get_raw_shape
@@get_shape
@@tt_cores
@@dtype
@@name
@@graph
@@ndims
@@get_tt_ranks
@@left_tt_rank_dim
@@right_tt_rank_dim
@@is_tt_matrix
@@is_variable
@@eval
"""

def __init__(self, tt_cores, shape=None, tt_ranks=None, convert_to_tensors=True):
Expand Down
20 changes: 5 additions & 15 deletions t3f/tensor_train_base.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
from functools import reduce
import numpy as np
import tensorflow as tf


# TODO: check the methods of _TensorLike
class TensorTrainBase(object):
"""An abstract class that represents a collection of Tensor Train cores.
```
@@__init__
@@get_raw_shape
@@get_shape
@@tt_cores
@@dtype
@@name
@@graph
@@ndims
@@get_tt_ranks
@@is_tt_matrix
@@is_variable
@@eval
"""

def __init__(self, tt_cores):
Expand All @@ -43,9 +31,11 @@ def get_shape(self):
"""
raw_shape = self.get_raw_shape()
if self.is_tt_matrix():
# Use python prod instead of np.prod to avoid overflows.
prod_f = lambda arr: reduce(lambda x, y: x*y, arr)
# TODO: as list is not available if shape is partly known.
m = np.prod(raw_shape[0].as_list())
n = np.prod(raw_shape[1].as_list())
m = prod_f(raw_shape[0].as_list())
n = prod_f(raw_shape[1].as_list())
return tf.TensorShape((m, n))
else:
return raw_shape[0]
Expand Down
15 changes: 0 additions & 15 deletions t3f/tensor_train_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ class TensorTrainBatch(TensorTrainBase):
"""Represents a batch of Tensor Train objects (TT-tensors or TT-matrices).
t3f represents a Tensor Train object as a tuple of TT-cores.
```
@@__init__
@@get_raw_shape
@@get_shape
@@tt_cores
@@dtype
@@name
@@graph
@@ndims
@@get_tt_ranks
@@left_tt_rank_dim
@@right_tt_rank_dim
@@is_tt_matrix
@@is_variable
@@eval
"""

def __init__(self, tt_cores, shape=None, tt_ranks=None, batch_size=None,
Expand Down
6 changes: 6 additions & 0 deletions t3f/tensor_train_batch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def testPlaceholderTensorIndexing(self):
desired, actual = sess.run([desired, actual], {start: 1, end: 3})
self.assertAllClose(desired, actual)

def testShapeOverflow(self):
large_shape = [10] * 20
tensor = initializers.random_matrix_batch([large_shape, large_shape], batch_size=5)
shape = tensor.get_shape()
self.assertEqual([5, 10 ** 20, 10 ** 20], shape)


if __name__ == "__main__":
tf.test.main()
6 changes: 6 additions & 0 deletions t3f/tensor_train_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,11 @@ def testPlaceholderTensorIndexing(self):
desired, actual = sess.run([desired, actual], {start: 1, end: 3})
self.assertAllClose(desired, actual)

def testShapeOverflow(self):
large_shape = [10] * 20
matrix = initializers.matrix_zeros([large_shape, large_shape])
shape = matrix.get_shape()
self.assertEqual([10 ** 20, 10 ** 20], shape)

if __name__ == "__main__":
tf.test.main()
9 changes: 9 additions & 0 deletions t3f/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,12 @@ def max_tt_ranks(raw_shape):
right_to_left = robust_cumprod(raw_shape[::-1])[::-1]
tt_ranks[1:-1] = np.minimum(left_to_right[:-1], right_to_left[1:])
return tt_ranks


def in_eager_mode():
"""Checks whether tensorflow eager mode is avaialable and active."""
try:
from tensorflow.python.eager import context
return context.in_eager_mode()
except ImportError:
return False
4 changes: 3 additions & 1 deletion t3f/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from t3f.tensor_train import TensorTrain
from t3f.tensor_train_batch import TensorTrainBatch
from t3f import utils


def get_variable(name,
Expand Down Expand Up @@ -55,7 +56,8 @@ def get_variable(name,
raise ValueError('Scope reuse is False and initializer is not provided.')

variable_cores = []
if reuse:

if reuse and not utils.in_eager_mode():
# Find an existing variable in the collection.
path = tf.get_variable_scope().name
if path != '' and path[-1] != '/':
Expand Down

0 comments on commit 2a9c68e

Please sign in to comment.