def test_filter_sanity_check(self): # Simple test on typed list type filter myType = TypedListType(T.TensorType(theano.config.floatX, (False, False))) x = rand_ranged_matrix(-1000, 1000, [100, 100]) self.assertTrue(np.array_equal(myType.filter([x]), [x]))
def test_interface(self): mySymbolicMatricesList = TypedListType(T.TensorType(theano.config.floatX, (False, False)))() z = mySymbolicMatricesList.__len__() f = theano.function([mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(f([x, x]) == 2)
def test_get_depth(self): # test case for get_depth utilitary function myType = TypedListType(T.TensorType(theano.config.floatX, (False, False))) myManualNestedType = TypedListType(TypedListType( TypedListType(myType))) self.assertTrue(myManualNestedType.get_depth() == 3)
def test_basic_nested_list(self): # Testing nested list with one level of depth myNestedType = TypedListType(T.TensorType(theano.config.floatX, (False, False))) myType = TypedListType(myNestedType) x = rand_ranged_matrix(-1000, 1000, [100, 100]) self.assertTrue(np.array_equal(myType.filter([[x]]), [[x]]))
def test_intern_filter(self): # Test checking if values contained are themselves # filtered. If they weren't this code would raise # an exception. myType = TypedListType(T.TensorType('float64', (False, False))) x = np.asarray([[4, 5], [4, 5]], dtype='float32') self.assertTrue(np.array_equal(myType.filter([x]), [x]))
def test_interface(self): mySymbolicMatricesList = TypedListType(T.TensorType(theano.config.floatX, (False, False)))() z = mySymbolicMatricesList.reverse() f = theano.function([mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x, y]), [y, x]))
def test_interface(self): mySymbolicMatricesList = TypedListType(T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() z = mySymbolicMatricesList.count(myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(f([x, y], y) == 1)
def test_interfaces(self): mySymbolicMatricesList = TypedListType(T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() z = mySymbolicMatricesList.append(myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x], y), [x, y]))
def test_interface(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)) )() z = mySymbolicMatricesList.reverse() f = theano.function([mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert np.array_equal(f([x, y]), [y, x])
def test_interface(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() z = mySymbolicMatricesList.count(myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert f([x, y], y) == 1
def test_interface(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() z = mySymbolicMatricesList.remove(myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x, y], y), [x]))
def test_nested_list_arg(self): """ test for the 'depth' optionnal argument """ myNestedType = TypedListType( T.TensorType(theano.config.floatX, (False, False)), 3) myType = TypedListType( T.TensorType(theano.config.floatX, (False, False))) myManualNestedType = TypedListType(TypedListType( TypedListType(myType))) self.assertTrue(myNestedType == myManualNestedType)
def test_interface(self): mySymbolicMatricesList = TypedListType(T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() myScalar = T.scalar(dtype="int64") z = mySymbolicMatricesList.insert(myScalar, myMatrix) f = theano.function([mySymbolicMatricesList, myScalar, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x], numpy.asarray(1, dtype="int64"), y), [x, y]))
def test_interfaces(self): mySymbolicMatricesList = TypedListType( tt.TensorType(theano.config.floatX, (False, False)) )() myMatrix = tt.matrix() z = mySymbolicMatricesList.append(myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert np.array_equal(f([x], y), [x, y])
def test_interface(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() myScalar = T.scalar(dtype="int64") z = mySymbolicMatricesList.insert(myScalar, myMatrix) f = theano.function([mySymbolicMatricesList, myScalar, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert np.array_equal(f([x], np.asarray(1, dtype="int64"), y), [x, y])
def test_wrong_input(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() mySymbolicMatrix = T.matrix() with pytest.raises(TypeError): GetItem()(mySymbolicMatricesList, mySymbolicMatrix)
def test_sanity_check(self): mySymbolicMatricesList1 = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() mySymbolicMatricesList2 = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() z = Extend()(mySymbolicMatricesList1, mySymbolicMatricesList2) f = theano.function([mySymbolicMatricesList1, mySymbolicMatricesList2], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert np.array_equal(f([x], [y]), [x, y])
def test_interface(self): mySymbolicMatricesList1 = TypedListType(T.TensorType( theano.config.floatX, (False, False)))() mySymbolicMatricesList2 = TypedListType(T.TensorType( theano.config.floatX, (False, False)))() z = mySymbolicMatricesList1.extend(mySymbolicMatricesList2) f = theano.function([mySymbolicMatricesList1, mySymbolicMatricesList2], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(np.array_equal(f([x], [y]), [x, y]))
def test_type_equality(self): # Typed list types should only be equal # when they contains the same theano # variables # list of matrices myType1 = TypedListType( T.TensorType(theano.config.floatX, (False, False))) # list of matrices myType2 = TypedListType( T.TensorType(theano.config.floatX, (False, False))) # list of scalars myType3 = TypedListType(T.TensorType(theano.config.floatX, ())) self.assertTrue(myType2 == myType1) self.assertFalse(myType3 == myType1)
def test_interface(self): mySymbolicMatricesList1 = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() mySymbolicMatricesList2 = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() z = mySymbolicMatricesList1.extend(mySymbolicMatricesList2) f = theano.function([mySymbolicMatricesList1, mySymbolicMatricesList2], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x], [y]), [x, y]))
def test_wrong_input_on_creation(self): # Typed list type should raises an # error if the argument passed for # type is not a valid theano type with pytest.raises(TypeError): TypedListType(None)
def test_variable_is_Typed_List_variable(self): mySymbolicVariable = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() self.assertTrue( isinstance(mySymbolicVariable, theano.typed_list.TypedListVariable))
def test_non_tensor_type(self): mySymbolicNestedMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)), 1)() mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() z = Count()(mySymbolicNestedMatricesList, mySymbolicMatricesList) f = theano.function( [mySymbolicNestedMatricesList, mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(f([[x, y], [x, y, y]], [x, y]) == 1)
def test_wrong_input(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() mySymbolicMatrix = T.matrix() self.assertRaises(TypeError, GetItem(), mySymbolicMatricesList, mySymbolicMatrix)
def test_insert_inplace(self): mySymbolicMatricesList = TypedListType( tt.TensorType(theano.config.floatX, (False, False)))() mySymbolicIndex = tt.scalar(dtype="int64") mySymbolicMatrix = tt.matrix() z = Insert()(mySymbolicMatricesList, mySymbolicIndex, mySymbolicMatrix) m = theano.compile.mode.get_default_mode().including( "typed_list_inplace_opt") f = theano.function( [ In(mySymbolicMatricesList, borrow=True, mutable=True), mySymbolicIndex, mySymbolicMatrix, ], z, accept_inplace=True, mode=m, ) assert f.maker.fgraph.toposort()[0].op.inplace x = rand_ranged(-1000, 1000, [100, 101]) y = rand_ranged(-1000, 1000, [100, 101]) assert np.array_equal(f([x], np.asarray(1, dtype="int64"), y), [x, y])
def test_not_a_list_on_filter(self): # Typed List Value should raises an error # if no iterable variable is given on input # list of matrices myType = TypedListType( T.TensorType(theano.config.floatX, (False, False))) self.assertRaises(TypeError, myType.filter, 4)
def test_inplace(self): mySymbolicMatricesList1 = TypedListType( tt.TensorType(theano.config.floatX, (False, False)) )() mySymbolicMatricesList2 = TypedListType( tt.TensorType(theano.config.floatX, (False, False)) )() z = Extend(True)(mySymbolicMatricesList1, mySymbolicMatricesList2) f = theano.function( [mySymbolicMatricesList1, mySymbolicMatricesList2], z, accept_inplace=True ) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) assert np.array_equal(f([x], [y]), [x, y])
def test_extend_inplace(self): mySymbolicMatricesList1 = TypedListType(T.TensorType( theano.config.floatX, (False, False)))() mySymbolicMatricesList2 = TypedListType(T.TensorType( theano.config.floatX, (False, False)))() z = Extend()(mySymbolicMatricesList1, mySymbolicMatricesList2) m = theano.compile.mode.get_default_mode().including("typed_list_inplace_opt") f = theano.function([In(mySymbolicMatricesList1, borrow=True, mutable=True), mySymbolicMatricesList2], z, mode=m) self.assertTrue(f.maker.fgraph.toposort()[0].op.inplace) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x], [y]), [x, y]))
def test_wrong_input_on_filter(self): # Typed list type should raises an # error if the argument given to filter # isn't of the same type as the one # specified on creation # list of matrices myType = TypedListType( T.TensorType(theano.config.floatX, (False, False))) self.assertRaises(TypeError, myType.filter, [4])
def test_sanity_check(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() z = Length()(mySymbolicMatricesList) f = theano.function([mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(f([x, x, x, x]) == 4)
def test_sparse(self): mySymbolicSparseList = TypedListType( sparse.SparseType('csr', theano.config.floatX))() mySymbolicSparse = sparse.csr_matrix() z = Count()(mySymbolicSparseList, mySymbolicSparse) f = theano.function([mySymbolicSparseList, mySymbolicSparse], z) x = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3)) y = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3)) self.assertTrue(f([x, y, y], y) == 2)
def test_inplace(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() z = Reverse(True)(mySymbolicMatricesList) f = theano.function([mySymbolicMatricesList], z, accept_inplace=True) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(np.array_equal(f([x, y]), [y, x]))
def test_sanity_check(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() z = Reverse()(mySymbolicMatricesList) f = theano.function([mySymbolicMatricesList], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x, y]), [y, x]))
def make_node(self, a): assert isinstance(a, (tuple, list)) a2 = [] for elem in a: if not isinstance(elem, Variable): elem = tt.as_tensor_variable(elem) a2.append(elem) if not all(a2[0].type == elem.type for elem in a2): raise TypeError( "MakeList need all input variable to be of the same type.") tl = TypedListType(a2[0].type)() return Apply(self, a2, [tl])
def test_get_depth(self): # test case for get_depth utilitary function myType = TypedListType(T.TensorType(theano.config.floatX, (False, False))) myManualNestedType = TypedListType(TypedListType(TypedListType(myType))) assert myManualNestedType.get_depth() == 3
def test_sanity_check(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() z = Count()(mySymbolicMatricesList, myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(f([y, y, x, y], y) == 3)
def test_sanity_check(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() myMatrix = T.matrix() z = Append()(mySymbolicMatricesList, myMatrix) f = theano.function([mySymbolicMatricesList, myMatrix], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) y = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(np.array_equal(f([x], y), [x, y]))
def test_sparse(self): sp = pytest.importorskip("scipy") mySymbolicSparseList = TypedListType( sparse.SparseType("csr", theano.config.floatX))() mySymbolicSparse = sparse.csr_matrix() z = Count()(mySymbolicSparseList, mySymbolicSparse) f = theano.function([mySymbolicSparseList, mySymbolicSparse], z) x = sp.sparse.csr_matrix(random_lil((10, 40), theano.config.floatX, 3)) y = sp.sparse.csr_matrix(random_lil((10, 40), theano.config.floatX, 3)) assert f([x, y, y], y) == 2
def test_sanity_check_single(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() mySymbolicScalar = T.scalar() z = GetItem()(mySymbolicMatricesList, mySymbolicScalar) f = theano.function([mySymbolicMatricesList, mySymbolicScalar], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x], numpy.asarray(0)), x))
def test_sparse(self): if not scipy_imported: raise SkipTest('Optional package SciPy not installed') mySymbolicSparseList = TypedListType( sparse.SparseType('csr', theano.config.floatX))() mySymbolicSparse = sparse.csr_matrix() z = Count()(mySymbolicSparseList, mySymbolicSparse) f = theano.function([mySymbolicSparseList, mySymbolicSparse], z) x = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3)) y = sp.csr_matrix(random_lil((10, 40), theano.config.floatX, 3)) self.assertTrue(f([x, y, y], y) == 2)