本文整理汇总了Python中tensorflow.sparse_retain方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.sparse_retain方法的具体用法?Python tensorflow.sparse_retain怎么用?Python tensorflow.sparse_retain使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.sparse_retain方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sparse_dropout
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def sparse_dropout(self, x, keep_prob, noise_shape):
"""
Dropout for sparse tensors.
Parameters
----------
x: Input data
keep_prob: Keep probability
noise_shape: Size of each entry of x
Returns
-------
pre_out: x after dropout
"""
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(x, dropout_mask)
return pre_out * (1./keep_prob)
示例2: sparse_dropout
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def sparse_dropout(x, keep_prob, noise_shape=None, seed=None, name=None):
'''borrowed logic and implementation from https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/python/ops/nn_ops.py'''
# Skipping all the assertions
if (keep_prob == 1):
return x
# uniform [keep_prob, 1.0 + keep_prob)
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape,
seed=seed,
dtype=x.dtype)
# 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob)
binary_tensor = tf.floor(random_tensor)
# Typecase necessary as mentioned on https://www.tensorflow.org/api_docs/python/tf/sparse_retain
binary_tensor = tf.cast(binary_tensor, dtype=tf.bool)
ret = tf.sparse_retain(x, binary_tensor)
ret = ret * (1 / keep_prob)
return ret
示例3: sparse_dropout
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def sparse_dropout(x, keep_prob, noise_shape):
"""Dropout for sparse tensors."""
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(x, dropout_mask)
return pre_out * (1./keep_prob)
示例4: sparse_dropout
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def sparse_dropout(x, keep_prob, noise_shape):
"""Dropout for sparse tensors."""
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(x, dropout_mask)
return pre_out * (1. / keep_prob)
示例5: _dropout_sparse
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def _dropout_sparse(x, keep_prob, num_nonzero_elements):
random_tensor = keep_prob + tf.random_uniform([num_nonzero_elements])
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
return tf.sparse_retain(x, dropout_mask) / keep_prob
示例6: dropout_supporting_sparse_tensors
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def dropout_supporting_sparse_tensors(X, keep_prob):
"""Add dropout layer on top of X.
Parameters
----------
X : tf.Tensor or tf.SparseTensor
Tensor over which dropout is applied.
keep_prob : float, tf.placeholder
Probability of keeping a value (= 1 - probability of dropout).
Returns
-------
X : tf.Tensor or tf.SparseTensor
Tensor with elementwise dropout applied.
Author: Oleksandr Shchur & Johannes Klicpera
"""
if isinstance(X, tf.SparseTensor):
# nnz = X.values.shape # number of nonzero entries
# random_tensor = keep_prob
# random_tensor += tf.random_uniform(nnz)
# dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
# pre_out = tf.sparse_retain(X, dropout_mask)
# return pre_out * (1.0 / keep_prob)
values_after_dropout = tf.nn.dropout(X.values, keep_prob)
return tf.SparseTensor(X.indices, values_after_dropout, X.dense_shape)
else:
return tf.nn.dropout(X, keep_prob)
示例7: _dropout_sparse
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def _dropout_sparse(self, X, keep_prob, n_nonzero_elems):
"""
Dropout for sparse tensors.
"""
noise_shape = [n_nonzero_elems]
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(X, dropout_mask)
return pre_out * tf.div(1., keep_prob)
示例8: sparse_dropout
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def sparse_dropout(x, keep_prob, noise_shape):
"""Dropout for sparse tensors."""
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(x, dropout_mask)
return tf.SparseTensor(
indices=pre_out.indices,
values=pre_out.values / keep_prob,
dense_shape=pre_out.dense_shape)
示例9: dropout_sparse
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def dropout_sparse(x, keep_prob, num_nonzero_elems):
"""Dropout for sparse tensors. Currently fails for very large sparse tensors (>1M elements)
"""
noise_shape = [num_nonzero_elems]
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(x, dropout_mask)
return pre_out * (1. / keep_prob)
示例10: sparse_dropout
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import sparse_retain [as 别名]
def sparse_dropout(x, keep_prob, noise_shape, is_training):
"""Dropout for sparse tensors."""
if is_training == True:
random_tensor = keep_prob
random_tensor += tf.random_uniform(noise_shape)
dropout_mask = tf.cast(tf.floor(random_tensor), dtype=tf.bool)
pre_out = tf.sparse_retain(x, dropout_mask)
x = pre_out * (1./keep_prob)
return x