Esempio n. 1
0
    def _test_max(self, axis, shape, keep_dims):
        with nn.variable_scope(self.get_scope()):
            tensor0 = fixture.create_ones_tensor(shape, dtype='float32')
            tensor1 = tensor0.max(axis=axis, keep_dims=keep_dims)

        session = nn.Session()

        val0, val1 = session.run(outputs=[tensor0, tensor1], )
        expected = val0.max(axis=axis, keepdims=keep_dims)
        np.testing.assert_equal(val1, expected)
Esempio n. 2
0
    def test_neg(self):
        """-Tensor is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            tensor2 = -tensor1

        session = nn.Session()
        val1, val2 = session.run(outputs=[tensor1, tensor2], )
        np.testing.assert_equal(-val1, val2)
Esempio n. 3
0
    def test_clip_tensor_by_value(self):
        """Test clip with Tensor"""
        shape, min_value, max_value = (10, 10), 0.4, 0.6
        with nn.variable_scope(self.get_scope()):
            variable0 = fixture.create_random_variable(shape, dtype='float32')
            min_tensor = min_value * fixture.create_ones_tensor(
                shape=[], dtype='float32', name='min_tensor')
            max_tensor = max_value * fixture.create_ones_tensor(
                shape=[], dtype='float32', name='max_tensor')
            tensor1 = nn.clip_by_value(variable0,
                                       max_value=max_tensor,
                                       min_value=min_tensor)

        session = nn.Session()
        session.initialize()

        val0, val1 = session.run(outputs=[variable0, tensor1], )
        expected = np.clip(val0, a_max=max_value, a_min=min_value)
        np.testing.assert_almost_equal(val1, expected)
Esempio n. 4
0
    def test_floordiv_tensor(self):
        """Tensor // Tensor is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
            tensor2 = tensor1 // tensor1

        session = nn.Session()

        val1, val2 = session.run(outputs=[tensor1, tensor2], )
        np.testing.assert_equal(np.floor_divide(val1, val1), val2)
Esempio n. 5
0
    def test_get_tensor_from_current_scope(self):
        """get_tensor retrieve existing tensor"""
        scope, name = self.get_scope(), 'foo'
        with nn.variable_scope(scope):
            tensor = fixture.create_ones_tensor([3, 1], 'float32', name=name)
            self.assertIs(tensor, nn.get_tensor(name))

        self.assertIs(tensor, nn.get_tensor('{}/{}'.format(scope, name)))

        with self.assertRaises(ValueError):
            nn.get_tensor(name)
Esempio n. 6
0
    def test_clip_tensor_by_value(self):
        """Test clip with Tensor"""
        shape, min_value, max_value = (10, 10), 0.4, 0.6
        with nn.variable_scope(self.get_scope()):
            variable0 = fixture.create_random_variable(shape, dtype='float32')
            min_tensor = min_value * fixture.create_ones_tensor(
                shape=[], dtype='float32', name='min_tensor')
            max_tensor = max_value * fixture.create_ones_tensor(
                shape=[], dtype='float32', name='max_tensor')
            tensor1 = nn.ops.clip_by_value(
                variable0, max_value=max_tensor, min_value=min_tensor)

        session = nn.Session()
        session.initialize()

        val0, val1 = session.run(
            outputs=[variable0, tensor1],
        )
        expected = np.clip(val0, a_max=max_value, a_min=min_value)
        np.testing.assert_almost_equal(val1, expected)
Esempio n. 7
0
    def test_get_tensor_from_current_scope(self):
        """get_tensor retrieve existing tensor"""
        scope, name = self.get_scope(), 'foo'
        with nn.variable_scope(scope):
            tensor = fixture.create_ones_tensor([3, 1], 'float32', name=name)
            self.assertIs(tensor, nn.get_tensor(name))

        self.assertIs(tensor, nn.get_tensor('{}/{}'.format(scope, name)))

        with self.assertRaises(ValueError):
            nn.get_tensor(name)
Esempio n. 8
0
    def test_neg(self):
        """-Tensor is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            tensor2 = -tensor1

        session = nn.Session()
        val1, val2 = session.run(
            outputs=[tensor1, tensor2],
        )
        np.testing.assert_equal(-val1, val2)
Esempio n. 9
0
    def test_floordiv_tensor(self):
        """Tensor // Tensor is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
            tensor2 = tensor1 // tensor1

        session = nn.Session()

        val1, val2 = session.run(
            outputs=[tensor1, tensor2],
        )
        np.testing.assert_equal(np.floor_divide(val1, val1), val2)
