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?
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.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With