Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 676
shadow Avatar asked May 13 '17 14:05

shadow


People also ask

How do you modify a tensor value?

we can modify a tensor by using the assignment operator. Assigning a new value in the tensor will modify the tensor with the new value.

Can we change the content of a tensor?

DType . If you're familiar with NumPy, tensors are (kind of) like np. arrays . All tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one.


1 Answers

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))
like image 82
gdelab Avatar answered Sep 30 '22 18:09

gdelab