pytorch - data convertor - create tensor directly on device, simplify code
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import torch
|
import torch
|
||||||
@@ -12,14 +11,14 @@ class PyTorchDataConvertor(ABC):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def convert_x(self, df: pd.DataFrame, device: Optional[str] = None) -> torch.Tensor:
|
def convert_x(self, df: pd.DataFrame, device: str) -> torch.Tensor:
|
||||||
"""
|
"""
|
||||||
:param df: "*_features" dataframe.
|
:param df: "*_features" dataframe.
|
||||||
:param device: The device to use for training (e.g. 'cpu', 'cuda').
|
:param device: The device to use for training (e.g. 'cpu', 'cuda').
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def convert_y(self, df: pd.DataFrame, device: Optional[str] = None) -> torch.Tensor:
|
def convert_y(self, df: pd.DataFrame, device: str) -> torch.Tensor:
|
||||||
"""
|
"""
|
||||||
:param df: "*_labels" dataframe.
|
:param df: "*_labels" dataframe.
|
||||||
:param device: The device to use for training (e.g. 'cpu', 'cuda').
|
:param device: The device to use for training (e.g. 'cpu', 'cuda').
|
||||||
@@ -33,8 +32,8 @@ class DefaultPyTorchDataConvertor(PyTorchDataConvertor):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
target_tensor_type: Optional[torch.dtype] = None,
|
target_tensor_type: torch.dtype = torch.float32,
|
||||||
squeeze_target_tensor: bool = False
|
squeeze_target_tensor: bool = False,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
:param target_tensor_type: type of target tensor, for classification use
|
:param target_tensor_type: type of target tensor, for classification use
|
||||||
@@ -45,23 +44,14 @@ class DefaultPyTorchDataConvertor(PyTorchDataConvertor):
|
|||||||
self._target_tensor_type = target_tensor_type
|
self._target_tensor_type = target_tensor_type
|
||||||
self._squeeze_target_tensor = squeeze_target_tensor
|
self._squeeze_target_tensor = squeeze_target_tensor
|
||||||
|
|
||||||
def convert_x(self, df: pd.DataFrame, device: Optional[str] = None) -> torch.Tensor:
|
def convert_x(self, df: pd.DataFrame, device: str) -> torch.Tensor:
|
||||||
x = torch.from_numpy(df.values).float()
|
numpy_arrays = df.values
|
||||||
if device:
|
x = torch.tensor(numpy_arrays, device=device, dtype=torch.float32)
|
||||||
x = x.to(device)
|
|
||||||
|
|
||||||
return x
|
return x
|
||||||
|
|
||||||
def convert_y(self, df: pd.DataFrame, device: Optional[str] = None) -> torch.Tensor:
|
def convert_y(self, df: pd.DataFrame, device: str) -> torch.Tensor:
|
||||||
y = torch.from_numpy(df.values)
|
numpy_arrays = df.values
|
||||||
|
y = torch.tensor(numpy_arrays, device=device, dtype=self._target_tensor_type)
|
||||||
if self._target_tensor_type:
|
|
||||||
y = y.to(self._target_tensor_type)
|
|
||||||
|
|
||||||
if self._squeeze_target_tensor:
|
if self._squeeze_target_tensor:
|
||||||
y = y.squeeze()
|
y = y.squeeze()
|
||||||
|
|
||||||
if device:
|
|
||||||
y = y.to(device)
|
|
||||||
|
|
||||||
return y
|
return y
|
||||||
|
|||||||
Reference in New Issue
Block a user