Python tensorflow.floor() Examples
The following are 30
code examples of tensorflow.floor().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: cyclical_learning_rate.py From addons with Apache License 2.0 | 6 votes |
def __call__(self, step): with tf.name_scope(self.name or "CyclicalLearningRate"): initial_learning_rate = tf.convert_to_tensor( self.initial_learning_rate, name="initial_learning_rate" ) dtype = initial_learning_rate.dtype maximal_learning_rate = tf.cast(self.maximal_learning_rate, dtype) step_size = tf.cast(self.step_size, dtype) cycle = tf.floor(1 + step / (2 * step_size)) x = tf.abs(step / step_size - 2 * cycle + 1) mode_step = cycle if self.scale_mode == "cycle" else step return initial_learning_rate + ( maximal_learning_rate - initial_learning_rate ) * tf.maximum(tf.cast(0, dtype), (1 - x)) * self.scale_fn(mode_step)
Example #2
Source File: CenterNet.py From CenterNet-tensorflow with MIT License | 6 votes |
def _compute_one_image_loss(self, keypoints, offset, size, ground_truth, meshgrid_y, meshgrid_x, stride, pshape): slice_index = tf.argmin(ground_truth, axis=0)[0] ground_truth = tf.gather(ground_truth, tf.range(0, slice_index, dtype=tf.int64)) ngbbox_y = ground_truth[..., 0] / stride ngbbox_x = ground_truth[..., 1] / stride ngbbox_h = ground_truth[..., 2] / stride ngbbox_w = ground_truth[..., 3] / stride class_id = tf.cast(ground_truth[..., 4], dtype=tf.int32) ngbbox_yx = ground_truth[..., 0:2] / stride ngbbox_yx_round = tf.floor(ngbbox_yx) offset_gt = ngbbox_yx - ngbbox_yx_round size_gt = ground_truth[..., 2:4] / stride ngbbox_yx_round_int = tf.cast(ngbbox_yx_round, tf.int64) keypoints_loss = self._keypoints_loss(keypoints, ngbbox_yx_round_int, ngbbox_y, ngbbox_x, ngbbox_h, ngbbox_w, class_id, meshgrid_y, meshgrid_x, pshape) offset = tf.gather_nd(offset, ngbbox_yx_round_int) size = tf.gather_nd(size, ngbbox_yx_round_int) offset_loss = tf.reduce_mean(tf.abs(offset_gt - offset)) size_loss = tf.reduce_mean(tf.abs(size_gt - size)) total_loss = keypoints_loss + 0.1*size_loss + offset_loss return total_loss
Example #3
Source File: diet.py From BERT with Apache License 2.0 | 6 votes |
def _quantize(x, params, randomize=True): """Quantize x according to params, optionally randomizing the rounding.""" if not params.quantize: return x if not randomize: return tf.bitcast( tf.cast(x / params.quantization_scale, tf.int16), tf.float16) abs_x = tf.abs(x) sign_x = tf.sign(x) y = abs_x / params.quantization_scale y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x))) y = tf.minimum(y, tf.int16.max) * sign_x q = tf.bitcast(tf.cast(y, tf.int16), tf.float16) return q
Example #4
Source File: diet.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def _quantize(x, params, randomize=True): """Quantize x according to params, optionally randomizing the rounding.""" if not params.quantize: return x if not randomize: return tf.bitcast( tf.cast(x / params.quantization_scale, tf.int16), tf.float16) abs_x = tf.abs(x) sign_x = tf.sign(x) y = abs_x / params.quantization_scale y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x))) y = tf.minimum(y, tf.int16.max) * sign_x q = tf.bitcast(tf.cast(y, tf.int16), tf.float16) return q
Example #5
Source File: tfops.py From glow with MIT License | 6 votes |
def gaussian_diag(mean, logsd): class o(object): pass o.mean = mean o.logsd = logsd o.eps = tf.random_normal(tf.shape(mean)) o.sample = mean + tf.exp(logsd) * o.eps o.sample2 = lambda eps: mean + tf.exp(logsd) * eps o.logps = lambda x: -0.5 * \ (np.log(2 * np.pi) + 2. * logsd + (x - mean) ** 2 / tf.exp(2. * logsd)) o.logp = lambda x: flatten_sum(o.logps(x)) o.get_eps = lambda x: (x - mean) / tf.exp(logsd) return o # def discretized_logistic_old(mean, logscale, binsize=1 / 256.0, sample=None): # scale = tf.exp(logscale) # sample = (tf.floor(sample / binsize) * binsize - mean) / scale # logp = tf.log(tf.sigmoid(sample + binsize / scale) - tf.sigmoid(sample) + 1e-7) # return tf.reduce_sum(logp, [1, 2, 3])
Example #6
Source File: demo.py From R3Det_Tensorflow with MIT License | 6 votes |
def drop_connect(inputs, is_training, drop_connect_rate): """Apply drop connect.""" if not is_training: return inputs # Compute keep_prob # TODO(tanmingxing): add support for training progress. keep_prob = 1.0 - drop_connect_rate # Compute drop_connect tensor batch_size = tf.shape(inputs)[0] random_tensor = keep_prob random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype) binary_tensor = tf.floor(random_tensor) output = tf.div(inputs, keep_prob) * binary_tensor return output
Example #7
Source File: utils.py From R3Det_Tensorflow with MIT License | 6 votes |
def drop_connect(inputs, is_training, survival_prob): """Drop the entire conv with given survival probability.""" # "Deep Networks with Stochastic Depth", https://arxiv.org/pdf/1603.09382.pdf if not is_training: return inputs # Compute tensor. batch_size = tf.shape(inputs)[0] random_tensor = survival_prob random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype) binary_tensor = tf.floor(random_tensor) # Unlike conventional way that multiply survival_prob at test time, here we # divide survival_prob at training time, such that no addition compute is # needed at test time. output = tf.div(inputs, survival_prob) * binary_tensor return output
Example #8
Source File: zoneout.py From Gun-Detector with Apache License 2.0 | 6 votes |
def __call__(self, inputs, state, scope=None): output, new_state = self._cell(inputs, state, scope) if not isinstance(self._cell.state_size, tuple): new_state = tf.split(value=new_state, num_or_size_splits=2, axis=1) state = tf.split(value=state, num_or_size_splits=2, axis=1) final_new_state = [new_state[0], new_state[1]] if self._is_training: for i, state_element in enumerate(state): random_tensor = 1 - self._zoneout_prob # keep probability random_tensor += tf.random_uniform(tf.shape(state_element)) # 0. if [zoneout_prob, 1.0) and 1. if [1.0, 1.0 + zoneout_prob) binary_tensor = tf.floor(random_tensor) final_new_state[ i] = (new_state[i] - state_element) * binary_tensor + state_element else: for i, state_element in enumerate(state): final_new_state[ i] = state_element * self._zoneout_prob + new_state[i] * ( 1 - self._zoneout_prob) if isinstance(self._cell.state_size, tuple): return output, tf.contrib.rnn.LSTMStateTuple( final_new_state[0], final_new_state[1]) return output, tf.concat([final_new_state[0], final_new_state[1]], 1)
Example #9
Source File: glow.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def preprocess(self, x): """Normalize x. Args: x: 4-D Tensor. Returns: x: Scaled such that x lies in-between -0.5 and 0.5 """ n_bits_x = self.hparams.n_bits_x n_bins = 2**n_bits_x x = tf.cast(x, dtype=tf.float32) if n_bits_x < 8: x = tf.floor(x / 2 ** (8 - n_bits_x)) x = x / n_bins - 0.5 return x
Example #10
Source File: dropout.py From jack with MIT License | 6 votes |
def fixed_dropout(xs, keep_prob, noise_shape, seed=None): """ Apply dropout with same mask over all inputs Args: xs: list of tensors keep_prob: noise_shape: seed: Returns: list of dropped inputs """ with tf.name_scope("dropout", values=xs): noise_shape = noise_shape # uniform [keep_prob, 1.0 + keep_prob) random_tensor = keep_prob random_tensor += tf.random_uniform(noise_shape, seed=seed, dtype=xs[0].dtype) # 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob) binary_tensor = tf.floor(random_tensor) outputs = [] for x in xs: ret = tf.div(x, keep_prob) * binary_tensor ret.set_shape(x.get_shape()) outputs.append(ret) return outputs
Example #11
Source File: diet.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def _quantize(x, params, randomize=True): """Quantize x according to params, optionally randomizing the rounding.""" if not params.quantize: return x if not randomize: return tf.bitcast( tf.cast(x / params.quantization_scale, tf.int16), tf.float16) abs_x = tf.abs(x) sign_x = tf.sign(x) y = abs_x / params.quantization_scale y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x))) y = tf.minimum(y, tf.int16.max) * sign_x q = tf.bitcast(tf.cast(y, tf.int16), tf.float16) return q
Example #12
Source File: layers.py From Pixel2MeshPlusPlus with BSD 3-Clause "New" or "Revised" License | 6 votes |
def bi_linear_sample(self, img_feat, n, x, y): x1 = tf.floor(x) x2 = tf.ceil(x) y1 = tf.floor(y) y2 = tf.ceil(y) Q11 = tf.gather_nd(img_feat, tf.stack([n, tf.cast(x1, tf.int32), tf.cast(y1, tf.int32)], 1)) Q12 = tf.gather_nd(img_feat, tf.stack([n, tf.cast(x1, tf.int32), tf.cast(y2, tf.int32)], 1)) Q21 = tf.gather_nd(img_feat, tf.stack([n, tf.cast(x2, tf.int32), tf.cast(y1, tf.int32)], 1)) Q22 = tf.gather_nd(img_feat, tf.stack([n, tf.cast(x2, tf.int32), tf.cast(y2, tf.int32)], 1)) weights = tf.multiply(tf.subtract(x2, x), tf.subtract(y2, y)) Q11 = tf.multiply(tf.expand_dims(weights, 1), Q11) weights = tf.multiply(tf.subtract(x, x1), tf.subtract(y2, y)) Q21 = tf.multiply(tf.expand_dims(weights, 1), Q21) weights = tf.multiply(tf.subtract(x2, x), tf.subtract(y, y1)) Q12 = tf.multiply(tf.expand_dims(weights, 1), Q12) weights = tf.multiply(tf.subtract(x, x1), tf.subtract(y, y1)) Q22 = tf.multiply(tf.expand_dims(weights, 1), Q22) outputs = tf.add_n([Q11, Q21, Q12, Q22]) return outputs
Example #13
Source File: autoaugment_utils.py From tpu_models with Apache License 2.0 | 6 votes |
def _apply_func_with_prob(func, image, args, prob, bboxes): """Apply `func` to image w/ `args` as input with probability `prob`.""" assert isinstance(args, tuple) assert 'bboxes' == inspect.getargspec(func)[0][1] # If prob is a function argument, then this randomness is being handled # inside the function, so make sure it is always called. if 'prob' in inspect.getargspec(func)[0]: prob = 1.0 # Apply the function with probability `prob`. should_apply_op = tf.cast( tf.floor(tf.random_uniform([], dtype=tf.float32) + prob), tf.bool) augmented_image, augmented_bboxes = tf.cond( should_apply_op, lambda: func(image, bboxes, *args), lambda: (image, bboxes)) return augmented_image, augmented_bboxes
Example #14
Source File: diet.py From fine-lm with MIT License | 6 votes |
def _quantize(x, params, randomize=True): """Quantize x according to params, optionally randomizing the rounding.""" if not params.quantize: return x if not randomize: return tf.bitcast( tf.cast(x / params.quantization_scale, tf.int16), tf.float16) abs_x = tf.abs(x) sign_x = tf.sign(x) y = abs_x / params.quantization_scale y = tf.floor(y + tf.random_uniform(common_layers.shape_list(x))) y = tf.minimum(y, tf.int16.max) * sign_x q = tf.bitcast(tf.cast(y, tf.int16), tf.float16) return q
Example #15
Source File: utils.py From tpu_models with Apache License 2.0 | 6 votes |
def drop_connect(inputs, is_training, drop_connect_rate): """Apply drop connect.""" if not is_training: return inputs # Compute keep_prob # TODO(tanmingxing): add support for training progress. keep_prob = 1.0 - drop_connect_rate # Compute drop_connect tensor batch_size = tf.shape(inputs)[0] random_tensor = keep_prob random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype) binary_tensor = tf.floor(random_tensor) output = tf.div(inputs, keep_prob) * binary_tensor return output
Example #16
Source File: utils.py From ghostnet with Apache License 2.0 | 6 votes |
def drop_path(inputs, keep_prob, is_training=True, scope=None): """Drops out a whole example hiddenstate with the specified probability. """ with tf.name_scope(scope, 'drop_path', [inputs]): net = inputs if is_training: batch_size = tf.shape(net)[0] noise_shape = [batch_size, 1, 1, 1] random_tensor = keep_prob random_tensor += tf.random_uniform(noise_shape, dtype=tf.float32) binary_tensor = tf.floor(random_tensor) net = tf.div(net, keep_prob) * binary_tensor return net # =========================================================================== # # Useful methods # =========================================================================== #
Example #17
Source File: utils.py From tpu_models with Apache License 2.0 | 6 votes |
def drop_connect(inputs, is_training, drop_connect_rate): """Apply drop connect.""" if not is_training: return inputs # Compute keep_prob # TODO(tanmingxing): add support for training progress. keep_prob = 1.0 - drop_connect_rate # Compute drop_connect tensor batch_size = tf.shape(inputs)[0] random_tensor = keep_prob random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype) binary_tensor = tf.floor(random_tensor) output = tf.div(inputs, keep_prob) * binary_tensor return output
Example #18
Source File: model.py From lm-human-preferences with MIT License | 6 votes |
def dropout(x, pdrop, *, do_dropout, stateless=True, seed=None, name): """Like tf.nn.dropout but stateless. """ if stateless: assert seed is not None def _dropout(): with tf.name_scope(name): noise_shape = tf.shape(x) if stateless: r = tf.random.stateless_uniform(noise_shape, seed, dtype=x.dtype) # floor uniform [keep_prob, 1.0 + keep_prob) mask = tf.floor(1 - pdrop + r) return x * (mask * (1 / (1 - pdrop))) else: return tf.nn.dropout(x, rate=pdrop, noise_shape=noise_shape) if pdrop == 0 or not do_dropout: return x else: return _dropout()
Example #19
Source File: learning_rate.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
def learning_rate_factor(name, step_num, hparams): """Compute the designated learning rate factor from hparams.""" if name == "constant": tf.logging.info("Base learning rate: %f", hparams.learning_rate_constant) return hparams.learning_rate_constant elif name == "linear_warmup": return tf.minimum(1.0, step_num / hparams.learning_rate_warmup_steps) elif name == "linear_decay": ret = (hparams.train_steps - step_num) / hparams.learning_rate_decay_steps return tf.minimum(1.0, tf.maximum(0.0, ret)) elif name == "cosdecay": # openai gpt in_warmup = tf.cast(step_num <= hparams.learning_rate_warmup_steps, dtype=tf.float32) ret = 0.5 * (1 + tf.cos( np.pi * step_num / hparams.learning_rate_decay_steps)) # if in warmup stage return 1 else return the decayed value return in_warmup * 1 + (1 - in_warmup) * ret elif name == "rsqrt_decay": return tf.rsqrt(tf.maximum(step_num, hparams.learning_rate_warmup_steps)) elif name == "rsqrt_normalized_decay": scale = tf.sqrt(tf.to_float(hparams.learning_rate_warmup_steps)) return scale * tf.rsqrt(tf.maximum( step_num, hparams.learning_rate_warmup_steps)) elif name == "exp_decay": decay_steps = hparams.learning_rate_decay_steps warmup_steps = hparams.learning_rate_warmup_steps p = (step_num - warmup_steps) / decay_steps p = tf.maximum(p, 0.) if hparams.learning_rate_decay_staircase: p = tf.floor(p) return tf.pow(hparams.learning_rate_decay_rate, p) elif name == "rsqrt_hidden_size": return hparams.hidden_size ** -0.5 elif name == "legacy": return legacy_learning_rate_schedule(hparams) else: raise ValueError("unknown learning rate factor %s" % name)
Example #20
Source File: nasnet_utils.py From yolo_v2 with Apache License 2.0 | 5 votes |
def drop_path(net, keep_prob, is_training=True): """Drops out a whole example hiddenstate with the specified probability.""" if is_training: batch_size = tf.shape(net)[0] noise_shape = [batch_size, 1, 1, 1] random_tensor = keep_prob random_tensor += tf.random_uniform(noise_shape, dtype=tf.float32) binary_tensor = tf.floor(random_tensor) net = tf.div(net, keep_prob) * binary_tensor return net
Example #21
Source File: test_forward.py From training_results_v0.6 with Apache License 2.0 | 5 votes |
def test_forward_floor(): ishape = (1, 3, 10, 10) inp_array = np.random.uniform(size=ishape).astype(np.float32) with tf.Graph().as_default(): in1 = tf.placeholder(shape=inp_array.shape, dtype=inp_array.dtype) tf.floor(in1) compare_tf_with_tvm(inp_array, 'Placeholder:0', 'Floor:0')
Example #22
Source File: neural_gpu.py From yolo_v2 with Apache License 2.0 | 5 votes |
def quantize(t, quant_scale, max_value=1.0): """Quantize a tensor t with each element in [-max_value, max_value].""" t = tf.minimum(max_value, tf.maximum(t, -max_value)) big = quant_scale * (t + max_value) + 0.5 with tf.get_default_graph().gradient_override_map({"Floor": "CustomIdG"}): res = (tf.floor(big) / quant_scale) - max_value return res
Example #23
Source File: rnn.py From seq2seq with Apache License 2.0 | 5 votes |
def _dropout(values, recurrent_noise, keep_prob): def dropout(index, value, noise): random_tensor = keep_prob + noise binary_tensor = tf.floor(random_tensor) ret = tf.div(value, keep_prob) * binary_tensor ret.set_shape(value.get_shape()) return ret return DropoutGRUCell._enumerated_map_structure(dropout, values, recurrent_noise)
Example #24
Source File: custom_objects.py From keras_mixnets with MIT License | 5 votes |
def call(self, inputs, training=None): def drop_connect(): keep_prob = 1.0 - self.drop_connect_rate # Compute drop_connect tensor batch_size = tf.shape(inputs)[0] random_tensor = keep_prob random_tensor += tf.random_uniform([batch_size, 1, 1, 1], dtype=inputs.dtype) binary_tensor = tf.floor(random_tensor) output = (inputs / keep_prob) * binary_tensor return output return K.in_train_phase(drop_connect, inputs, training=training)
Example #25
Source File: neural_gpu.py From Gun-Detector with Apache License 2.0 | 5 votes |
def quantize(t, quant_scale, max_value=1.0): """Quantize a tensor t with each element in [-max_value, max_value].""" t = tf.minimum(max_value, tf.maximum(t, -max_value)) big = quant_scale * (t + max_value) + 0.5 with tf.get_default_graph().gradient_override_map({"Floor": "CustomIdG"}): res = (tf.floor(big) / quant_scale) - max_value return res
Example #26
Source File: layer.py From bonnet with GNU General Public License v3.0 | 5 votes |
def spatial_dropout(x, keep_prob, training, data_format="NCHW"): """ Drop random channels, using tf.nn.dropout (Partially from https://stats.stackexchange.com/questions/282282/how-is-spatial-dropout-in-2d-implemented) """ if training: with tf.variable_scope("spatial_dropout"): batch_size = x.get_shape().as_list()[0] if data_format == "NCHW": # depth of previous layer feature map prev_depth = x.get_shape().as_list()[1] num_feature_maps = [batch_size, prev_depth] else: # depth of previous layer feature map prev_depth = x.get_shape().as_list()[3] num_feature_maps = [batch_size, prev_depth] # get some uniform noise between keep_prob and 1 + keep_prob random_tensor = keep_prob random_tensor += tf.random_uniform(num_feature_maps, dtype=x.dtype) # if we take the floor of this, we get a binary matrix where # (1-keep_prob)% of the values are 0 and the rest are 1 binary_tensor = tf.floor(random_tensor) # Reshape to multiply our feature maps by this tensor correctly if data_format == "NCHW": binary_tensor = tf.reshape(binary_tensor, [batch_size, prev_depth, 1, 1]) else: binary_tensor = tf.reshape(binary_tensor, [batch_size, 1, 1, prev_depth]) # Zero out feature maps where appropriate; scale up to compensate ret = tf.div(x, keep_prob) * binary_tensor else: ret = x return ret
Example #27
Source File: autoaugment_utils.py From tpu_models with Apache License 2.0 | 5 votes |
def _randomly_negate_tensor(tensor): """With 50% prob turn the tensor negative.""" should_flip = tf.cast(tf.floor(tf.random_uniform([]) + 0.5), tf.bool) final_tensor = tf.cond(should_flip, lambda: tensor, lambda: -tensor) return final_tensor
Example #28
Source File: autoaugment.py From tpu_models with Apache License 2.0 | 5 votes |
def _randomly_negate_tensor(tensor): """With 50% prob turn the tensor negative.""" should_flip = tf.cast(tf.floor(tf.random_uniform([]) + 0.5), tf.bool) final_tensor = tf.cond(should_flip, lambda: tensor, lambda: -tensor) return final_tensor
Example #29
Source File: network_utils.py From tpu_models with Apache License 2.0 | 5 votes |
def drop_path(net, keep_prob, is_training=True): """Drops out a whole example hiddenstate with the specified probability.""" if is_training: batch_size = tf.shape(net)[0] noise_shape = [batch_size, 1, 1, 1] keep_prob = tf.cast(keep_prob, dtype=net.dtype) random_tensor = keep_prob random_tensor += tf.random_uniform(noise_shape, dtype=net.dtype) binary_tensor = tf.floor(random_tensor) net = tf.div(net, keep_prob) * binary_tensor return net
Example #30
Source File: nasnet_utils.py From Creative-Adversarial-Networks with MIT License | 5 votes |
def drop_path(net, keep_prob, is_training=True): """Drops out a whole example hiddenstate with the specified probability.""" if is_training: batch_size = tf.shape(net)[0] noise_shape = [batch_size, 1, 1, 1] random_tensor = keep_prob random_tensor += tf.random_uniform(noise_shape, dtype=tf.float32) binary_tensor = tf.floor(random_tensor) net = tf.div(net, keep_prob) * binary_tensor return net