Python convert tensor
60 Python code examples are found related to "
convert tensor".
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.
Example 1
Source File: caffe_translator.py From optimized-models with Apache License 2.0 | 6 votes |
def ConvertTensorProtosToInitNet(net_params, input_name): """Takes the net_params returned from TranslateModel, and wrap it as an init net that contain GivenTensorFill. This is a very simple feature that only works with float tensors, and is only intended to be used in an environment where you want a single initialization file - for more complex cases, use a db to store the parameters. """ init_net = caffe2_pb2.NetDef() for tensor in net_params.protos: if len(tensor.float_data) == 0: raise RuntimeError( "Only float tensors are supported in this util.") op = core.CreateOperator( "GivenTensorFill", [], [tensor.name], arg=[ utils.MakeArgument("shape", list(tensor.dims)), utils.MakeArgument("values", tensor.float_data)]) init_net.op.extend([op]) init_net.op.extend([core.CreateOperator("ConstantFill", [], [input_name], shape=[1])]) return init_net
Example 2
Source File: metric_learning_test.py From tf-slim with Apache License 2.0 | 6 votes |
def convert_to_list_of_sparse_tensor(np_matrix): list_of_sparse_tensors = [] nrows, ncols = np_matrix.shape for i in range(nrows): sp_indices = [] for j in range(ncols): if np_matrix[i][j] == 1: sp_indices.append([j]) num_non_zeros = len(sp_indices) list_of_sparse_tensors.append(sparse_tensor.SparseTensor( indices=np.array(sp_indices), values=np.ones((num_non_zeros,)), dense_shape=np.array([ncols,]))) return list_of_sparse_tensors
Example 3
Source File: common_layers.py From training_results_v0.5 with Apache License 2.0 | 6 votes |
def convert_gradient_to_tensor(x): """Identity operation whose gradient is converted to a `Tensor`. Currently, the gradient to `tf.concat` is particularly expensive to compute if dy is an `IndexedSlices` (a lack of GPU implementation forces the gradient operation onto CPU). This situation occurs when the output of the `tf.concat` is eventually passed to `tf.gather`. It is sometimes faster to convert the gradient to a `Tensor`, so as to get the cheaper gradient for `tf.concat`. To do this, replace `tf.concat(x)` with `convert_gradient_to_tensor(tf.concat(x))`. Args: x: A `Tensor`. Returns: The input `Tensor`. """ return x
Example 4
Source File: load_and_format_data.py From CDSS with GNU General Public License v3.0 | 6 votes |
def convert_to_tensor(frame_list): ''' Converts a list pandas dataframes into a tensor. Parameters: @frame_list: (list of pandas dataframes) all patient data for a split Returns: @tensor: (tensor i.e numpy nd array) ''' tensor = [] for frame in frame_list: tensor.append(frame.as_matrix()) tensor = np.asarray(tensor) return tensor
Example 5
Source File: generate_programs.py From XNM-Net with MIT License | 6 votes |
def convert_question_str_to_tensor(question, vocab, question_len): if isinstance(question, str): question = punctuation_regex.sub('', question).split() # tell the user they can't use unknown words question_token_to_idx = vocab['question_token_to_idx'] if any(word not in question_token_to_idx for word in question): print('Error: there are unknown words in the question you provided!') print('Unknown words:') print([word for word in question if word not in question_token_to_idx]) assert False # encode the question using our vocab encoded = np.zeros((1, question_len), dtype='int64') encoded[0, 0] = question_token_to_idx['<START>'] encoded[0, 1:len(question)+1] = [question_token_to_idx[word] for word in question] encoded[0, len(question)+1] = question_token_to_idx['<END>'] question_tensor = torch.LongTensor(encoded) return question_tensor elif isinstance(question, list): question_tensor = [convert_question_str_to_tensor(q) for q in question] question_tensor = torch.cat(question_tensor, 0) return question_tensor
Example 6
Source File: base_dataset.py From Learning-Monocular-Depth-by-Stereo with MIT License | 6 votes |
def convert_to_tensor(self, sample): tensor_sample = {} for k, v in sample.items(): if v is None: continue elif isinstance(v, str): tensor_sample[k] = v elif isinstance(v, np.ndarray): if len(v.shape) == 3: tensor_sample[k] = torch.from_numpy(np.transpose(v, [2, 0, 1])) else: tensor_sample[k] = torch.from_numpy(v.copy()[np.newaxis, :, :]) elif isinstance(v, (float, int)): tensor_sample[k] = v else: raise NotImplemented return tensor_sample
Example 7
Source File: expert_utils.py From ASR with Apache License 2.0 | 6 votes |
def ConvertGradientToTensor(x): """Identity operation whose gradient is converted to a `Tensor`. Currently, the gradient to `tf.concat` is particularly expensive to compute if dy is an `IndexedSlices` (a lack of GPU implementation forces the gradient operation onto CPU). This situation occurs when the output of the `tf.concat` is eventually passed to `tf.gather`. It is sometimes faster to convert the gradient to a `Tensor`, so as to get the cheaper gradient for `tf.concat`. To do this, replace `tf.concat(x)` with `ConvertGradientToTensor(tf.concat(x))`. Args: x: A `Tensor`. Returns: The input `Tensor`. """ return x
Example 8
Source File: nnef_to_tf.py From NNEF-Tools with Apache License 2.0 | 6 votes |
def convert_tensor(self, source_tensor, target_graph): # type: (NNEFTensor, TFGraph)->TFTensor quantization = None dtype = self.tf_dtype(source_tensor.dtype) if source_tensor.quantization: assert source_tensor.quantization.name == 'tflite_quantize' quantization = TFTensor.Quantization(min=source_tensor.quantization.attribs['min'], max=source_tensor.quantization.attribs['max'], scale=source_tensor.quantization.attribs['scale'], zero_point=source_tensor.quantization.attribs['zero_point']) dtype = "uint8" if source_tensor.quantization.attribs['bits'] == 8 else "int32" return TFTensor(graph=target_graph, label=source_tensor.label, # can be None shape=list(source_tensor.shape), dtype=dtype, data=copy.copy(source_tensor.data), quantization=quantization)
Example 9
Source File: tensor_info.py From hub with Apache License 2.0 | 6 votes |
def convert_dict_to_compatible_tensor(values, targets): """Converts dict `values` in tensors that are compatible with `targets`. Args: values: A dict to objects to convert with same keys as `targets`. targets: A dict returned by `parse_tensor_info_map`. Returns: A map with the same keys as `values` but values converted into Tensor/SparseTensors that can be fed into `protomap`. Raises: TypeError: If it fails to convert. """ result = {} for key, value in sorted(values.items()): result[key] = _convert_to_compatible_tensor( value, targets[key], error_prefix="Can't convert %r" % key) return result
Example 10
Source File: ops.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def convert_to_tensor_or_indexed_slices(value, dtype=None, name=None): """Converts the given object to a `Tensor` or an `IndexedSlices`. If `value` is an `IndexedSlices` or `SparseTensor` it is returned unmodified. Otherwise, it is converted to a `Tensor` using `convert_to_tensor()`. Args: value: An `IndexedSlices`, `SparseTensor`, or an object that can be consumed by `convert_to_tensor()`. dtype: (Optional.) The required `DType` of the returned `Tensor` or `IndexedSlices`. name: (Optional.) A name to use if a new `Tensor` is created. Returns: An `Tensor`, `IndexedSlices`, or `SparseTensor` based on `value`. Raises: ValueError: If `dtype` does not match the element type of `value`. """ return internal_convert_to_tensor_or_indexed_slices( value=value, dtype=dtype, name=name, as_ref=False)
Example 11
Source File: audio_util.py From amortized-variational-filtering with MIT License | 6 votes |
def convert_tensor(audio_tensor): """ Converts a a tensor of audio samples into an audio file. Args: audio_tensor (PyTorch tensor / Variable or numpy array): tensor of samples """ if type(audio_tensor) == torch.autograd.variable.Variable: audio_tensor = audio_tensor.data if type(audio_tensor) in [torch.cuda.FloatTensor, torch.cuda.IntTensor]: audio_tensor = audio_tensor.cpu() if type(audio_tensor) in [torch.FloatTensor, torch.IntTensor, torch.DoubleTensor]: audio_tensor = audio_tensor.numpy() assert type(audio_tensor) == np.ndarray, 'Unknown audio tensor data type.' assert len(audio_tensor.shape) == 1, 'Audio sample must have a single dimension.' return audio_tensor
Example 12
Source File: torch_module.py From video_analyst with MIT License | 6 votes |
def convert_numpy_to_tensor(raw_data): r""" convert numpy array dict or list to torch.Tensor """ elem_type = type(raw_data) if (elem_type.__module__ == "numpy" and elem_type.__name__ != "str_" and elem_type.__name__ != "string_"): return torch.from_numpy(raw_data).float() elif isinstance(raw_data, collections.abc.Mapping): data = {key: convert_numpy_to_tensor(raw_data[key]) for key in raw_data} if 'image' in data: data['image'] = data['image'].permute(2, 0, 1) return data elif isinstance(raw_data, collections.abc.Sequence): return [convert_numpy_to_tensor(data) for data in raw_data] else: return raw_data
Example 13
Source File: datasets.py From RFHO with MIT License | 5 votes |
def convert_sparse_matrix_to_sparse_tensor(X): if isinstance(X, sc_sp.csr.csr_matrix): coo = X.tocoo() indices = np.mat([coo.row, coo.col]).transpose() else: coo, indices = X, [X.row, X.col] # data = np.array(coo.data, dtype=) return tf.SparseTensor(indices, tf.constant(coo.data, dtype=tf.float32), coo.shape)
Example 14
Source File: torch_ops.py From ray with Apache License 2.0 | 5 votes |
def convert_to_torch_tensor(stats, device=None): """Converts any struct to torch.Tensors. stats (any): Any (possibly nested) struct, the values in which will be converted and returned as a new struct with all leaves converted to torch tensors. Returns: Any: A new struct with the same structure as `stats`, but with all values converted to torch Tensor types. """ def mapping(item): # Already torch tensor -> make sure it's on right device. if torch.is_tensor(item): return item if device is None else item.to(device) # Special handling of "Repeated" values. elif isinstance(item, RepeatedValues): return RepeatedValues( tree.map_structure(mapping, item.values), item.lengths, item.max_len) tensor = torch.from_numpy(np.asarray(item)) # Floatify all float64 tensors. if tensor.dtype == torch.double: tensor = tensor.float() return tensor if device is None else tensor.to(device) return tree.map_structure(mapping, stats)
Example 15
Source File: datasets.py From RFHO with MIT License | 5 votes |
def convert_to_tensor(self, keep_sparse=True): matrices = ['_data', '_target'] for att in matrices: if keep_sparse and isinstance(self.__getattribute__(att), SPARSE_SCIPY_MATRICES): self.__setattr__(att, convert_sparse_matrix_to_sparse_tensor(self.__getattribute__(att))) else: self.__setattr__(att, tf.convert_to_tensor(self.__getattribute__(att), dtype=tf.float32)) self._tensor_mode = True
Example 16
Source File: image_tools.py From DFace with Apache License 2.0 | 5 votes |
def convert_image_to_tensor(image): """convert an image to pytorch tensor Parameters: ---------- image: numpy array , h * w * c Returns: ------- image_tensor: pytorch.FloatTensor, c * h * w """ image = image.astype(np.float) return transform(image) # return transform(image)
Example 17
Source File: data_pipeline.py From pregel with MIT License | 5 votes |
def convert_sparse_matrix_to_sparse_tensor(X): ''' code borrowed from https://stackoverflow.com/questions/40896157/scipy-sparse-csr-matrix-to-tensorflow-sparsetensor-mini-batch-gradient-descent ''' coo = X.tocoo() indices = np.mat([coo.row, coo.col]).transpose() return tf.SparseTensorValue(indices, coo.data, coo.shape)
Example 18
Source File: video2feature.py From TA3N with MIT License | 5 votes |
def convert_c3d_tensor_batch(batch_tensor): # e.g. 30x3x112x112 --> 15x3x16x112x112 batch_tensor_c3d = torch.Tensor() for b in range(batch_tensor.size(0)-c3d_clip_size+1): tensor_c3d = batch_tensor[b:b+c3d_clip_size,:,:,:] tensor_c3d = torch.transpose(tensor_c3d,0,1).unsqueeze(0) batch_tensor_c3d = torch.cat((batch_tensor_c3d, tensor_c3d)) batch_tensor_c3d = batch_tensor_c3d*255 return batch_tensor_c3d
Example 19
Source File: span_util.py From semanticRetrievalMRS with MIT License | 5 votes |
def convert_input_weight_list_to_tensor(weight_list: List, batch_span: List[ParagraphSpan], device: torch.device) \ -> Tuple[torch.Tensor, torch.Tensor]: assert len(weight_list) == len(batch_span) weight_tensors_list = [] weight_lengths_list = [] for i, weights in enumerate(weight_list): cur_span = batch_span[i] cur_weights = weights[:cur_span.number_of_span()] weight_tensors_list.append(torch.from_numpy(np.asarray(cur_weights, dtype=np.float32)).to(device)) weight_lengths_list.append(cur_span.number_of_span()) output_weight = torch_util.pack_list_sequence(weight_tensors_list, weight_lengths_list) output_l = torch.from_numpy(np.asarray(weight_lengths_list, dtype=np.int64)).to(device) return output_weight, output_l
Example 20
Source File: numpy_backend.py From TensorNetwork with Apache License 2.0 | 5 votes |
def convert_to_tensor(self, tensor: Tensor) -> Tensor: if (not isinstance(tensor, np.ndarray) and not np.isscalar(tensor)): raise TypeError("Expected a `np.array` or scalar. Got {}".format( type(tensor))) result = np.asarray(tensor) return result
Example 21
Source File: decoder.py From pytorch-nlp with MIT License | 5 votes |
def convert_tensor(self, offsets, sizes): results = [] for b, batch in enumerate(offsets): utterances = [] for p, utt in enumerate(batch): size = sizes[b][p] if sizes[b][p] > 0: utterances.append(utt[0:size]) else: utterances.append(torch.IntTensor()) results.append(utterances) return results
Example 22
Source File: helper.py From transferable_sent2vec with MIT License | 5 votes |
def convert_sent_to_tensor(sentence, max_length, pad_token): word_list = [] for i in range(max_length): word = sentence[i] if i < len(sentence) else pad_token word_list.append(ELMoCharacterMapper.convert_word_to_char_ids(word)) return word_list
Example 23
Source File: utils.py From ignite with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert_tensor( input_: Union[torch.Tensor, collections.Sequence, collections.Mapping, str, bytes], device: Optional[Union[str, torch.device]] = None, non_blocking: bool = False, ) -> Union[torch.Tensor, collections.Sequence, collections.Mapping, str, bytes]: """Move tensors to relevant device.""" def _func(tensor: torch.Tensor) -> torch.Tensor: return tensor.to(device=device, non_blocking=non_blocking) if device is not None else tensor return apply_to_tensor(input_, _func)
Example 24
Source File: ops.py From dragon with BSD 2-Clause "Simplified" License | 5 votes |
def convert_to_tensor( value, dtype=None, name=None, preferred_dtype=None, ): """Converts the given value to a Tensor. Parameters ---------- value : number, sequence or numpy.ndarray The value to convert. dtype : dragon.vm.tensorflow.dtypes.DType, optional The optional data type. name : str, optional The Optional name. preferred_dtype : dragon.vm.tensorflow.dtypes.DType, optional The optional type when ``dtype`` is *None*. Returns ------- dragon.Tensor The output tensor. """ if types.is_tensor(value): return value return constant_op.constant(value, dtype=dtype, name=name)
Example 25
Source File: embeddings_to_torch.py From OpenNMT-py with MIT License | 5 votes |
def convert_to_torch_tensor(word_to_float_list_dict, vocab): dim = len(six.next(six.itervalues(word_to_float_list_dict))) tensor = torch.zeros((len(vocab), dim)) for word, values in word_to_float_list_dict.items(): tensor[vocab.stoi[word]] = torch.Tensor(values) return tensor
Example 26
Source File: convert.py From BERT-pytorch with The Unlicense | 5 votes |
def convert_to_tensor(data, device): if type(data) == tuple: return tuple(torch.tensor(d, device=device) for d in data) else: return torch.tensor(data, device=device)
Example 27
Source File: constant_op.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def convert_to_eager_tensor(value, ctx, dtype=None): """Converts the given `value` to an `EagerTensor`. Note that this function could return cached copies of created constants for performance reasons. Args: value: value to convert to EagerTensor. ctx: value of context.context(). dtype: optional desired dtype of the converted EagerTensor. Returns: EagerTensor created from value. Raises: TypeError: if `dtype` is not compatible with the type of t. """ if isinstance(value, ops.EagerTensor): if dtype is not None and value.dtype != dtype: raise TypeError("Expected tensor with type %r not %r" % ( dtype, value.dtype)) return value if dtype is not None: dtype = dtype.as_datatype_enum device = ctx.device_name handle = ctx._handle # pylint: disable=protected-access if isinstance(value, (int, float)): # Use a scalar cache. This will put each scalar of each type only once on # each device. Scalars don't use much device memory but copying scalars can # trigger memcpys which are slow. cache_key = device, value, dtype, type(value) scalar_cache = ctx.scalar_cache() tensor = scalar_cache.get(cache_key, None) if tensor is not None: return tensor t = ops.EagerTensor(value, context=handle, device=device, dtype=dtype) scalar_cache[cache_key] = t return t else: return ops.EagerTensor(value, context=handle, device=device, dtype=dtype)
Example 28
Source File: sparse_tensor.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def convert_to_tensor_or_sparse_tensor(value, dtype=None, name=None): """Converts value to a `SparseTensor` or `Tensor`. Args: value: A `SparseTensor`, `SparseTensorValue`, or an object whose type has a registered `Tensor` conversion function. dtype: Optional element type for the returned tensor. If missing, the type is inferred from the type of `value`. name: Optional name to use if a new `Tensor` is created. Returns: A `SparseTensor` or `Tensor` based on `value`. Raises: RuntimeError: If result type is incompatible with `dtype`. """ if dtype is not None: dtype = dtypes.as_dtype(dtype) if isinstance(value, SparseTensorValue): value = SparseTensor.from_value(value) if isinstance(value, SparseTensor): if dtype and not dtype.is_compatible_with(value.dtype): raise RuntimeError( "Sparse dtype: requested = %s, actual = %s" % ( dtype.name, value.dtype.name)) return value return ops.internal_convert_to_tensor( value, dtype=dtype, name=name)
Example 29
Source File: _utils.py From NeuralSceneDecomposition with GNU General Public License v3.0 | 5 votes |
def convert_tensor(input_, device=None): if torch.is_tensor(input_): if device: input_ = input_.to(device=device) return input_ elif isinstance(input_, string_classes): return input_ elif isinstance(input_, collections.Mapping): return {k: convert_tensor(sample, device=device) for k, sample in input_.items()} elif isinstance(input_, collections.Sequence): return [convert_tensor(sample, device=device) for sample in input_] else: raise TypeError(("input must contain tensors, dicts or lists; found {}" .format(type(input_))))
Example 30
Source File: float16.py From onnxconverter-common with MIT License | 5 votes |
def convert_tensor_float_to_float16(tensor): ''' Convert tensor float to float16. :param tensor: TensorProto object :return tensor_float16: converted TensorProto object Example: :: from onnxmltools.utils.float16_converter import convert_tensor_float_to_float16 new_tensor = convert_tensor_float_to_float16(tensor) ''' if not isinstance(tensor, onnx_proto.TensorProto): raise ValueError('Expected input type is an ONNX TensorProto but got %s' % type(tensor)) if tensor.data_type == onnx_proto.TensorProto.FLOAT: tensor.data_type = onnx_proto.TensorProto.FLOAT16 # convert float_data (float type) to float16 and write to int32_data if tensor.float_data: int_list = _npfloat16_to_int(np.float16(tensor.float_data)) tensor.int32_data[:] = int_list tensor.float_data[:] = [] # convert raw_data (bytes type) if tensor.raw_data: # convert n.raw_data to float float32_list = np.fromstring(tensor.raw_data, dtype='float32') # convert float to float16 float16_list = np.float16(float32_list) # convert float16 to bytes and write back to raw_data tensor.raw_data = float16_list.tostring() return tensor
Example 31
Source File: torch_module.py From video_analyst with MIT License | 5 votes |
def convert_tensor_to_numpy(raw_data): r""" convert numpy array dict or list to torch.Tensor """ if isinstance(raw_data, torch.Tensor): return raw_data.cpu().numpy() elif isinstance(raw_data, collections.abc.Mapping): data = {key: convert_tensor_to_numpy(raw_data[key]) for key in raw_data} if 'image' in data: data['image'] = data['image'].transpose(1, 2, 0).astype(np.uint8) return data elif isinstance(raw_data, collections.abc.Sequence): return [convert_tensor_to_numpy(data) for data in raw_data]
Example 32
Source File: core.py From keras-lambda with MIT License | 5 votes |
def convert_to_labeled_tensor(value, dtype=None, name=None): """Converts the given `value` to a `LabeledTensor`. This function accepts `LabeledTensor` objects, 0-dimensional `Tensor` objects and numpy arrays, and Python scalars. Higher dimensional unlabeled tensors must use the `LabeledTensor` constructor explicitly. Args: value: Object to convert. dtype: Optional element type for the returned tensor. If missing, the type is inferred from the type of value. name: Optional name to use if a new Tensor is created. Returns: `value` converted into a `LabeledTensor` object. Raises: ValueError: If the output would have rank>0 but the input was not already a `LabeledTensor`. """ # TODO(shoyer): consider extending to accept xarray.DataArray as input. if isinstance(value, LabeledTensor): axes = value.axes.values() value = value.tensor else: axes = [] # We call convert_to_tensor even for LabeledTensor input because it also # checks to make sure the dtype argument is compatible. tensor = ops.convert_to_tensor(value, dtype=dtype, name=name) if len(tensor.get_shape()) != len(axes): raise ValueError('cannot automatically convert unlabeled arrays or tensors ' 'with rank>0 into LabeledTensors: %r' % value) return LabeledTensor(tensor, axes)
Example 33
Source File: arrays.py From trax with Apache License 2.0 | 5 votes |
def convert_to_tensor(value, dtype=None): # A safer version of `tf.convert_to_tensor` to work around b/149876037. # TODO(wangpeng): Remove this function once the bug is fixed. if (dtype is None and isinstance(value, six.integer_types) and value >= 2 ** 63): dtype = tf.uint64 elif (dtype is None and isinstance(value, float)): dtype = dtypes.default_float_type() return tf.convert_to_tensor(value, dtype=dtype)
Example 34
Source File: model.py From lm-human-preferences with MIT License | 5 votes |
def convert_gradient_to_tensor(x): """Force gradient to be a dense tensor. It's often faster to do dense embedding gradient on GPU than sparse on CPU. """ return x
Example 35
Source File: core.py From mars with Apache License 2.0 | 5 votes |
def convert_to_tensor_or_dataframe(item): if isinstance(item, (DATAFRAME_TYPE, pd.DataFrame)): item = DataFrame(item) elif isinstance(item, (SERIES_TYPE, pd.Series)): item = Series(item) else: item = astensor(item) return item
Example 36
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 37
Source File: helpers.py From tensorflow_constrained_optimization with Apache License 2.0 | 5 votes |
def convert_to_1d_tensor(tensor, name=None): """Converts the given object to a rank-one `Tensor`. This function checks that the object is trivially convertible to rank-1, i.e. has only one "nontrivial" dimension (e.g. the shapes [1000] and [1, 1, None, 1] are allowed, but [None, 1, None] and [50, 10] are not). If it satisfies this condition, then it is returned as a rank-1 `Tensor`. Args: tensor: an object convertible to a `Tensor` (e.g. a scalar, list, numpy array, or a `Tensor` itself). name: str, how to refer to the tensor in error messages. Returns: A rank-1 `Tensor`. Raises: ValueError: if the resulting tensor is not rank-1. """ tensor = tf.convert_to_tensor(tensor, name=name) name = name or "tensor" dims = tensor.shape.dims if dims is None: raise ValueError("%s must have a known rank" % name) # If the Tensor is already rank-1, then return it without wrapping a # tf.reshape() around it. if len(dims) == 1: return tensor if sum(dim.value != 1 for dim in dims) > 1: raise ValueError("all but one dimension of %s must have size one" % name) # Reshape to a rank-1 Tensor. return tf.reshape(tensor, (-1,))
Example 38
Source File: decoder.py From LipReading with MIT License | 5 votes |
def convert_tensor(self, offsets, sizes): results = [] for b, batch in enumerate(offsets): utterances = [] for p, utt in enumerate(batch): size = sizes[b][p] if sizes[b][p] > 0: utterances.append(utt[0:size]) else: utterances.append(torch.tensor([], dtype=torch.int)) results.append(utterances) return results
Example 39
Source File: distributed_util.py From ClassyVision with MIT License | 5 votes |
def convert_to_distributed_tensor(tensor: torch.Tensor) -> Tuple[torch.Tensor, str]: """ For some backends, such as NCCL, communication only works if the tensor is on the GPU. This helper function converts to the correct device and returns the tensor + original device. """ orig_device = "cpu" if not tensor.is_cuda else "gpu" if ( torch.distributed.is_available() and torch.distributed.get_backend() == torch.distributed.Backend.NCCL and not tensor.is_cuda ): tensor = tensor.cuda() return (tensor, orig_device)
Example 40
Source File: distributed_util.py From ClassyVision with MIT License | 5 votes |
def convert_to_normal_tensor(tensor: torch.Tensor, orig_device: str) -> torch.Tensor: """ For some backends, such as NCCL, communication only works if the tensor is on the GPU. This converts the tensor back to original device. """ if tensor.is_cuda and orig_device == "cpu": tensor = tensor.cpu() return tensor
Example 41
Source File: checkpoint.py From translate with BSD 3-Clause "New" or "Revised" License | 5 votes |
def convert_tensor(tensor: torch.Tensor, clone: bool) -> torch.Tensor: tensor = tensor.detach().cpu() if isinstance(tensor, torch.HalfTensor): # We convert any fp16 params to fp32 to make sure operations like # division by a scalar value are supported. tensor = tensor.float() elif clone: # tensor.float() would have effectively cloned the fp16 tensor already, # so we don't need to do it again even if clone=True. tensor = tensor.clone() return tensor
Example 42
Source File: extensions.py From trax with Apache License 2.0 | 5 votes |
def convert_sharded_tensor_to_eager_tensor(value, *args, **kwargs): del args, kwargs # TODO(nareshmodi): Consider a collective op to gather the tensors from the # various devices for performance reasons. return tf.stack(value.tensors)
Example 43
Source File: net_run.py From PyMIC with Apache License 2.0 | 5 votes |
def convert_tensor_type(self, input_tensor): if(self.tensor_type == 'float'): return input_tensor.float() else: return input_tensor.double()
Example 44
Source File: utils.py From finetune-transformer-lm with MIT License | 5 votes |
def convert_gradient_to_tensor(x): """force gradient to be a dense tensor it's often faster to do dense embedding gradient on GPU than sparse on CPU """ return x
Example 45
Source File: tensor_util.py From deep_image_model with Apache License 2.0 | 5 votes |
def convert_to_tensor_or_sparse_tensor( value, dtype=None, name=None, as_ref=False): """Converts value to a `SparseTensor` or `Tensor`. Args: value: A `SparseTensor`, `SparseTensorValue`, or an object whose type has a registered `Tensor` conversion function. dtype: Optional element type for the returned tensor. If missing, the type is inferred from the type of `value`. name: Optional name to use if a new `Tensor` is created. as_ref: True if we want the result as a ref tensor. Only used if a new `Tensor` is created. Returns: A `SparseTensor` or `Tensor` based on `value`. Raises: RuntimeError: If result type is incompatible with `dtype`. """ if dtype is not None: dtype = dtypes.as_dtype(dtype) if isinstance(value, sparse_tensor.SparseTensorValue): value = sparse_tensor.SparseTensor.from_value(value) if isinstance(value, sparse_tensor.SparseTensor): if dtype and not dtype.is_compatible_with(value.dtype): raise RuntimeError( 'Sparse dtype: requested = %s, actual = %s' % ( dtype.name, value.dtype.name)) return value return ops.convert_to_tensor(value, dtype=dtype, name=name, as_ref=as_ref)