Created
August 24, 2020 22:02
-
-
Save ZaxR/4543d221b11790e1b86bc50837eb341d to your computer and use it in GitHub Desktop.
Tensor Randomization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Data | |
tensor = np.array([ | |
[ 9.8348e+18, 4.5845e-41, -3.5873e-11], | |
[ 3.0950e-41, 1.2141e-40, 3.8102e-38], | |
[ 5.3741e-30, 4.8419e+30, 7.7765e+31], | |
[ 4.7987e+30, 4.9796e-37, 2.1325e-41], | |
[ 2.4230e+21, 1.6045e-37, 1.9106e-28] | |
]) | |
# Setup | |
rate = 0.2 | |
# Replace a random 20% of each array in the tensor with zeroes | |
keep_prob = 1 - rate | |
ones = np.ones(tensor.shape, dtype=int) | |
mask = np.random.binomial(n=ones, p=keep_prob, size=ones.shape) | |
tensor * mask | |
# Mask a random 20% of each array in the tensor | |
ones = np.ones(tensor.shape, dtype=int) | |
mask = np.random.binomial(n=ones, p=rate, size=ones.shape) | |
np.ma.masked_array(tensor, mask) | |
# # Links re: how DL libraries do this for dropout layers | |
# https://stackoverflow.com/questions/54109617/implementing-dropout-from-scratch | |
# # Tensorflow internals | |
# https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/ops/nn_ops.py#L5024 | |
# https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/framework/ops.py#L1480 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment