本文整理匯總了Python中sugartensor.sg_floatx方法的典型用法代碼示例。如果您正苦於以下問題:Python sugartensor.sg_floatx方法的具體用法?Python sugartensor.sg_floatx怎麽用?Python sugartensor.sg_floatx使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sugartensor
的用法示例。
在下文中一共展示了sugartensor.sg_floatx方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sg_summary_activation
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def sg_summary_activation(tensor, prefix=None, name=None):
r"""Register `tensor` to summary report as `activation`
Args:
tensor: A `Tensor` to log as activation
prefix: A `string`. A prefix to display in the tensor board web UI.
name: A `string`. A name to display in the tensor board web UI.
Returns:
None
"""
# defaults
prefix = '' if prefix is None else prefix + '/'
# summary name
name = prefix + _pretty_name(tensor) if name is None else prefix + name
# summary statistics
_scalar(name + '/ratio',
tf.reduce_mean(tf.cast(tf.greater(tensor, 0), tf.sg_floatx)))
_histogram(name + '/ratio-h', tensor)
示例2: glorot_uniform
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def glorot_uniform(name, shape, scale=1, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True):
r"""See [Glorot & Bengio. 2010.](http://jmlr.org/proceedings/papers/v9/glorot10a/glorot10a.pdf)
Args:
name: The name of new variable
shape: A tuple/list of integers.
scale: A Python scalar. Scale to initialize. Default is 1.
dtype: The data type. Default is float32.
summary: If True, add this constant to tensor board summary.
regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable
will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization
trainable: If True, add this constant to trainable collection. Default is True.
Returns:
A `Variable`.
"""
fin, fout = _get_fans(shape)
s = np.sqrt(6. * scale / (fin + fout))
return uniform(name, shape, s, dtype, summary, regularizer, trainable)
示例3: sg_emb
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def sg_emb(**kwargs):
r"""Returns a look-up table for embedding.
kwargs:
name: A name for the layer.
emb: A 2-D array (optional).
If None, the resulting tensor should have the shape of
`[vocabulary size, embedding dimension size]`.
Note that its first row is filled with 0's associated with padding.
in_dim: A positive `integer`. The size of input dimension.
dim: A positive `integer`. The size of output dimension.
voca_size: A positive integer. The size of vocabulary.
summary: If True, summaries are added. The default is True.
Returns:
A 2-D `Tensor` of float32.
"""
opt = tf.sg_opt(kwargs)
assert opt.name is not None, 'name is mandatory.'
if opt.emb is None:
# initialize embedding matrix
assert opt.voca_size is not None, 'voca_size is mandatory.'
assert opt.dim is not None, 'dim is mandatory.'
w = tf.sg_initializer.he_uniform(opt.name, (opt.voca_size - 1, opt.dim), summary=opt.summary)
else:
# use given embedding matrix
w = tf.sg_initializer.external(opt.name, value=opt.emb, summary=opt.summary)
# 1st row should be zero and not be updated by backprop because of zero padding.
emb = tf.concat([tf.zeros((1, opt.dim), dtype=tf.sg_floatx), w], 0)
return emb
# layer normalization for rnn
示例4: sg_hinge
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def sg_hinge(tensor, opt):
r"""Returns hinge loss between `tensor` and `target`.
Args:
tensor: A `Tensor`.
opt:
target: A `Tensor`. Labels.
margin: An int. Maximum margin. Default is 1.
name: A `string`. A name to display in the tensor board web UI.
Returns:
A `Tensor`.
For example,
```
tensor = [[30, 10, 40], [13, 30, 42]]
target = [[0, 0, 1], [0, 1, 0]]
tensor.sg_hinge(target=target, one_hot=True) => [[ 1. 1. 0.]
[ 1. 0. 1.]]
```
"""
assert opt.target is not None, 'target is mandatory.'
# default margin
opt += tf.sg_opt(margin=1)
# reshape target
shape = tensor.get_shape().as_list()
broadcast_shape = [-1] + [1] * (len(shape) - 2) + [shape[-1]]
target = tf.cast(tf.reshape(opt.target, broadcast_shape), tf.sg_floatx)
# hinge loss
out = tf.identity(tf.maximum(opt.margin - target * tensor, 0), 'hinge')
# add summary
tf.sg_summary_loss(out, name=opt.name)
return out
示例5: constant
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def constant(name, shape, value=0, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True):
r"""Creates a tensor variable of which initial values are `value` and shape is `shape`.
Args:
name: The name of new variable.
shape: A tuple/list of integers or an integer.
If shape is an integer, it is converted to a list.
value: A Python scalar. All elements of the initialized variable
will be set to this value. Default is 0.
dtype: The data type. Only floating point types are supported. Default is float32.
summary: If True, add this constant to tensor board summary.
regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable
will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization
trainable: If True, add this constant to trainable collection. Default is True.
Returns:
A `Variable`.
"""
shape = shape if isinstance(shape, (tuple, list)) else [shape]
x = tf.get_variable(name, shape, dtype=dtype,
initializer=tf.constant_initializer(value),
regularizer=regularizer, trainable=trainable)
# add summary
if summary:
tf.sg_summary_param(x)
return x
示例6: uniform
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def uniform(name, shape, scale=0.05, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True):
r"""Creates a tensor variable of which initial values are
random numbers based on uniform distribution.
Note that the default value of `scale` (=0.05) is different from
the min/max values (=0.0, 1.0) of tf.random_uniform_initializer.
Args:
name: The name of the new variable.
shape: A tuple/list of integers or an integer.
If shape is an integer, it's converted to a list.
scale: A Python scalar. All initial values should be in range `[-scale, scale)`. Default is .05.
dtype: The data type. Only floating point types are supported. Default is float32.
summary: If True, add this constant to tensor board summary.
regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable
will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization
trainable: If True, add this constant to trainable collection. Default is True.
Returns:
A `Variable`.
"""
shape = shape if isinstance(shape, (tuple, list)) else [shape]
x = tf.get_variable(name, shape, dtype=dtype,
initializer=tf.random_uniform_initializer(minval=-scale, maxval=scale),
regularizer=regularizer, trainable=trainable)
# add summary
if summary:
tf.sg_summary_param(x)
return x
示例7: identity
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def identity(name, dim, scale=1, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True):
r"""Creates a tensor variable of which initial values are of
an identity matrix.
Note that the default value of `scale` (=0.05) is different from
the min/max values (=0.0, 1.0) of tf.random_uniform_initializer.
For example,
```
identity("identity", 3, 2) =>
[[2. 0. 0.]
[0. 2. 0.]
[0. 0. 2.]]
```
Args:
name: The name of new variable.
dim: An int. The size of the first and second dimension of the output tensor.
scale: A Python scalar. The value on the diagonal.
dtype: The type of the elements of the resulting tensor.
summary: If True, add this constant to tensor board summary.
regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable
will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization
trainable: If True, add this constant to trainable collection. Default is True.
Returns:
A 2-D `Variable`.
"""
x = tf.get_variable(name,
initializer=tf.constant(np.eye(dim) * scale, dtype=dtype),
regularizer=regularizer, trainable=trainable)
# add summary
if summary:
tf.sg_summary_param(x)
return x
示例8: orthogonal
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def orthogonal(name, shape, scale=1.1, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True):
r"""Creates a tensor variable of which initial values are of
an orthogonal ndarray.
See [Saxe et al. 2014.](http://arxiv.org/pdf/1312.6120.pdf)
Args:
name: The name of new variable.
shape: A tuple/list of integers.
scale: A Python scalar.
dtype: Either float32 or float64.
summary: If True, add this constant to tensor board summary.
regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable
will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization
trainable: If True, add this constant to trainable collection. Default is True.
Returns:
A `Variable`.
"""
flat_shape = (shape[0], np.prod(shape[1:]))
a = np.random.normal(0.0, 1.0, flat_shape)
u, _, v = np.linalg.svd(a, full_matrices=False)
# pick the one with the correct shape
q = u if u.shape == flat_shape else v
q = q.reshape(shape)
# create variable
x = tf.get_variable(name,
initializer=tf.constant(scale * q[:shape[0], :shape[1]], dtype=dtype),
regularizer=regularizer, trainable=trainable)
# add summary
if summary:
tf.sg_summary_param(x)
return x
示例9: external
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def external(name, value, dtype=tf.sg_floatx, summary=True, regularizer=None, trainable=True):
r"""Creates a tensor variable of which initial values are `value`.
For example,
```
external("external", [3,3,1,2])
=> [3. 3. 1. 2.]
```
Args:
name: The name of new variable.
value: A constant value (or list) of output type `dtype`.
dtype: The type of the elements of the resulting tensor.
summary: If True, add this constant to tensor board summary.
regularizer: A (Tensor -> Tensor or None) function; the result of applying it on a newly created variable
will be added to the collection tf.GraphKeys.REGULARIZATION_LOSSES and can be used for regularization
trainable: If True, add this constant to trainable collection. Default is True.
Returns:
A `Variable`. Has the same contents as `value` of `dtype`.
"""
# create variable
x = tf.get_variable(name,
initializer=tf.constant(value, dtype=dtype),
regularizer=regularizer, trainable=trainable)
# add summary
if summary:
tf.sg_summary_param(x)
return x
示例10: sg_float
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def sg_float(tensor, opt):
r"""Casts a tensor to floatx.
See `tf.cast()` in tensorflow.
Args:
tensor: A `Tensor` or `SparseTensor` (automatically given by chain).
opt:
name : If provided, it replaces current tensor's name
Returns:
A `Tensor` or `SparseTensor` with same shape as `tensor`.
"""
return tf.cast(tensor, tf.sg_floatx, name=opt.name)
示例11: __init__
# 需要導入模塊: import sugartensor [as 別名]
# 或者: from sugartensor import sg_floatx [as 別名]
def __init__(self, batch_size=16, set_name='train'):
# load meta file
label, mfcc_file = [], []
with open(_data_path + 'preprocess/meta/%s.csv' % set_name) as csv_file:
reader = csv.reader(csv_file, delimiter=',')
for row in reader:
# mfcc file
mfcc_file.append(_data_path + 'preprocess/mfcc/' + row[0] + '.npy')
# label info ( convert to string object for variable-length support )
label.append(np.asarray(row[1:], dtype=np.int).tostring())
# to constant tensor
label_t = tf.convert_to_tensor(label)
mfcc_file_t = tf.convert_to_tensor(mfcc_file)
# create queue from constant tensor
label_q, mfcc_file_q \
= tf.train.slice_input_producer([label_t, mfcc_file_t], shuffle=True)
# create label, mfcc queue
label_q, mfcc_q = _load_mfcc(source=[label_q, mfcc_file_q],
dtypes=[tf.sg_intx, tf.sg_floatx],
capacity=256, num_threads=64)
# create batch queue with dynamic pad
batch_queue = tf.train.batch([label_q, mfcc_q], batch_size,
shapes=[(None,), (20, None)],
num_threads=64, capacity=batch_size*32,
dynamic_pad=True)
# split data
self.label, self.mfcc = batch_queue
# batch * time * dim
self.mfcc = self.mfcc.sg_transpose(perm=[0, 2, 1])
# calc total batch count
self.num_batch = len(label) // batch_size
# print info
tf.sg_info('%s set loaded.(total data=%d, total batch=%d)'
% (set_name.upper(), len(label), self.num_batch))