3 examples of 'convert list to tensor' in Python

Every line of 'convert list to tensor' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
140def __to_ndarray_list(tensors, titles):
141 if not isinstance(tensors, list):
142 tensors = [tensors]
143 titles = [titles]
144 assert len(titles) == len(tensors),\
145 "[visualizer]: {} titles are not enough for {} tensors".format(
146 len(titles), len(tensors))
147 for i in range(len(tensors)):
148 if torch.is_tensor(tensors[i]):
149 tensors[i] = tensors[i].cpu().detach().numpy()
150 return tensors, titles
111def _to_tensors(self, ts):
112 x = []
113 y = []
114 for sample in ts:
115 x.append(sample['word'].squeeze())
116 y.append(sample['y'].squeeze())
117 return np.stack(x), np.stack(y)
396def tensor_to_ndarray(tensor):
397 """
398 Convert float tensor into numpy image
399
400 :param tensor: input tensor
401 :type tensor: torch.Tensor
402 :return: numpy image
403 :rtype: np.ndarray
404 """
405 tensor_np = tensor.permute(1, 2, 0).cpu().numpy()
406 tensor_np = tensor_np.astype(np.float32)
407 tensor_np = (tensor_np * 255).astype(np.uint8)
408 return tensor_np

Related snippets