-
Notifications
You must be signed in to change notification settings - Fork 3
/
Util.py
42 lines (33 loc) · 1.12 KB
/
Util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import torch
class Util:
@staticmethod
def convert_to_tensor(X, Y, device):
"""
Converts the dataset to tensor.
:param X: dataset
:param Y: label
:param device: whether {cpu or gpu}
:return: the dataset as tensor
"""
tensor_x = torch.stack([torch.Tensor(i) for i in X])
tensor_y = torch.from_numpy(Y)
processed_dataset = torch.utils.data.TensorDataset(tensor_x, tensor_y)
return processed_dataset
@staticmethod
def convert_to_tensor_test(X):
"""
Converts the dataset to tensor.
:param X: dataset
:param Y: label
:param device: whether {cpu or gpu}
:return: the dataset as tensor
"""
tensor_x = torch.stack([torch.Tensor(i) for i in X])
processed_dataset = torch.utils.data.TensorDataset(tensor_x)
return processed_dataset
@staticmethod
def get_device():
return torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@staticmethod
def get_num_correct(preds, labels):
return preds.argmax(dim=1).eq(labels).sum().item()