Esempio n. 10
0
    def test_floordiv_numbers(self):
        """Tensor // number is correct elementwise"""
        constant, shape = 10., (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
            tensor2 = tensor1 // constant
            tensor3 = constant // tensor1

        session = nn.Session()

        val1, val2, val3 = session.run(outputs=[tensor1, tensor2, tensor3], )
        np.testing.assert_equal(np.floor_divide(val1, constant), val2)
        np.testing.assert_equal(np.floor_divide(constant, val1), val3)
Esempio n. 11
0
    def test_sub_numbers(self):
        """Tensor - number is correct elementwise"""
        constant, shape = 10, (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            tensor2 = tensor1 - constant
            tensor3 = constant - tensor1

        session = nn.Session()

        val1, val2, val3 = session.run(outputs=[tensor1, tensor2, tensor3], )
        np.testing.assert_equal(val1 - constant, val2)
        np.testing.assert_equal(constant - val1, val3)
Esempio n. 12
0
    def test_mul_numbers(self):
        """Tensor * number is correct elementwise"""
        constant, shape = 10, (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            tensor2 = tensor1 * constant
            tensor3 = constant * tensor1

        session = nn.Session()

        val1, val2, val3 = session.run(
            outputs=[tensor1, tensor2, tensor3],
        )
        np.testing.assert_equal(val1 * constant, val2)
        np.testing.assert_equal(constant * val1, val3)
Esempio n. 13
0
    def test_floordiv_numbers(self):
        """Tensor // number is correct elementwise"""
        constant, shape = 10., (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
            tensor2 = tensor1 // constant
            tensor3 = constant // tensor1

        session = nn.Session()

        val1, val2, val3 = session.run(
            outputs=[tensor1, tensor2, tensor3],
        )
        np.testing.assert_equal(np.floor_divide(val1, constant), val2)
        np.testing.assert_equal(np.floor_divide(constant, val1), val3)
Esempio n. 14
0
    def test_sub_input(self):
        """Tensor - Input is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            input_ = nn.Input(shape=[], dtype='int32')
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            tensor2 = tensor1 - input_
            tensor3 = input_ - tensor1

        session = nn.Session()
        val0 = np.array(10, dtype='int32')
        val1, val2, val3 = session.run(outputs=[tensor1, tensor2, tensor3],
                                       givens={input_: val0})
        np.testing.assert_equal(val1 - val0, val2)
        np.testing.assert_equal(val0 - val1, val3)
Esempio n. 15
0
    def test_mul_variable(self):
        """Tensor * Variable is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            variable = fixture.create_constant_variable(shape, dtype='int32')
            tensor2 = tensor1 * variable
            tensor3 = variable * tensor1

        session = nn.Session()
        session.initialize()

        val1, val2, val3, val0 = session.run(
            outputs=[tensor1, tensor2, tensor3, variable], )
        np.testing.assert_equal(val1 * val0, val2)
        np.testing.assert_equal(val0 * val1, val3)
Esempio n. 16
0
    def test_mul_input(self):
        """Tensor * Input is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            input_ = nn.Input(name='input', shape=[], dtype='int32')
            tensor2 = tensor1 * input_
            tensor3 = input_ * tensor1

        session = nn.Session()
        session.initialize()

        input_val = np.array(5, dtype='int32')
        val1, val2, val3 = session.run(outputs=[tensor1, tensor2, tensor3],
                                       givens={input_: input_val})
        np.testing.assert_equal(val1 * input_val, val2)
        np.testing.assert_equal(input_val * val1, val3)
Esempio n. 17
0
    def test_mul_variable(self):
        """Tensor * Variable is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            variable = fixture.create_constant_variable(shape, dtype='int32')
            tensor2 = tensor1 * variable
            tensor3 = variable * tensor1

        session = nn.Session()
        session.initialize()

        val1, val2, val3, val0 = session.run(
            outputs=[tensor1, tensor2, tensor3, variable],
        )
        np.testing.assert_equal(val1 * val0, val2)
        np.testing.assert_equal(val0 * val1, val3)
Esempio n. 18
0
    def test_sub_input(self):
        """Tensor - Input is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            input_ = nn.Input(shape=[], dtype='int32')
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            tensor2 = tensor1 - input_
            tensor3 = input_ - tensor1

        session = nn.Session()
        val0 = np.array(10, dtype='int32')
        val1, val2, val3 = session.run(
            outputs=[tensor1, tensor2, tensor3],
            givens={input_: val0}
        )
        np.testing.assert_equal(val1 - val0, val2)
        np.testing.assert_equal(val0 - val1, val3)
Esempio n. 19
0
    def test_floordiv_input(self):
        """Tensor // Input is correct elementwise"""
        constant, shape = 10., (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
            input1 = nn.Input(shape=[], dtype='float32')
            tensor2 = tensor1 // input1
            tensor3 = input1 // tensor1

        session = nn.Session()
        session.initialize()

        val1, val2, val3 = session.run(
            outputs=[tensor1, tensor2, tensor3],
            givens={input1: np.array(constant, dtype='float32')})
        np.testing.assert_equal(np.floor_divide(val1, constant), val2)
        np.testing.assert_equal(np.floor_divide(constant, val1), val3)
Esempio n. 20
0
    def test_floordiv_input(self):
        """Tensor // Input is correct elementwise"""
        constant, shape = 10., (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
            input1 = nn.Input(shape=[], dtype='float32')
            tensor2 = tensor1 // input1
            tensor3 = input1 // tensor1

        session = nn.Session()
        session.initialize()

        val1, val2, val3 = session.run(
            outputs=[tensor1, tensor2, tensor3],
            givens={input1: np.array(constant, dtype='float32')}
        )
        np.testing.assert_equal(np.floor_divide(val1, constant), val2)
        np.testing.assert_equal(np.floor_divide(constant, val1), val3)
Esempio n. 21
0
    def test_mul_input(self):
        """Tensor * Input is correct elementwise"""
        shape = (3, 5)
        with nn.variable_scope(self.get_scope()):
            tensor1 = fixture.create_ones_tensor(shape, dtype='int32')
            input_ = nn.Input(name='input', shape=[], dtype='int32')
            tensor2 = tensor1 * input_
            tensor3 = input_ * tensor1

        session = nn.Session()
        session.initialize()

        input_val = np.array(5, dtype='int32')
        val1, val2, val3 = session.run(
            outputs=[tensor1, tensor2, tensor3],
            givens={input_: input_val}
        )
        np.testing.assert_equal(val1 * input_val, val2)
        np.testing.assert_equal(input_val * val1, val3)