Convert values in numpy.ndarray type to torch.Tensor, and move Tensors to the target device. All keys will exist in the returned dict. Args: device (Union[torch.device, str], optional): A specified device. Defaults to CPU_DEVICE. dtype (torch.
(self,
device: Optional[Union[torch.device, str]] = _CPU_DEVICE,
dtype: Optional[torch.dtype] = None,
non_blocking: Optional[bool] = False,
copy: Optional[bool] = False,
memory_format: Optional[torch.memory_format] = None)
| 381 | return writer_args_dict, sliced_data |
| 382 | |
| 383 | def to(self, |
| 384 | device: Optional[Union[torch.device, str]] = _CPU_DEVICE, |
| 385 | dtype: Optional[torch.dtype] = None, |
| 386 | non_blocking: Optional[bool] = False, |
| 387 | copy: Optional[bool] = False, |
| 388 | memory_format: Optional[torch.memory_format] = None) -> dict: |
| 389 | """Convert values in numpy.ndarray type to torch.Tensor, and move |
| 390 | Tensors to the target device. All keys will exist in the returned dict. |
| 391 | |
| 392 | Args: |
| 393 | device (Union[torch.device, str], optional): |
| 394 | A specified device. Defaults to CPU_DEVICE. |
| 395 | dtype (torch.dtype, optional): |
| 396 | The data type of the expected torch.Tensor. |
| 397 | If dtype is None, it is decided according to numpy.ndarry. |
| 398 | Defaults to None. |
| 399 | non_blocking (bool, optional): |
| 400 | When non_blocking, tries to convert asynchronously with |
| 401 | respect to the host if possible, e.g., |
| 402 | converting a CPU Tensor with pinned memory to a CUDA Tensor. |
| 403 | Defaults to False. |
| 404 | copy (bool, optional): |
| 405 | When copy is set, a new Tensor is created even when |
| 406 | the Tensor already matches the desired conversion. |
| 407 | No matter what value copy is, Tensor constructed from numpy |
| 408 | will not share the same memory with the source numpy.ndarray. |
| 409 | Defaults to False. |
| 410 | memory_format (torch.memory_format, optional): |
| 411 | The desired memory format of returned Tensor. |
| 412 | Not supported by pytorch-cpu. |
| 413 | Defaults to None. |
| 414 | |
| 415 | Returns: |
| 416 | dict: |
| 417 | A dict with all numpy.ndarray values converted into |
| 418 | torch.Tensor and all Tensors moved to the target device. |
| 419 | """ |
| 420 | ret_dict = {} |
| 421 | for key in self.keys(): |
| 422 | raw_value = self.get_raw_value(key) |
| 423 | tensor_value = None |
| 424 | if isinstance(raw_value, np.ndarray): |
| 425 | tensor_value = torch.from_numpy(raw_value).clone() |
| 426 | elif isinstance(raw_value, torch.Tensor): |
| 427 | tensor_value = raw_value |
| 428 | if tensor_value is None: |
| 429 | ret_dict[key] = raw_value |
| 430 | else: |
| 431 | if memory_format is None: |
| 432 | ret_dict[key] = \ |
| 433 | tensor_value.to(device, dtype, |
| 434 | non_blocking, copy) |
| 435 | else: |
| 436 | ret_dict[key] = \ |
| 437 | tensor_value.to(device, dtype, |
| 438 | non_blocking, copy, |
| 439 | memory_format=memory_format) |
| 440 | return ret_dict |
no test coverage detected