From 3e7fb39cada1fce5a96a7743ebc4f5022959cc22 Mon Sep 17 00:00:00 2001 From: jh Date: Wed, 17 Mar 2021 08:26:00 -0700 Subject: [PATCH] Vectorize --- models/Model_SAEHD/Model.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/models/Model_SAEHD/Model.py b/models/Model_SAEHD/Model.py index 7cd65a6..95445ac 100644 --- a/models/Model_SAEHD/Model.py +++ b/models/Model_SAEHD/Model.py @@ -502,19 +502,15 @@ Examples: df, liae, df-d, df-ud, liae-ud, ... def get_smooth_noisy_labels(label, tensor, smoothing=0.2, noise=0.05): - labels = [] num_labels = self.batch_size for d in tensor.shape[1:]: num_labels *= d - for _ in range(num_labels): - if np.random.random() < noise: - label = 0 if label == 1 else 1 - if label == 0: - label = np.random.uniform(0, 0+smoothing/2) - else: - label = np.random.uniform(1-smoothing/2, 1.0) - labels.append(label) - return tf.reshape(labels, (self.batch_size,) + tensor.shape[1:]) + probs = tf.math.log([[noise, 1-noise]]) if label == 1 else tf.math.log([[1-noise, noise]]) + x = tf.random.categorical(probs, num_labels) + x = tf.cast(x, tf.float32) + x = x * (1-smoothing) + (smoothing/x.shape[1]) + x = tf.reshape(x, (self.batch_size,) + tensor.shape[1:]) + return x gpu_pred_src_src_d_ones = get_smooth_noisy_labels(1, gpu_pred_src_src_d, smoothing=0.2, noise=0.05) gpu_pred_src_src_d_zeros = get_smooth_noisy_labels(0, gpu_pred_src_src_d, smoothing=0.2, noise=0.05)