Python tensorflow.py_func() Examples
The following are 30
code examples of tensorflow.py_func().
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: utils_pytorch.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 7 votes |
def _py_func_with_gradient(func, inp, Tout, stateful=True, name=None, grad_func=None): """ PyFunc defined as given by Tensorflow :param func: Custom Function :param inp: Function Inputs :param Tout: Ouput Type of out Custom Function :param stateful: Calculate Gradients when stateful is True :param name: Name of the PyFunction :param grad: Custom Gradient Function :return: """ # Generate random name in order to avoid conflicts with inbuilt names rnd_name = 'PyFuncGrad-' + '%0x' % getrandbits(30 * 4) # Register Tensorflow Gradient tf.RegisterGradient(rnd_name)(grad_func) # Get current graph g = tf.get_default_graph() # Add gradient override map with g.gradient_override_map( {"PyFunc": rnd_name, "PyFuncStateless": rnd_name}): return tf.py_func(func, inp, Tout, stateful=stateful, name=name)
Example #2
Source File: mpi_adam_optimizer.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def compute_gradients(self, loss, var_list, **kwargs): grads_and_vars = tf.train.AdamOptimizer.compute_gradients(self, loss, var_list, **kwargs) grads_and_vars = [(g, v) for g, v in grads_and_vars if g is not None] flat_grad = tf.concat([tf.reshape(g, (-1,)) for g, v in grads_and_vars], axis=0) shapes = [v.shape.as_list() for g, v in grads_and_vars] sizes = [int(np.prod(s)) for s in shapes] num_tasks = self.comm.Get_size() buf = np.zeros(sum(sizes), np.float32) def _collect_grads(flat_grad): self.comm.Allreduce(flat_grad, buf, op=MPI.SUM) np.divide(buf, float(num_tasks), out=buf) return buf avg_flat_grad = tf.py_func(_collect_grads, [flat_grad], tf.float32) avg_flat_grad.set_shape(flat_grad.shape) avg_grads = tf.split(avg_flat_grad, sizes, axis=0) avg_grads_and_vars = [(tf.reshape(g, v.shape), v) for g, (_, v) in zip(avg_grads, grads_and_vars)] return avg_grads_and_vars
Example #3
Source File: iou_rotate.py From R2CNN_Faster-RCNN_Tensorflow with MIT License | 6 votes |
def iou_rotate_calculate(boxes1, boxes2, use_gpu=True, gpu_id=0): ''' :param boxes_list1:[N, 8] tensor :param boxes_list2: [M, 8] tensor :return: ''' boxes1 = tf.cast(boxes1, tf.float32) boxes2 = tf.cast(boxes2, tf.float32) if use_gpu: iou_matrix = tf.py_func(rbbx_overlaps, inp=[boxes1, boxes2, gpu_id], Tout=tf.float32) else: iou_matrix = tf.py_func(get_iou_matrix, inp=[boxes1, boxes2], Tout=tf.float32) iou_matrix = tf.reshape(iou_matrix, [tf.shape(boxes1)[0], tf.shape(boxes2)[0]]) return iou_matrix
Example #4
Source File: nms_rotate.py From R2CNN_Faster-RCNN_Tensorflow with MIT License | 6 votes |
def nms_rotate_gpu(boxes_list, scores, iou_threshold, use_angle_condition=False, angle_gap_threshold=0, device_id=0): if use_angle_condition: x_c, y_c, w, h, theta = tf.unstack(boxes_list, axis=1) boxes_list = tf.transpose(tf.stack([x_c, y_c, w, h, theta])) det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1) keep = tf.py_func(rotate_gpu_nms, inp=[det_tensor, iou_threshold, device_id], Tout=tf.int64) return keep else: x_c, y_c, w, h, theta = tf.unstack(boxes_list, axis=1) boxes_list = tf.transpose(tf.stack([x_c, y_c, w, h, theta])) det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1) keep = tf.py_func(rotate_gpu_nms, inp=[det_tensor, iou_threshold, device_id], Tout=tf.int64) keep = tf.reshape(keep, [-1]) return keep
Example #5
Source File: in_graph_batch_env.py From soccer-matlab with BSD 2-Clause "Simplified" License | 6 votes |
def simulate(self, action): """Step the batch of environments. The results of the step can be accessed from the variables defined below. Args: action: Tensor holding the batch of actions to apply. Returns: Operation. """ with tf.name_scope('environment/simulate'): if action.dtype in (tf.float16, tf.float32, tf.float64): action = tf.check_numerics(action, 'action') observ_dtype = self._parse_dtype(self._batch_env.observation_space) observ, reward, done = tf.py_func( lambda a: self._batch_env.step(a)[:3], [action], [observ_dtype, tf.float32, tf.bool], name='step') observ = tf.check_numerics(observ, 'observ') reward = tf.check_numerics(reward, 'reward') return tf.group( self._observ.assign(observ), self._action.assign(action), self._reward.assign(reward), self._done.assign(done))
Example #6
Source File: in_graph_batch_env.py From soccer-matlab with BSD 2-Clause "Simplified" License | 6 votes |
def simulate(self, action): """Step the batch of environments. The results of the step can be accessed from the variables defined below. Args: action: Tensor holding the batch of actions to apply. Returns: Operation. """ with tf.name_scope('environment/simulate'): if action.dtype in (tf.float16, tf.float32, tf.float64): action = tf.check_numerics(action, 'action') observ_dtype = self._parse_dtype(self._batch_env.observation_space) observ, reward, done = tf.py_func( lambda a: self._batch_env.step(a)[:3], [action], [observ_dtype, tf.float32, tf.bool], name='step') observ = tf.check_numerics(observ, 'observ') reward = tf.check_numerics(reward, 'reward') return tf.group( self._observ.assign(observ), self._action.assign(action), self._reward.assign(reward), self._done.assign(done))
Example #7
Source File: in_graph_batch_env.py From soccer-matlab with BSD 2-Clause "Simplified" License | 6 votes |
def reset(self, indices=None): """Reset the batch of environments. Args: indices: The batch indices of the environments to reset; defaults to all. Returns: Batch tensor of the new observations. """ if indices is None: indices = tf.range(len(self._batch_env)) observ_dtype = self._parse_dtype(self._batch_env.observation_space) observ = tf.py_func( self._batch_env.reset, [indices], observ_dtype, name='reset') observ = tf.check_numerics(observ, 'observ') reward = tf.zeros_like(indices, tf.float32) done = tf.zeros_like(indices, tf.bool) with tf.control_dependencies([ tf.scatter_update(self._observ, indices, observ), tf.scatter_update(self._reward, indices, reward), tf.scatter_update(self._done, indices, done)]): return tf.identity(observ)
Example #8
Source File: network.py From TFFRCNN with MIT License | 6 votes |
def proposal_target_layer(self, input, classes, name): if isinstance(input[0], tuple): input[0] = input[0][0] with tf.variable_scope(name) as scope: #inputs: 'rpn_rois','gt_boxes', 'gt_ishard', 'dontcare_areas' rois,labels,bbox_targets,bbox_inside_weights,bbox_outside_weights \ = tf.py_func(proposal_target_layer_py, [input[0],input[1],input[2],input[3],classes], [tf.float32,tf.float32,tf.float32,tf.float32,tf.float32]) # rois <- (1 x H x W x A, 5) e.g. [0, x1, y1, x2, y2] # rois = tf.convert_to_tensor(rois, name='rois') rois = tf.reshape(rois, [-1, 5], name='rois') # goes to roi_pooling labels = tf.convert_to_tensor(tf.cast(labels,tf.int32), name = 'labels') # goes to FRCNN loss bbox_targets = tf.convert_to_tensor(bbox_targets, name = 'bbox_targets') # goes to FRCNN loss bbox_inside_weights = tf.convert_to_tensor(bbox_inside_weights, name = 'bbox_inside_weights') bbox_outside_weights = tf.convert_to_tensor(bbox_outside_weights, name = 'bbox_outside_weights') self.layers['rois'] = rois return rois, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights
Example #9
Source File: network.py From TFFRCNN with MIT License | 6 votes |
def anchor_target_layer(self, input, _feat_stride, anchor_scales, name): if isinstance(input[0], tuple): input[0] = input[0][0] with tf.variable_scope(name) as scope: # 'rpn_cls_score', 'gt_boxes', 'gt_ishard', 'dontcare_areas', 'im_info' rpn_labels,rpn_bbox_targets,rpn_bbox_inside_weights,rpn_bbox_outside_weights = \ tf.py_func(anchor_target_layer_py, [input[0],input[1],input[2],input[3],input[4], _feat_stride, anchor_scales], [tf.float32,tf.float32,tf.float32,tf.float32]) rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels,tf.int32), name = 'rpn_labels') # shape is (1 x H x W x A, 2) rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name = 'rpn_bbox_targets') # shape is (1 x H x W x A, 4) rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights , name = 'rpn_bbox_inside_weights') # shape is (1 x H x W x A, 4) rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights , name = 'rpn_bbox_outside_weights') # shape is (1 x H x W x A, 4) return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights
Example #10
Source File: py_func_batch_env.py From fine-lm with MIT License | 6 votes |
def simulate(self, action): """Step the batch of environments. The results of the step can be accessed from the variables defined below. Args: action: Tensor holding the batch of actions to apply. Returns: Operation. """ with tf.name_scope('environment/simulate'): if action.dtype in (tf.float16, tf.float32, tf.float64): action = tf.check_numerics(action, 'action') observ_dtype = utils.parse_dtype(self._batch_env.observation_space) observ, reward, done = tf.py_func( lambda a: self._batch_env.step(a)[:3], [action], [observ_dtype, tf.float32, tf.bool], name='step') observ = tf.check_numerics(observ, 'observ') reward = tf.check_numerics(reward, 'reward') reward.set_shape((len(self),)) done.set_shape((len(self),)) with tf.control_dependencies([self._observ.assign(observ)]): return tf.identity(reward), tf.identity(done)
Example #11
Source File: rouge.py From fine-lm with MIT License | 6 votes |
def rouge_l_fscore(predictions, labels, **unused_kwargs): """ROUGE scores computation between labels and predictions. This is an approximate ROUGE scoring method since we do not glue word pieces or decode the ids and tokenize the output. Args: predictions: tensor, model predictions labels: tensor, gold output. Returns: rouge_l_fscore: approx rouge-l f1 score. """ outputs = tf.to_int32(tf.argmax(predictions, axis=-1)) # Convert the outputs and labels to a [batch_size, input_length] tensor. outputs = tf.squeeze(outputs, axis=[-1, -2]) labels = tf.squeeze(labels, axis=[-1, -2]) rouge_l_f_score = tf.py_func(rouge_l_sentence_level, (outputs, labels), tf.float32) return rouge_l_f_score, tf.constant(1.0)
Example #12
Source File: rouge.py From fine-lm with MIT License | 6 votes |
def rouge_2_fscore(predictions, labels, **unused_kwargs): """ROUGE-2 F1 score computation between labels and predictions. This is an approximate ROUGE scoring method since we do not glue word pieces or decode the ids and tokenize the output. Args: predictions: tensor, model predictions labels: tensor, gold output. Returns: rouge2_fscore: approx rouge-2 f1 score. """ outputs = tf.to_int32(tf.argmax(predictions, axis=-1)) # Convert the outputs and labels to a [batch_size, input_length] tensor. outputs = tf.squeeze(outputs, axis=[-1, -2]) labels = tf.squeeze(labels, axis=[-1, -2]) rouge_2_f_score = tf.py_func(rouge_n, (outputs, labels), tf.float32) return rouge_2_f_score, tf.constant(1.0)
Example #13
Source File: train.py From fine-lm with MIT License | 6 votes |
def add_noise(ids, sequence_length): """Wraps add_noise_python for a batch of tensors.""" def _add_noise_single(ids, sequence_length): noisy_ids = add_noise_python(ids[:sequence_length]) noisy_sequence_length = len(noisy_ids) ids[:noisy_sequence_length] = noisy_ids ids[noisy_sequence_length:] = 0 return ids, np.int32(noisy_sequence_length) noisy_ids, noisy_sequence_length = tf.map_fn( lambda x: tf.py_func(_add_noise_single, x, [ids.dtype, tf.int32]), [ids, sequence_length], dtype=[ids.dtype, tf.int32], back_prop=False) noisy_ids.set_shape(ids.get_shape()) noisy_sequence_length.set_shape(sequence_length.get_shape()) return noisy_ids, noisy_sequence_length # Step 3
Example #14
Source File: py_func_batch_env.py From fine-lm with MIT License | 6 votes |
def _reset_non_empty(self, indices): """Reset the batch of environments. Args: indices: The batch indices of the environments to reset; defaults to all. Returns: Batch tensor of the new observations. """ observ_dtype = utils.parse_dtype(self._batch_env.observation_space) observ = tf.py_func( self._batch_env.reset, [indices], observ_dtype, name='reset') observ = tf.check_numerics(observ, 'observ') with tf.control_dependencies([ tf.scatter_update(self._observ, indices, observ)]): return tf.identity(observ)
Example #15
Source File: mpi_adam_optimizer.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def compute_gradients(self, loss, var_list, **kwargs): grads_and_vars = tf.train.AdamOptimizer.compute_gradients(self, loss, var_list, **kwargs) grads_and_vars = [(g, v) for g, v in grads_and_vars if g is not None] flat_grad = tf.concat([tf.reshape(g, (-1,)) for g, v in grads_and_vars], axis=0) shapes = [v.shape.as_list() for g, v in grads_and_vars] sizes = [int(np.prod(s)) for s in shapes] num_tasks = self.comm.Get_size() buf = np.zeros(sum(sizes), np.float32) def _collect_grads(flat_grad): self.comm.Allreduce(flat_grad, buf, op=MPI.SUM) np.divide(buf, float(num_tasks), out=buf) return buf avg_flat_grad = tf.py_func(_collect_grads, [flat_grad], tf.float32) avg_flat_grad.set_shape(flat_grad.shape) avg_grads = tf.split(avg_flat_grad, sizes, axis=0) avg_grads_and_vars = [(tf.reshape(g, v.shape), v) for g, (_, v) in zip(avg_grads, grads_and_vars)] return avg_grads_and_vars
Example #16
Source File: mpi_adam_optimizer.py From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 | 6 votes |
def compute_gradients(self, loss, var_list, **kwargs): grads_and_vars = tf.train.AdamOptimizer.compute_gradients(self, loss, var_list, **kwargs) grads_and_vars = [(g, v) for g, v in grads_and_vars if g is not None] flat_grad = tf.concat([tf.reshape(g, (-1,)) for g, v in grads_and_vars], axis=0) shapes = [v.shape.as_list() for g, v in grads_and_vars] sizes = [int(np.prod(s)) for s in shapes] num_tasks = self.comm.Get_size() buf = np.zeros(sum(sizes), np.float32) def _collect_grads(flat_grad): self.comm.Allreduce(flat_grad, buf, op=MPI.SUM) np.divide(buf, float(num_tasks), out=buf) return buf avg_flat_grad = tf.py_func(_collect_grads, [flat_grad], tf.float32) avg_flat_grad.set_shape(flat_grad.shape) avg_grads = tf.split(avg_flat_grad, sizes, axis=0) avg_grads_and_vars = [(tf.reshape(g, v.shape), v) for g, (_, v) in zip(avg_grads, grads_and_vars)] return avg_grads_and_vars
Example #17
Source File: gt.py From keras-ctpn with Apache License 2.0 | 6 votes |
def generate_gt_graph(gt_quadrilaterals, input_gt_class_ids, image_shape, width_stride, max_gt_num): """ :param gt_quadrilaterals: [mat_gt_num,(x1,y1,x2,y2,x3,y3,x4,y4,tag)] 左上、右上、右下、左下(顺时针) :param input_gt_class_ids: :param image_shape: :param width_stride: :param max_gt_num: :return: """ gt_quadrilaterals = tf_utils.remove_pad(gt_quadrilaterals) input_gt_class_ids = tf_utils.remove_pad(input_gt_class_ids) gt_boxes, gt_class_ids = tf.py_func(func=gt_utils.gen_gt_from_quadrilaterals, inp=[gt_quadrilaterals, input_gt_class_ids, image_shape, width_stride], Tout=[tf.float32] * 2) return tf_utils.pad_list_to_fixed_size([gt_boxes, gt_class_ids], max_gt_num)
Example #18
Source File: rouge_tensor.py From TransferRL with MIT License | 6 votes |
def rouge_2_fscore(predictions, labels, **unused_kwargs): """ROUGE-2 F1 score computation between labels and predictions. This is an approximate ROUGE scoring method since we do not glue word pieces or decode the ids and tokenize the output. Args: predictions: tensor, model predictions (batch_size, <=max_dec_steps) labels: tensor, gold output. (batch_size, max_dec_steps) Returns: rouge2_fscore: approx rouge-2 f1 score. """ rouge_2_f_score = tf.py_func(rouge_n, (predictions, labels), [tf.float32]) return rouge_2_f_score, tf.constant(1.0)
Example #19
Source File: rouge_tensor.py From TransferRL with MIT License | 6 votes |
def rouge_l_fscore(hypothesis, references, **unused_kwargs): """ROUGE scores computation between labels and predictions. This is an approximate ROUGE scoring method since we do not glue word pieces or decode the ids and tokenize the output. Args: predictions: tensor, model predictions (batch_size, <=max_dec_steps) labels: tensor, gold output. (batch_size, max_dec_steps) Returns: rouge_l_fscore: approx rouge-l f1 score. """ rouge_l_f_score = tf.py_func(rouge_l_sentence_level, (hypothesis, references), [tf.float32]) #rouge_l_f_score = tf.py_func(rouge_l_sentence_level, (hypothesis, references), tf.float32) return rouge_l_f_score
Example #20
Source File: model_train.py From ICDAR-2019-SROIE with MIT License | 6 votes |
def anchor_target_layer(cls_pred, bbox, im_info, scope_name): with tf.variable_scope(scope_name) as scope: # 'rpn_cls_score', 'gt_boxes', 'im_info' rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = \ tf.py_func(anchor_target_layer_py, [cls_pred, bbox, im_info, [16, ], [16]], [tf.float32, tf.float32, tf.float32, tf.float32]) rpn_labels = tf.convert_to_tensor(tf.cast(rpn_labels, tf.int32), name='rpn_labels') rpn_bbox_targets = tf.convert_to_tensor(rpn_bbox_targets, name='rpn_bbox_targets') rpn_bbox_inside_weights = tf.convert_to_tensor(rpn_bbox_inside_weights, name='rpn_bbox_inside_weights') rpn_bbox_outside_weights = tf.convert_to_tensor(rpn_bbox_outside_weights, name='rpn_bbox_outside_weights') return [rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights]
Example #21
Source File: ms_ssim_np.py From imgcomp-cvpr with GNU General Public License v3.0 | 6 votes |
def tf_msssim_np(img1, img2, data_format='NHWC'): assert img1.shape.ndims == img2.shape.ndims == 4, 'Expected {}, got {}'.format(data_format, img1.shape, img2.shape) assert tf.uint8.is_compatible_with(img1.dtype), 'Expected uint8 intput' assert tf.uint8.is_compatible_with(img2.dtype), 'Expected uint8 intput' if data_format == 'NCHW': def make_NHWC(x): return tf.transpose(x, (0, 2, 3, 1), name='make_NHWC') return tf_msssim_np(make_NHWC(img1), make_NHWC(img2), data_format='NHWC') assert img1.shape[3] == 3, 'Expected 3-channel images, got {}'.format(img1) with tf.name_scope('ms-ssim_np'): v = tf.py_func(_calc_msssim_orig, [img1, img2], tf.float32, stateful=False, name='MS-SSIM') v.set_shape(()) return v
Example #22
Source File: mpi_adam_optimizer.py From HardRLWithYoutube with MIT License | 6 votes |
def compute_gradients(self, loss, var_list, **kwargs): grads_and_vars = tf.train.AdamOptimizer.compute_gradients(self, loss, var_list, **kwargs) grads_and_vars = [(g, v) for g, v in grads_and_vars if g is not None] flat_grad = tf.concat([tf.reshape(g, (-1,)) for g, v in grads_and_vars], axis=0) shapes = [v.shape.as_list() for g, v in grads_and_vars] sizes = [int(np.prod(s)) for s in shapes] num_tasks = self.comm.Get_size() buf = np.zeros(sum(sizes), np.float32) def _collect_grads(flat_grad): self.comm.Allreduce(flat_grad, buf, op=MPI.SUM) np.divide(buf, float(num_tasks), out=buf) return buf avg_flat_grad = tf.py_func(_collect_grads, [flat_grad], tf.float32) avg_flat_grad.set_shape(flat_grad.shape) avg_grads = tf.split(avg_flat_grad, sizes, axis=0) avg_grads_and_vars = [(tf.reshape(g, v.shape), v) for g, (_, v) in zip(avg_grads, grads_and_vars)] return avg_grads_and_vars
Example #23
Source File: bleu_hook.py From fine-lm with MIT License | 6 votes |
def bleu_score(predictions, labels, **unused_kwargs): """BLEU score computation between labels and predictions. An approximate BLEU scoring method since we do not glue word pieces or decode the ids and tokenize the output. By default, we use ngram order of 4 and use brevity penalty. Also, this does not have beam search. Args: predictions: tensor, model predictions labels: tensor, gold output. Returns: bleu: int, approx bleu score """ outputs = tf.to_int32(tf.argmax(predictions, axis=-1)) # Convert the outputs and labels to a [batch_size, input_length] tensor. outputs = tf.squeeze(outputs, axis=[-1, -2]) labels = tf.squeeze(labels, axis=[-1, -2]) bleu = tf.py_func(compute_bleu, (labels, outputs), tf.float32) return bleu, tf.constant(1.0)
Example #24
Source File: core.py From lm-human-preferences with MIT License | 5 votes |
def mpi_bcast(comm, value, root=0): """Broadcast value from root to other processes via a TensorFlow py_func.""" value = tf.convert_to_tensor(value) if comm.Get_size() == 1: return value comm = comm.Dup() # Allow parallelism at graph execution time if comm.Get_rank() == root: out = tf.py_func(partial(comm.bcast, root=root), [value], value.dtype) else: out = tf.py_func(partial(comm.bcast, None, root=root), [], value.dtype) out.set_shape(value.shape) return out
Example #25
Source File: ssd_common.py From lambda-deep-learning-demo with Apache License 2.0 | 5 votes |
def loss(gt, outputs, class_weights, bboxes_weights): gt_classes, gt_bboxes, gt_mask = gt feat_classes = outputs[0] feat_bboxes = outputs[1] gt_mask = tf.reshape(gt_mask, [-1]) logits_classes = tf.reshape(feat_classes, [-1, tf.shape(feat_classes)[2]]) gt_classes = tf.reshape(gt_classes, [-1, 1]) logits_bboxes = tf.reshape(feat_bboxes, [-1, 4]) gt_bboxes = tf.reshape(gt_bboxes, [-1, 4]) # # heuristic sampling # fg_index, bg_index = tf.py_func( # heuristic_sampling, [gt_mask], (tf.int64, tf.int64)) # hard negative mining fg_index, bg_index = hard_negative_mining(logits_classes, gt_mask) loss_classes = class_weights * create_loss_classes_fn(logits_classes, gt_classes, fg_index, bg_index) loss_bboxes = bboxes_weights * create_loss_bboxes_fn(logits_bboxes, gt_bboxes, fg_index) return loss_classes, loss_bboxes # ------------------------------------------------------------------------ # Detection head # ------------------------------------------------------------------------
Example #26
Source File: core.py From lm-human-preferences with MIT License | 5 votes |
def mpi_allreduce_sum(values, *, comm): if comm.Get_size() == 1: return values orig_dtype = values.dtype if hvd is None: orig_shape = values.shape def _allreduce(vals): buf = np.zeros(vals.shape, np.float32) comm.Allreduce(vals, buf, op=MPI.SUM) return buf values = tf.py_func(_allreduce, [values], tf.float32) values.set_shape(orig_shape) else: values = hvd.mpi_ops._allreduce(values) return tf.cast(values, dtype=orig_dtype)
Example #27
Source File: utils.py From transformer with Apache License 2.0 | 5 votes |
def convert_idx_to_token_tensor(inputs, idx2token): '''Converts int32 tensor to string tensor. inputs: 1d int32 tensor. indices. idx2token: dictionary Returns 1d string tensor. ''' def my_func(inputs): return " ".join(idx2token[elem] for elem in inputs) return tf.py_func(my_func, [inputs], tf.string) # # def pad(x, maxlen): # # '''Pads x, list of sequences, and make it as a numpy array. # # x: list of sequences. e.g., [[2, 3, 4], [5, 6, 7, 8, 9], ...] # # maxlen: scalar # # # # Returns # # numpy int32 array of (len(x), maxlen) # # ''' # # padded = [] # # for seq in x: # # seq += [0] * (maxlen - len(seq)) # # padded.append(seq) # # # # arry = np.array(padded, np.int32) # # assert arry.shape == (len(x), maxlen), "Failed to make an array" # # return arry
Example #28
Source File: squad_data.py From mipsqa with Apache License 2.0 | 5 votes |
def get_answer_op(context, context_words, answer_start, answer_end): return tf.py_func( _enum_fn(_get_answer), [context, context_words, answer_start, answer_end], 'string')
Example #29
Source File: losses.py From Pixel2MeshPlusPlus with BSD 3-Clause "New" or "Revised" License | 5 votes |
def sample(pred, placeholders, block_id): uni = tf.distributions.Uniform(low=0.0, high=1.0) faces = placeholders['faces_triangle'][block_id - 1] tilefaces = tf.py_func(choice_faces, [pred, faces], tf.int32) num_of_tile_faces = tf.shape(tilefaces)[0] xs = tf.gather(pred, tilefaces[:, 0]) ys = tf.gather(pred, tilefaces[:, 1]) zs = tf.gather(pred, tilefaces[:, 2]) u = tf.sqrt(uni.sample([num_of_tile_faces, 1])) v = uni.sample([num_of_tile_faces, 1]) points = (1 - u) * xs + (u * (1 - v)) * ys + u * v * zs return points
Example #30
Source File: wrapper.py From FastMaskRCNN with Apache License 2.0 | 5 votes |
def anchor_decoder(boxes, scores, all_anchors, ih, iw, scope='AnchorDecoder'): with tf.name_scope(scope) as sc: final_boxes, classes, scores = \ tf.py_func(anchor.decode, [boxes, scores, all_anchors, ih, iw], [tf.float32, tf.int32, tf.float32]) final_boxes = tf.convert_to_tensor(final_boxes, name='boxes') classes = tf.convert_to_tensor(tf.cast(classes, tf.int32), name='classes') scores = tf.convert_to_tensor(scores, name='scores') final_boxes = tf.reshape(final_boxes, (-1, 4)) classes = tf.reshape(classes, (-1, )) scores = tf.reshape(scores, (-1, )) return final_boxes, classes, scores