假设我有一个张量,其形状为[35,4,5,3],它是batch_size,4列,来自不同时间的5个样本,3个通道。
我想做的是从批次中的每一列,每个通道和每个样本中,从不同的时间随机抽取5个样本中的3个。对于所选的3,将其放大5/3。丢弃的2设置为0。 这是我当前正在使用的代码:
def hard_dropout(x, num_drop, axis=-1, training=False):
if not training:
return x
if axis < 0:
axis = len(x.shape) + axis
assert axis >= 0
shape = [int(i) for i in x.shape]
# length of dropout mask
length = shape[axis]
del shape[axis]
# number of dropout mask (for each example, column and channel)
num_repeat = np.prod(shape)
masks = []
mask = np.concatenate([np.ones(length - num_drop, dtype=np.float32) * length / (length - num_drop),
np.zeros(num_drop, dtype=np.float32)])
with tf.name_scope('hard_dropout'):
for i in range(num_repeat):
masks.append(tf.random_shuffle(mask))
masks = tf.stack(masks, axis=0)
# reshape and transpose the masks to the shape of input tensor
masks = tf.reshape(masks, [*shape, length])
permutation = list(range(0,len(shape)))
permutation.insert(axis, len(shape))
masks = tf.transpose(masks, permutation)
x = masks * x
return x
当前代码似乎运行良好,但是效率不是很高,并且减慢了培训的速度,所以我想知道是否有更好的方法可以做到这一点?