Skip to content

Commit

Permalink
TST: Add rudimentary test for tensorflow
Browse files Browse the repository at this point in the history
  • Loading branch information
adler-j committed Aug 22, 2017
1 parent 127814e commit a97fcd5
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions odl/contrib/tensorflow/test/tensorflow_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2014-2017 The ODL contributors
#
# This file is part of ODL.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.

"""Tests for tensorflow."""

from __future__ import division
from itertools import permutations
import pytest
import numpy as np
import tensorflow as tf

import odl
import odl.contrib.tensorflow
from odl.util import all_almost_equal


def test_as_tensorflow_layer():
# Define ODL operator
matrix = np.random.rand(3, 2)
odl_op = odl.MatrixOperator(matrix)

# Define evaluation points
x = np.random.rand(2)
z = np.random.rand(3)

# Add empty axes for batch and channel
x_tf = tf.constant(x)[None, ..., None]
z_tf = tf.constant(z)[None, ..., None]

# Create tensorflow layer from odl operator
odl_op_layer = odl.contrib.tensorflow.as_tensorflow_layer(
odl_op, 'MatrixOperator')
y_tf = odl_op_layer(x_tf)

# Evaluate using tensorflow
result = y_tf.eval().ravel()
expected = odl_op(x)

assert all_almost_equal(result, expected)

# Evaluate the adjoint of the derivative, called gradient in tensorflow
result = tf.gradients(y_tf, [x_tf], z_tf)[0].eval().ravel()
expected = odl_op.derivative(x).adjoint(z)

assert all_almost_equal(result, expected)



if __name__ == '__main__':
with tf.Session():
pytest.main([str(__file__.replace('\\', '/')), '-v'])

0 comments on commit a97fcd5

Please sign in to comment.