shadow
shadow

Reputation: 161

How to set some values of tensor as zero value in tensorflow?

I want to sparse the convolution kernels,so I need to set some values in the kernels as zero value in the training process. Are there some apis in the tensorflow to help me realize my idea, to set some values in the tensor as zero?

Upvotes: 9

Views: 14319

Answers (2)

gdelab
gdelab

Reputation: 6220

You can use tf.boolean_mask(original_tensor, mask) to keep only the values that you want (you'll remove the other ones instead of setting them to 0).

To keep the initial shape and just have zeros in some places, you can just do something like that:

new_tensor = tf.multiply(original_tensor, tf.cast(mask, original_tensor.type()))

For your example, you could build the mask with sthg like:

mask = tf.less(original_tensor, 0.0001 * tf.ones_like(original_tensor))

Upvotes: 17

Manuel
Manuel

Reputation: 290

tf.relu_layer() is what you're looking for, which is itself calling tf.nn.relu() with

tensor * weight + bias

So you could just call

tf.nn.relu_layer(tensor, 1.0, -your_threshold)

https://www.tensorflow.org/api_docs/python/tf/nn/relu_layer

Upvotes: 4

Related Questions