def test_bincountOp(self): w = T.vector('w') for dtype in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): # uint64 always fails # int64 and uint32 also fail if python int are 32-bit int_bitwidth = theano.gof.python_int_bitwidth() if int_bitwidth == 64: numpy_unsupported_dtypes = ('uint64',) if int_bitwidth == 32: numpy_unsupported_dtypes = ('uint32', 'int64', 'uint64') x = T.vector('x', dtype=dtype) if dtype in numpy_unsupported_dtypes: self.assertRaises(TypeError, bincount, x) else: a = np.random.random_integers(50, size=(25)).astype(dtype) weights = np.random.random((25,)).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (np.bincount(a) == f1(a)).all() assert np.allclose(np.bincount(a, weights=weights), f2(a, weights)) if not numpy_16: continue f3 = theano.function([x], bincount(x, minlength=23)) f4 = theano.function([x], bincount(x, minlength=5)) assert (np.bincount(a, minlength=23) == f3(a)).all() assert (np.bincount(a, minlength=5) == f4(a)).all()
def test_bincountOp(self): w = T.vector('w') for dtype in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): # uint64 always fails # int64 and uint32 also fail if python int are 32-bit int_bitwidth = theano.gof.python_int_bitwidth() if int_bitwidth == 64: numpy_unsupported_dtypes = ('uint64', ) if int_bitwidth == 32: numpy_unsupported_dtypes = ('uint32', 'int64', 'uint64') x = T.vector('x', dtype=dtype) if dtype in numpy_unsupported_dtypes: self.assertRaises(TypeError, bincount, x) else: a = np.random.random_integers(50, size=(25)).astype(dtype) weights = np.random.random((25, )).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (np.bincount(a) == f1(a)).all() assert np.allclose(np.bincount(a, weights=weights), f2(a, weights)) if not numpy_16: continue f3 = theano.function([x], bincount(x, minlength=23)) f4 = theano.function([x], bincount(x, minlength=5)) assert (np.bincount(a, minlength=23) == f3(a)).all() assert (np.bincount(a, minlength=5) == f4(a)).all()
def test_bincountFn(self): w = T.vector("w") for dtype in ("int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"): x = T.vector("x", dtype=dtype) # uint64 always fails # int64 and uint32 also fail if python int are 32-bit int_bitwidth = theano.gof.python_int_bitwidth() if int_bitwidth == 64: numpy_unsupported_dtypes = ("uint64",) if int_bitwidth == 32: numpy_unsupported_dtypes = ("uint32", "int64", "uint64") # uint64 always fails if dtype in numpy_unsupported_dtypes: self.assertRaises(TypeError, bincount, x) else: a = np.random.random_integers(50, size=(25)).astype(dtype) weights = np.random.random((25,)).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (np.bincount(a) == f1(a)).all() assert np.allclose(np.bincount(a, weights=weights), f2(a, weights)) f3 = theano.function([x], bincount(x, minlength=23)) f4 = theano.function([x], bincount(x, minlength=5)) assert (np.bincount(a, minlength=23) == f3(a)).all() assert (np.bincount(a, minlength=5) == f4(a)).all() # skip the following test when using unsigned ints if not dtype.startswith("u"): a[0] = -1 f5 = theano.function([x], bincount(x, assert_nonneg=True)) self.assertRaises(AssertionError, f5, a)
def test_bincountFn(self): w = T.vector('w') for dtype in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): x = T.vector('x', dtype=dtype) # uint64 always fails if dtype in ('uint64',): self.assertRaises(TypeError, bincount, x) else: a = np.random.random_integers(50, size=(25)).astype(dtype) weights = np.random.random((25,)).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (np.bincount(a) == f1(a)).all() assert np.allclose(np.bincount(a, weights=weights), f2(a, weights)) f3 = theano.function([x], bincount(x, minlength=23)) f4 = theano.function([x], bincount(x, minlength=5)) assert (np.bincount(a, minlength=23) == f3(a)).all() assert (np.bincount(a, minlength=5) == f4(a)).all() # skip the following test when using unsigned ints if not dtype.startswith('u'): a[0] = -1 f5 = theano.function([x], bincount(x, assert_nonneg=True)) self.assertRaises(AssertionError, f5, a)
def test_bincountFn(self): w = T.vector("w") def ref(data, w=None, minlength=None): size = int(data.max() + 1) if minlength: size = max(size, minlength) if w is not None: out = np.zeros(size, dtype=w.dtype) for i in range(data.shape[0]): out[data[i]] += w[i] else: out = np.zeros(size, dtype=a.dtype) for i in range(data.shape[0]): out[data[i]] += 1 return out for dtype in ( "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", ): x = T.vector("x", dtype=dtype) a = np.random.randint(1, 51, size=(25)).astype(dtype) weights = np.random.random((25, )).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (ref(a) == f1(a)).all() assert np.allclose(ref(a, weights), f2(a, weights)) f3 = theano.function([x], bincount(x, minlength=55)) f4 = theano.function([x], bincount(x, minlength=5)) assert (ref(a, minlength=55) == f3(a)).all() assert (ref(a, minlength=5) == f4(a)).all() # skip the following test when using unsigned ints if not dtype.startswith("u"): a[0] = -1 f5 = theano.function([x], bincount(x, assert_nonneg=True)) with pytest.raises(AssertionError): f5(a)
def test_infer_shape(self): for dtype in tensor.discrete_dtypes: # uint64 always fails # int64 and uint32 also fail if python int are 32-bit int_bitwidth = theano.gof.python_int_bitwidth() if int_bitwidth == 64: numpy_unsupported_dtypes = ('uint64',) if int_bitwidth == 32: numpy_unsupported_dtypes = ('uint32', 'int64', 'uint64') x = T.vector('x', dtype=dtype) if dtype in numpy_unsupported_dtypes: self.assertRaises(TypeError, bincount, x) else: self._compile_and_check( [x], [bincount(x)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class) weights = np.random.random((25,)).astype(config.floatX) self._compile_and_check( [x], [bincount(x, weights=weights)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class) if not numpy_16: continue self._compile_and_check( [x], [bincount(x, minlength=60)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class) self._compile_and_check( [x], [bincount(x, minlength=5)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class)
def test_infer_shape(self): for dtype in tensor.discrete_dtypes: # uint64 always fails # int64 and uint32 also fail if python int are 32-bit int_bitwidth = theano.gof.python_int_bitwidth() if int_bitwidth == 64: numpy_unsupported_dtypes = ('uint64',) if int_bitwidth == 32: numpy_unsupported_dtypes = ('uint32', 'int64', 'uint64') x = T.vector('x', dtype=dtype) if dtype in numpy_unsupported_dtypes: self.assertRaises(TypeError, bincount, x) else: self._compile_and_check( [x], [bincount(x)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class) weights = np.random.random((25,)).astype(config.floatX) self._compile_and_check( [x], [bincount(x, weights=weights)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class) if not numpy_16: continue self._compile_and_check( [x], [bincount(x, minlength=60)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class) self._compile_and_check( [x], [bincount(x, minlength=5)], [np.random.random_integers( 50, size=(25,)).astype(dtype)], self.op_class)
def test_bincountFn(self): w = T.vector('w') def ref(data, w=None, minlength=None): size = data.max() + 1 if minlength: size = max(size, minlength) if w is not None: out = np.zeros(size, dtype=w.dtype) for i in range(data.shape[0]): out[data[i]] += w[i] else: out = np.zeros(size, dtype=a.dtype) for i in range(data.shape[0]): out[data[i]] += 1 return out for dtype in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): x = T.vector('x', dtype=dtype) a = np.random.random_integers(50, size=(25)).astype(dtype) weights = np.random.random((25, )).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (ref(a) == f1(a)).all() assert np.allclose(ref(a, weights), f2(a, weights)) f3 = theano.function([x], bincount(x, minlength=55)) f4 = theano.function([x], bincount(x, minlength=5)) assert (ref(a, minlength=55) == f3(a)).all() assert (ref(a, minlength=5) == f4(a)).all() # skip the following test when using unsigned ints if not dtype.startswith('u'): a[0] = -1 f5 = theano.function([x], bincount(x, assert_nonneg=True)) self.assertRaises(AssertionError, f5, a)
def test_bincountFn(self): w = T.vector('w') def ref(data, w=None, minlength=None): size = int(data.max() + 1) if minlength: size = max(size, minlength) if w is not None: out = np.zeros(size, dtype=w.dtype) for i in range(data.shape[0]): out[data[i]] += w[i] else: out = np.zeros(size, dtype=a.dtype) for i in range(data.shape[0]): out[data[i]] += 1 return out for dtype in ('int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64'): x = T.vector('x', dtype=dtype) a = np.random.randint(1, 51, size=(25)).astype(dtype) weights = np.random.random((25,)).astype(config.floatX) f1 = theano.function([x], bincount(x)) f2 = theano.function([x, w], bincount(x, weights=w)) assert (ref(a) == f1(a)).all() assert np.allclose(ref(a, weights), f2(a, weights)) f3 = theano.function([x], bincount(x, minlength=55)) f4 = theano.function([x], bincount(x, minlength=5)) assert (ref(a, minlength=55) == f3(a)).all() assert (ref(a, minlength=5) == f4(a)).all() # skip the following test when using unsigned ints if not dtype.startswith('u'): a[0] = -1 f5 = theano.function([x], bincount(x, assert_nonneg=True)) self.assertRaises(AssertionError, f5, a)
def computeC(self, S): M = self.M n_step_sizes = len(self.step_sizes) obs_jump_ind = TT.as_tensor_variable(self.obs_jump_ind, 'obs_jump_ind') tau_ind = obs_jump_ind[1:] * M * M #tau_ind = obs_jump_ind[:-1]*M*M #tau_ind = TT.flatten(obs_jump_ind)[:-1]*M*M keep_jumps = (tau_ind >= 0).nonzero() jump_from_ind = S[:-1] * M jump_to_ind = S[1:] #jump_from_ind = TT.flatten(S)[:-1]*M #jump_to_ind = TT.flatten(S)[1:] #import pdb; pdb.set_trace() flat_ind = (tau_ind + jump_from_ind + jump_to_ind)[keep_jumps] flat_ind_counts = bincount(flat_ind, minlength=n_step_sizes * M * M) C = flat_ind_counts.reshape(shape=np.array([n_step_sizes, M, M])) return C
def computeC(self,S): M = self.M n_step_sizes = len(self.step_sizes) obs_jump_ind = TT.as_tensor_variable(self.obs_jump_ind, 'obs_jump_ind') tau_ind = obs_jump_ind[1:]*M*M #tau_ind = obs_jump_ind[:-1]*M*M #tau_ind = TT.flatten(obs_jump_ind)[:-1]*M*M keep_jumps = (tau_ind >= 0).nonzero() jump_from_ind = S[:-1]*M jump_to_ind = S[1:] #jump_from_ind = TT.flatten(S)[:-1]*M #jump_to_ind = TT.flatten(S)[1:] #import pdb; pdb.set_trace() flat_ind = (tau_ind + jump_from_ind + jump_to_ind)[keep_jumps] flat_ind_counts = bincount(flat_ind, minlength=n_step_sizes*M*M) C = flat_ind_counts.reshape(shape=np.array([n_step_sizes,M,M])) return C