Python tensorflow.one_hot() Examples
The following are 30
code examples of tensorflow.one_hot().
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: dqn.py From reinforcement_learning with MIT License | 6 votes |
def _build_qnet(self): """ Build q-network """ with tf.variable_scope(self.scope): self.state_input = tf.placeholder(tf.float32, [None, self.state_size]) self.action = tf.placeholder(tf.int32, [None]) self.target_q = tf.placeholder(tf.float32, [None]) fc1 = tf_utils.fc(self.state_input, n_output=self.n_hidden_1, activation_fn=tf.nn.relu) fc2 = tf_utils.fc(fc1, n_output=self.n_hidden_2, activation_fn=tf.nn.relu) self.q_values = tf_utils.fc(fc2, self.action_size, activation_fn=None) action_mask = tf.one_hot(self.action, self.action_size, 1.0, 0.0) q_value_pred = tf.reduce_sum(self.q_values * action_mask, 1) self.loss = tf.reduce_mean(tf.square(tf.subtract(self.target_q, q_value_pred))) self.optimizer = tf.train.AdamOptimizer(self.lr) self.train_op = self.optimizer.minimize(self.loss, global_step=tf.contrib.framework.get_global_step())
Example #2
Source File: metrics.py From fine-lm with MIT License | 6 votes |
def sigmoid_precision_one_hot(logits, labels, weights_fn=None): """Calculate precision for a set, given one-hot labels and logits. Predictions are converted to one-hot, as predictions[example][arg-max(example)] = 1 Args: logits: Tensor of size [batch-size, o=1, p=1, num-classes] labels: Tensor of size [batch-size, o=1, p=1, num-classes] weights_fn: Function that takes in labels and weighs examples (unused) Returns: precision (scalar), weights """ with tf.variable_scope("sigmoid_precision_one_hot", values=[logits, labels]): del weights_fn num_classes = logits.shape[-1] predictions = tf.nn.sigmoid(logits) predictions = tf.argmax(predictions, -1) predictions = tf.one_hot(predictions, num_classes) _, precision = tf.metrics.precision(labels=labels, predictions=predictions) return precision, tf.constant(1.0)
Example #3
Source File: metrics.py From fine-lm with MIT License | 6 votes |
def set_precision(predictions, labels, weights_fn=common_layers.weights_nonzero): """Precision of set predictions. Args: predictions : A Tensor of scores of shape [batch, nlabels]. labels: A Tensor of int32s giving true set elements, of shape [batch, seq_length]. weights_fn: A function to weight the elements. Returns: hits: A Tensor of shape [batch, nlabels]. weights: A Tensor of shape [batch, nlabels]. """ with tf.variable_scope("set_precision", values=[predictions, labels]): labels = tf.squeeze(labels, [2, 3]) weights = weights_fn(labels) labels = tf.one_hot(labels, predictions.shape[-1]) labels = tf.reduce_max(labels, axis=1) labels = tf.cast(labels, tf.bool) return tf.to_float(tf.equal(labels, predictions)), weights
Example #4
Source File: dsn_test.py From DOTA_models with Apache License 2.0 | 6 votes |
def _testBuildDefaultModel(self): images = tf.to_float(np.random.rand(32, 28, 28, 1)) labels = {} labels['classes'] = tf.one_hot( tf.to_int32(np.random.randint(0, 9, (32))), 10) params = { 'use_separation': True, 'layers_to_regularize': 'fc3', 'weight_decay': 0.0, 'ps_tasks': 1, 'domain_separation_startpoint': 1, 'alpha_weight': 1, 'beta_weight': 1, 'gamma_weight': 1, 'recon_loss_name': 'sum_of_squares', 'decoder_name': 'small_decoder', 'encoder_name': 'default_encoder', } return images, labels, params
Example #5
Source File: policy.py From DOTA_models with Apache License 2.0 | 6 votes |
def log_prob_action(self, action, logits, sampling_dim, act_dim, act_type): """Calculate log-prob of action sampled from distribution.""" if self.env_spec.is_discrete(act_type): act_log_prob = tf.reduce_sum( tf.one_hot(action, act_dim) * tf.nn.log_softmax(logits), -1) elif self.env_spec.is_box(act_type): means = logits[:, :sampling_dim / 2] std = logits[:, sampling_dim / 2:] act_log_prob = (- 0.5 * tf.log(2 * np.pi * tf.square(std)) - 0.5 * tf.square(action - means) / tf.square(std)) act_log_prob = tf.reduce_sum(act_log_prob, -1) else: assert False return act_log_prob
Example #6
Source File: preprocessor.py From DOTA_models with Apache License 2.0 | 6 votes |
def one_hot_encoding(labels, num_classes=None): """One-hot encodes the multiclass labels. Example usage: labels = tf.constant([1, 4], dtype=tf.int32) one_hot = OneHotEncoding(labels, num_classes=5) one_hot.eval() # evaluates to [0, 1, 0, 0, 1] Args: labels: A tensor of shape [None] corresponding to the labels. num_classes: Number of classes in the dataset. Returns: onehot_labels: a tensor of shape [num_classes] corresponding to the one hot encoding of the labels. Raises: ValueError: if num_classes is not specified. """ with tf.name_scope('OneHotEncoding', values=[labels]): if num_classes is None: raise ValueError('num_classes must be specified') labels = tf.one_hot(labels, num_classes, 1, 0) return tf.reduce_max(labels, 0)
Example #7
Source File: vision_baseline_lstm.py From DOTA_models with Apache License 2.0 | 6 votes |
def visit_count_fc(visit_count, last_visit, embed_neurons, wt_decay, fc_dropout): with tf.variable_scope('embed_visit_count'): visit_count = tf.reshape(visit_count, shape=[-1]) last_visit = tf.reshape(last_visit, shape=[-1]) visit_count = tf.clip_by_value(visit_count, clip_value_min=-1, clip_value_max=15) last_visit = tf.clip_by_value(last_visit, clip_value_min=-1, clip_value_max=15) visit_count = tf.one_hot(visit_count, depth=16, axis=1, dtype=tf.float32, on_value=10., off_value=0.) last_visit = tf.one_hot(last_visit, depth=16, axis=1, dtype=tf.float32, on_value=10., off_value=0.) f = tf.concat([visit_count, last_visit], 1) x, _ = tf_utils.fc_network( f, neurons=embed_neurons, wt_decay=wt_decay, name='visit_count_embed', offset=0, batch_norm_param=None, dropout_ratio=fc_dropout, is_training=is_training) return x
Example #8
Source File: model.py From DOTA_models with Apache License 2.0 | 6 votes |
def label_smoothing_regularization(self, chars_labels, weight=0.1): """Applies a label smoothing regularization. Uses the same method as in https://arxiv.org/abs/1512.00567. Args: chars_labels: ground truth ids of charactes, shape=[batch_size, seq_length]; weight: label-smoothing regularization weight. Returns: A sensor with the same shape as the input. """ one_hot_labels = tf.one_hot( chars_labels, depth=self._params.num_char_classes, axis=-1) pos_weight = 1.0 - weight neg_weight = weight / self._params.num_char_classes return one_hot_labels * pos_weight + neg_weight
Example #9
Source File: model.py From Neural-LP with MIT License | 6 votes |
def _build_input(self): self.tails = tf.placeholder(tf.int32, [None]) self.heads = tf.placeholder(tf.int32, [None]) self.targets = tf.one_hot(indices=self.heads, depth=self.num_entity) if not self.query_is_language: self.queries = tf.placeholder(tf.int32, [None, self.num_step]) self.query_embedding_params = tf.Variable(self._random_uniform_unit( self.num_query + 1, # <END> token self.query_embed_size), dtype=tf.float32) rnn_inputs = tf.nn.embedding_lookup(self.query_embedding_params, self.queries) else: self.queries = tf.placeholder(tf.int32, [None, self.num_step, self.num_word]) self.vocab_embedding_params = tf.Variable(self._random_uniform_unit( self.num_vocab + 1, # <END> token self.vocab_embed_size), dtype=tf.float32) embedded_query = tf.nn.embedding_lookup(self.vocab_embedding_params, self.queries) rnn_inputs = tf.reduce_mean(embedded_query, axis=2) return rnn_inputs
Example #10
Source File: metrics.py From fine-lm with MIT License | 6 votes |
def set_recall(predictions, labels, weights_fn=common_layers.weights_nonzero): """Recall of set predictions. Args: predictions : A Tensor of scores of shape [batch, nlabels]. labels: A Tensor of int32s giving true set elements, of shape [batch, seq_length]. weights_fn: A function to weight the elements. Returns: hits: A Tensor of shape [batch, nlabels]. weights: A Tensor of shape [batch, nlabels]. """ with tf.variable_scope("set_recall", values=[predictions, labels]): labels = tf.squeeze(labels, [2, 3]) weights = weights_fn(labels) labels = tf.one_hot(labels, predictions.shape[-1]) labels = tf.reduce_max(labels, axis=1) labels = tf.cast(labels, tf.bool) return tf.to_float(tf.equal(labels, predictions)), weights
Example #11
Source File: metrics.py From fine-lm with MIT License | 6 votes |
def sigmoid_recall_one_hot(logits, labels, weights_fn=None): """Calculate recall for a set, given one-hot labels and logits. Predictions are converted to one-hot, as predictions[example][arg-max(example)] = 1 Args: logits: Tensor of size [batch-size, o=1, p=1, num-classes] labels: Tensor of size [batch-size, o=1, p=1, num-classes] weights_fn: Function that takes in labels and weighs examples (unused) Returns: recall (scalar), weights """ with tf.variable_scope("sigmoid_recall_one_hot", values=[logits, labels]): del weights_fn num_classes = logits.shape[-1] predictions = tf.nn.sigmoid(logits) predictions = tf.argmax(predictions, -1) predictions = tf.one_hot(predictions, num_classes) _, recall = tf.metrics.recall(labels=labels, predictions=predictions) return recall, tf.constant(1.0)
Example #12
Source File: metrics_test.py From fine-lm with MIT License | 6 votes |
def testSequenceEditDistanceMetric(self): predictions = np.array([[3, 4, 5, 1, 0, 0], [2, 1, 3, 4, 0, 0], [2, 1, 3, 4, 0, 0]]) # Targets are just a bit different: # - first sequence has a different prediction # - second sequence has a different prediction and one extra step # - third sequence is identical targets = np.array([[5, 4, 5, 1, 0, 0], [2, 5, 3, 4, 1, 0], [2, 1, 3, 4, 0, 0]]) # Reshape to match expected input format by metric fns. predictions = np.reshape(predictions, [3, 6, 1, 1]) targets = np.reshape(targets, [3, 6, 1, 1]) with self.test_session() as session: scores, weight = metrics.sequence_edit_distance( tf.one_hot(predictions, depth=6, dtype=tf.float32), tf.constant(targets, dtype=tf.int32)) session.run(tf.global_variables_initializer()) actual_scores, actual_weight = session.run([scores, weight]) self.assertAlmostEqual(actual_scores, 3.0 / 13) self.assertEqual(actual_weight, 13)
Example #13
Source File: metrics_test.py From fine-lm with MIT License | 6 votes |
def testMultilabelMatch3(self): predictions = np.random.randint(1, 5, size=(100, 1, 1, 1)) targets = np.random.randint(1, 5, size=(100, 10, 1, 1)) weights = np.random.randint(0, 2, size=(100, 1, 1, 1)) targets *= weights predictions_repeat = np.repeat(predictions, 10, axis=1) expected = (predictions_repeat == targets).astype(float) expected = np.sum(expected, axis=(1, 2, 3)) expected = np.minimum(expected / 3.0, 1.) expected = np.sum(expected * weights[:, 0, 0, 0]) / weights.shape[0] with self.test_session() as session: scores, weights_ = metrics.multilabel_accuracy_match3( tf.one_hot(predictions, depth=5, dtype=tf.float32), tf.constant(targets, dtype=tf.int32)) a, a_op = tf.metrics.mean(scores, weights_) session.run(tf.local_variables_initializer()) session.run(tf.global_variables_initializer()) _ = session.run(a_op) actual = session.run(a) self.assertAlmostEqual(actual, expected, places=6)
Example #14
Source File: rouge_test.py From fine-lm with MIT License | 6 votes |
def testRougeLMetricE2E(self): vocab_size = 4 batch_size = 12 seq_length = 12 predictions = tf.one_hot( np.random.randint(vocab_size, size=(batch_size, seq_length, 1, 1)), depth=4, dtype=tf.float32) targets = np.random.randint(4, size=(12, 12, 1, 1)) with self.test_session() as session: scores, _ = rouge.rouge_l_fscore( predictions, tf.constant(targets, dtype=tf.int32)) a = tf.reduce_mean(scores) session.run(tf.global_variables_initializer()) session.run(a)
Example #15
Source File: transformer_nat.py From fine-lm with MIT License | 6 votes |
def vq_nearest_neighbor(x, hparams): """Find the nearest element in means to elements in x.""" bottleneck_size = 2**hparams.bottleneck_bits means = hparams.means x_norm_sq = tf.reduce_sum(tf.square(x), axis=-1, keepdims=True) means_norm_sq = tf.reduce_sum(tf.square(means), axis=-1, keepdims=True) scalar_prod = tf.matmul(x, means, transpose_b=True) dist = x_norm_sq + tf.transpose(means_norm_sq) - 2 * scalar_prod if hparams.bottleneck_kind == "em": x_means_idx = tf.multinomial(-dist, num_samples=hparams.num_samples) x_means_hot = tf.one_hot( x_means_idx, depth=bottleneck_size) x_means_hot = tf.reduce_mean(x_means_hot, axis=1) else: x_means_idx = tf.argmax(-dist, axis=-1) x_means_hot = tf.one_hot(x_means_idx, depth=bottleneck_size) x_means = tf.matmul(x_means_hot, means) e_loss = tf.reduce_mean(tf.square(x - tf.stop_gradient(x_means))) return x_means_hot, e_loss
Example #16
Source File: universal_transformer_util.py From fine-lm with MIT License | 6 votes |
def fill_memory_slot(memory, value, index): """Fills the memory slot at a particular index with the given value. Args: memory: a 4-d tensor [memory_size, batch, length, channel] containing the state of all steps value: a 3-d tensor [batch, length, channel] as the sate index: integer in [0, memory_size) Returns: filled memory """ mask = tf.to_float( tf.one_hot(index, tf.shape(memory)[0])[:, None, None, None]) fill_memory = (1 - mask) * memory + mask * value[None, ...] return fill_memory
Example #17
Source File: modalities.py From fine-lm with MIT License | 6 votes |
def targets_bottom(self, x, summary_prefix="targets_bottom"): # pylint: disable=arguments-differ inputs = x with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE): common_layers.summarize_video(inputs, summary_prefix) inputs_shape = common_layers.shape_list(inputs) # We embed each of 256=self.top_dimensionality possible pixel values. embedding_var = tf.get_variable( "pixel_embedding", [self.top_dimensionality, self.PIXEL_EMBEDDING_SIZE]) hot_inputs = tf.one_hot(tf.to_int32(inputs), self.top_dimensionality) hot_inputs = tf.reshape(hot_inputs, [-1, self.top_dimensionality]) embedded = tf.matmul(hot_inputs, embedding_var) # Let's now merge all channels that were embedded into a single vector. merged_size = self.PIXEL_EMBEDDING_SIZE * inputs_shape[4] embedded = tf.reshape(embedded, inputs_shape[:4] + [merged_size]) transposed = common_layers.time_to_channels(embedded) return tf.layers.dense( transposed, self._body_input_depth, name="merge_pixel_embedded_frames")
Example #18
Source File: preprocessor.py From vehicle_counting_tensorflow with MIT License | 6 votes |
def one_hot_encoding(labels, num_classes=None): """One-hot encodes the multiclass labels. Example usage: labels = tf.constant([1, 4], dtype=tf.int32) one_hot = OneHotEncoding(labels, num_classes=5) one_hot.eval() # evaluates to [0, 1, 0, 0, 1] Args: labels: A tensor of shape [None] corresponding to the labels. num_classes: Number of classes in the dataset. Returns: onehot_labels: a tensor of shape [num_classes] corresponding to the one hot encoding of the labels. Raises: ValueError: if num_classes is not specified. """ with tf.name_scope('OneHotEncoding', values=[labels]): if num_classes is None: raise ValueError('num_classes must be specified') labels = tf.one_hot(labels, num_classes, 1, 0) return tf.reduce_max(labels, 0)
Example #19
Source File: common_attention.py From fine-lm with MIT License | 6 votes |
def coordinate_tensor(shape, axis): """Return a tensor with given shape containing coordinate along given axis. Args: shape: a Tensor representing the shape of the output Tensor axis: an integer Returns: A tensor with shape shape and type tf.int32, where each elements its coordinate along the given axis. """ if axis < 0: axis = tf.size(shape) + axis # Convert to positive for the one_hot indice r = tf.range(shape[axis]) r_shape = tf.one_hot( axis, tf.size(shape), on_value=-1, off_value=1, dtype=tf.int32) return tf.zeros(shape, dtype=tf.int32) + tf.reshape(r, r_shape)
Example #20
Source File: ops.py From vehicle_counting_tensorflow with MIT License | 6 votes |
def matmul_gather_on_zeroth_axis(params, indices, scope=None): """Matrix multiplication based implementation of tf.gather on zeroth axis. TODO(rathodv, jonathanhuang): enable sparse matmul option. Args: params: A float32 Tensor. The tensor from which to gather values. Must be at least rank 1. indices: A Tensor. Must be one of the following types: int32, int64. Must be in range [0, params.shape[0]) scope: A name for the operation (optional). Returns: A Tensor. Has the same type as params. Values from params gathered from indices given by indices, with shape indices.shape + params.shape[1:]. """ with tf.name_scope(scope, 'MatMulGather'): params_shape = shape_utils.combined_static_and_dynamic_shape(params) indices_shape = shape_utils.combined_static_and_dynamic_shape(indices) params2d = tf.reshape(params, [params_shape[0], -1]) indicator_matrix = tf.one_hot(indices, params_shape[0]) gathered_result_flattened = tf.matmul(indicator_matrix, params2d) return tf.reshape(gathered_result_flattened, tf.stack(indices_shape + params_shape[1:]))
Example #21
Source File: audio_records.py From Tensorflow-Audio-Classification with Apache License 2.0 | 6 votes |
def iterator(self, is_onehot=True, is_shuffle=False, batch_size=64, buffer_size=512): parse_func = lambda example: parse_example(example, shape=self.shape) dataset = self.dataset.map(parse_func) # Parse the record into tensors. # Only go through the data once with no repeat. num_repeats = 1 if is_shuffle: # If training then read a buffer of the given size and randomly shuffle it. dataset = dataset.shuffle(buffer_size=buffer_size) dataset = dataset.repeat(num_repeats) # Repeat the input indefinitely. if is_onehot: onehot_func = lambda feature, label: (feature, tf.one_hot(label, self.num_classes)) dataset = dataset.map(onehot_func) dataset = dataset.batch(batch_size) iterator = dataset.make_initializable_iterator() batch = iterator.get_next() return iterator, batch
Example #22
Source File: categorical_calibration_layer.py From lattice with Apache License 2.0 | 6 votes |
def call(self, inputs): """Standard Keras call() method.""" if inputs.dtype not in [tf.uint8, tf.int32, tf.int64]: inputs = tf.cast(inputs, dtype=tf.int32) if self.default_input_value is not None: default_input_value_tensor = tf.constant( int(self.default_input_value), dtype=inputs.dtype, name=DEFAULT_INPUT_VALUE_NAME) replacement = tf.zeros_like(inputs) + (self.num_buckets - 1) inputs = tf.where( tf.equal(inputs, default_input_value_tensor), replacement, inputs) # We can't use tf.gather_nd(self.kernel, inputs) as it doesn't support # constraints (constraint functions are not supported for IndexedSlices). # Instead we use matrix multiplication by one-hot encoding of the index. if self.units == 1: # This can be slightly faster as it uses matmul. return tf.matmul( tf.one_hot(tf.squeeze(inputs, axis=[-1]), depth=self.num_buckets), self.kernel) return tf.reduce_sum( tf.one_hot(inputs, axis=1, depth=self.num_buckets) * self.kernel, axis=1)
Example #23
Source File: dqn.py From tensorflow_RL with MIT License | 6 votes |
def __init__(self, sess, output_size, mainNet, targetNet, max_length=1000000): self.memory = collections.deque(maxlen=max_length) self.mainNet = mainNet self.targetNet = targetNet self.output_size = output_size self.batch_size = 8 self.sess = sess self.gamma = 0.99 self.lr = 0.00025 self.target = tf.placeholder(tf.float32, [None]) self.action = tf.placeholder(tf.int32, [None]) self.target_vars = self.targetNet.get_trainable_variables() self.main_vars = self.mainNet.get_trainable_variables() self.update_oldpi_op = [oldp.assign(p) for p, oldp in zip(self.main_vars, self.target_vars)] self.action_one_hot = tf.one_hot(self.action, self.output_size) self.q_val = self.mainNet.Q self.q_val_action = tf.reduce_sum(self.q_val * self.action_one_hot, axis=1) self.loss = tf.reduce_mean((self.target - self.q_val_action) ** 2) self.train_op = tf.train.AdamOptimizer(learning_rate=self.lr, epsilon=1e-2).minimize(self.loss)
Example #24
Source File: reinforce_w_baseline.py From reinforcement_learning with MIT License | 6 votes |
def _build_policy_net(self): """Build policy network""" with tf.variable_scope(self.scope): self.state_input = tf.placeholder(tf.float32, [None, self.state_size]) self.action = tf.placeholder(tf.int32, [None]) self.target = tf.placeholder(tf.float32, [None]) layer_1 = tf_utils.fc(self.state_input, self.n_hidden_1, tf.nn.relu) layer_2 = tf_utils.fc(layer_1, self.n_hidden_2, tf.nn.relu) self.value = tf_utils.fc(layer_2, 1) self.action_values = tf_utils.fc(layer_2, self.action_size) action_mask = tf.one_hot(self.action, self.action_size, 1.0, 0.0) self.action_value_pred = tf.reduce_sum(tf.nn.softmax(self.action_values) * action_mask, 1) self.action_probs = tf.nn.softmax(self.action_values) self.value_loss = tf.reduce_mean(tf.square(self.target - self.value)) self.pg_loss = tf.reduce_mean(-tf.log(self.action_value_pred) * (self.target - self.value)) self.l2_loss = tf.add_n([ tf.nn.l2_loss(v) for v in tf.trainable_variables() ]) self.loss = self.pg_loss + 5*self.value_loss + 0.002 * self.l2_loss self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) self.train_op = self.optimizer.minimize(self.loss, global_step=tf.contrib.framework.get_global_step())
Example #25
Source File: reinforce.py From reinforcement_learning with MIT License | 6 votes |
def _build_policy_net(self): """Build policy network""" with tf.variable_scope(self.scope): self.state_input = tf.placeholder(tf.float32, [None, self.state_size]) self.action = tf.placeholder(tf.int32, [None]) self.target = tf.placeholder(tf.float32, [None]) layer_1 = tf_utils.fc(self.state_input, self.n_hidden_1, tf.nn.relu) layer_2 = tf_utils.fc(layer_1, self.n_hidden_2, tf.nn.relu) self.action_values = tf_utils.fc(layer_2, self.action_size) action_mask = tf.one_hot(self.action, self.action_size, 1.0, 0.0) self.action_prob = tf.nn.softmax(self.action_values) self.action_value_pred = tf.reduce_sum(self.action_prob * action_mask, 1) # l2 regularization self.l2_loss = tf.add_n([ tf.nn.l2_loss(v) for v in tf.trainable_variables() ]) self.pg_loss = tf.reduce_mean(-tf.log(self.action_value_pred) * self.target) self.loss = self.pg_loss + 0.002 * self.l2_loss self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr) self.train_op = self.optimizer.minimize(self.loss, global_step=tf.contrib.framework.get_global_step())
Example #26
Source File: preprocessor.py From object_detector_app with MIT License | 6 votes |
def one_hot_encoding(labels, num_classes=None): """One-hot encodes the multiclass labels. Example usage: labels = tf.constant([1, 4], dtype=tf.int32) one_hot = OneHotEncoding(labels, num_classes=5) one_hot.eval() # evaluates to [0, 1, 0, 0, 1] Args: labels: A tensor of shape [None] corresponding to the labels. num_classes: Number of classes in the dataset. Returns: onehot_labels: a tensor of shape [num_classes] corresponding to the one hot encoding of the labels. Raises: ValueError: if num_classes is not specified. """ with tf.name_scope('OneHotEncoding', values=[labels]): if num_classes is None: raise ValueError('num_classes must be specified') labels = tf.one_hot(labels, num_classes, 1, 0) return tf.reduce_max(labels, 0)
Example #27
Source File: distributions.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def neglogp(self, x): # return tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits, labels=x) # Note: we can't use sparse_softmax_cross_entropy_with_logits because # the implementation does not allow second-order derivatives... if x.dtype in {tf.uint8, tf.int32, tf.int64}: # one-hot encoding x_shape_list = x.shape.as_list() logits_shape_list = self.logits.get_shape().as_list()[:-1] for xs, ls in zip(x_shape_list, logits_shape_list): if xs is not None and ls is not None: assert xs == ls, 'shape mismatch: {} in x vs {} in logits'.format(xs, ls) x = tf.one_hot(x, self.logits.get_shape().as_list()[-1]) else: # already encoded assert x.shape.as_list() == self.logits.shape.as_list() return tf.nn.softmax_cross_entropy_with_logits_v2( logits=self.logits, labels=x)
Example #28
Source File: models.py From nlp-tensorflow with MIT License | 5 votes |
def _build_net(self): with tf.variable_scope("placeholder"): self.input_x = tf.placeholder(tf.float32, shape=(None, self.vocab_size)) self.input_y = tf.placeholder(tf.int32, shape=(None,)) Y_one_hot = tf.one_hot(self.input_y, self.n_class) with tf.variable_scope("output", reuse=tf.AUTO_REUSE): W = tf.get_variable('W', dtype=tf.float32, initializer=tf.truncated_normal((self.vocab_size, self.n_class))) b = tf.get_variable('b', dtype=tf.float32, initializer=tf.constant(0.1, shape=(self.n_class,))) logits = tf.nn.xw_plus_b(self.input_x, W, b) self.prob = tf.reduce_max(tf.nn.softmax(logits), axis=1) self.prediction = tf.cast(tf.argmax(logits, axis=1), tf.int32) with tf.variable_scope("loss"): self.loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y_one_hot)) with tf.variable_scope("train", reuse=tf.AUTO_REUSE): optimizer = tf.train.AdamOptimizer(self.lr) self.train_op = optimizer.minimize(self.loss) with tf.variable_scope("accuracy"): correct = tf.equal(self.prediction, self.input_y) self.accuracy = tf.reduce_mean(tf.cast(correct, tf.float32)) self.sess.run(tf.global_variables_initializer())
Example #29
Source File: goal_nav_agent.py From streetlearn with Apache License 2.0 | 5 votes |
def _torso(self, input_): """Processing of all the visual and language inputs to the LSTM core.""" # Extract the inputs last_action, env_output = input_ last_reward, _, _, observation = env_output frame = observation[self._idx_frame] goal = observation[self._idx_goal] goal = tf.to_float(goal) # Convert to image to floats and normalise. frame = tf.to_float(frame) frame = snt.FlattenTrailingDimensions(dim_from=3)(frame) frame /= 255.0 # Feed image through convnet. with tf.variable_scope('convnet'): # Convolutional layers. conv_out = self._convnet(frame) # Fully connected layer. conv_out = snt.BatchFlatten()(conv_out) conv_out = snt.Linear(256)(conv_out) conv_out = tf.nn.relu(conv_out) # Concatenate outputs of the visual and instruction pathways. if self._feed_action_and_reward: # Append clipped last reward and one hot last action. tf.logging.info('Append last reward clipped to: %f', self._max_reward) clipped_last_reward = tf.expand_dims( tf.clip_by_value(last_reward, -self._max_reward, self._max_reward), -1) tf.logging.info('Append last action (one-hot of %d)', self._num_actions) one_hot_last_action = tf.one_hot(last_action, self._num_actions) tf.logging.info('Append goal:') tf.logging.info(goal) action_and_reward = tf.concat([clipped_last_reward, one_hot_last_action], axis=1) else: action_and_reward = tf.constant([0], dtype=tf.float32) return conv_out, action_and_reward, goal
Example #30
Source File: attacks_tf.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 5 votes |
def margin_logit_loss(model_logits, label, num_classes=10): """Computes difference between logit for `label` and next highest logit. The loss is high when `label` is unlikely (targeted by default). This follows the same interface as `loss_fn` for UnrolledOptimizer and pgd_attack, i.e. it returns a batch of loss values. """ logit_mask = tf.one_hot(label, depth=num_classes, axis=-1) label_logits = reduce_sum(logit_mask * model_logits, axis=-1) logits_with_target_label_neg_inf = model_logits - logit_mask * 99999 highest_nonlabel_logits = reduce_max( logits_with_target_label_neg_inf, axis=-1) loss = highest_nonlabel_logits - label_logits return loss