def test_setfloatx_correct_values(): # Keep track of the old value old_floatx = floatx() # Check correct values for value in ['float16', 'float32', 'float64']: set_floatx(value) assert floatx() == value # Restore old value set_floatx(old_floatx)
def test_setfloatx_incorrect_values(): # Keep track of the old value old_floatx = floatx() # Try some incorrect values initial = floatx() for value in ['', 'beerfloat', 123]: with pytest.raises(Exception): set_floatx(value) assert floatx() == initial # Restore old value set_floatx(old_floatx)
def _random_prep(dtype=None, rng=None): """Helper function for random functions """ if dtype is None: dtype = floatx() if rng is None: rng = make_rng() return dtype, rng
def mean(x, axis=None, keepdims=False): """Mean of a tensor, alongside the specified axis. """ dtype = None # bool is available since theano v0.9dev if 'int' in x.dtype or x.dtype == 'bool': dtype = floatx() return T.mean(x, axis=axis, keepdims=keepdims, dtype=dtype)
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None): """Instantiate an input data placeholder variable. """ if dtype is None: dtype = floatx() if shape is None and ndim is None: raise ValueError('Specify either a shape or ndim value.') if shape is not None: ndim = len(shape) broadcast = (False,) * ndim x = T.TensorType(dtype, broadcast)(name) return x
def test_set_floatx(): """ Make sure that changes to the global floatx are effectively taken into account by the backend. """ # Keep track of the old value old_floatx = floatx() set_floatx('float16') var = BTH.variable([10]) check_dtype(var, 'float16') set_floatx('float64') var = BTH.variable([10]) check_dtype(var, 'float64') # Restore old value set_floatx(old_floatx)
def variable(value, dtype=None, name=None): """Instantiates a variable and returns it. # Arguments value: Numpy array, initial value of the tensor. dtype: Tensor type. name: Optional name string for the tensor. # Returns A variable instance . """ if dtype is None: dtype = floatx() if hasattr(value, 'eval'): value = value.eval() return np.asarray(value, dtype=dtype)
def variable(value, dtype=None, name=None): """Instantiates a variable and returns it. # Arguments value: Numpy array, initial value of the tensor. dtype: Tensor type. name: Optional name string for the tensor. # Returns A variable instance. """ if dtype is None: dtype = floatx() if isinstance(value, (theano.tensor.TensorVariable, theano.tensor.sharedvar.TensorSharedVariable, theano.tensor.TensorConstant)): value = value.eval() value = np.asarray(value, dtype=dtype) variable = theano.shared(value=value, name=name, strict=False) return variable
def eye(size, dtype=None, name=None): """Instantiates an identity matrix. """ if dtype is None: dtype = floatx() return variable(np.eye(size), dtype, name)
def ones(shape, dtype=None, name=None): """Instantiates an all-ones variable. """ if dtype is None: dtype = floatx() return variable(np.ones(shape), dtype, name)