如何在张量流中将张量的某些值设置为零值? [英] How to set some values of tensor as zero value in tensorflow?

查看:71
本文介绍了如何在张量流中将张量的某些值设置为零值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想稀疏卷积核,所以我需要在训练过程中将核中的一些值设置为零值.张量流中是否有一些api可以帮助我实现我的想法,将张量中的一些值设置为零?

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?

推荐答案

您可以使用 tf.boolean_mask(original_tensor, mask) 只保留您想要的值(您将删除其他值而不是将它们设置为 0).

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()))

对于您的示例,您可以使用 sthg 构建掩码,例如:

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

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

这篇关于如何在张量流中将张量的某些值设置为零值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