def make_node(self, x, index): assert isinstance(x.type, TypedListType) if not isinstance(index, Variable): if isinstance(index, slice): index = Constant(SliceType(), index) return Apply(self, [x, index], [x.type()]) else: index = tt.constant(index, ndim=0, dtype="int64") return Apply(self, [x, index], [x.ttype()]) if isinstance(index.type, SliceType): return Apply(self, [x, index], [x.type()]) elif isinstance(index, tt.TensorVariable) and index.ndim == 0: assert index.dtype == "int64" return Apply(self, [x, index], [x.ttype()]) else: raise TypeError("Expected scalar or slice as index.")
def test_sanity_check_slice(self): mySymbolicMatricesList = TypedListType( T.TensorType(theano.config.floatX, (False, False)))() mySymbolicSlice = SliceType()() z = GetItem()(mySymbolicMatricesList, mySymbolicSlice) self.assertFalse(isinstance(z, T.TensorVariable)) f = theano.function([mySymbolicMatricesList, mySymbolicSlice], z) x = rand_ranged_matrix(-1000, 1000, [100, 101]) self.assertTrue(numpy.array_equal(f([x], slice(0, 1, 1)), [x]))