-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutils.py
56 lines (44 loc) · 1.44 KB
/
utils.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
43
44
45
46
47
48
49
50
51
52
53
54
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from torch.utils.data import DataLoader, Dataset
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
use_cuda = torch.cuda.is_available()
class TorchDataset(Dataset):
def __init__(self, x, y):
self.x = x
self.y = y
def __getitem__(self, idx):
return self.x[idx], self.y[idx]
def __len__(self):
return len(self.x)
class TorchDataLoader:
def __init__(self,batch_size,shuffle = True):
self.batch_size = batch_size
self.shuffle = shuffle
def torch_dataloader(self,train_data,target_data):
torch_dataset = TorchDataset(train_data,target_data)
torch_loader = DataLoader(dataset = torch_dataset,
batch_size = self.batch_size,
shuffle = self.shuffle)
return torch_loader
def plot_results(predicted_data, true_data):
# use in train.py
# plot evaluate result
fig = plt.figure(facecolor='white')
ax = fig.add_subplot(111)
ax.plot(true_data, label='True Data')
plt.plot(predicted_data, label='Prediction')
plt.legend()
plt.show()
def ToVariable(x):
# use in train.py
# change from numpy.array to torch.variable
tmp = torch.DoubleTensor(x)
if use_cuda:
return Variable(tmp).cuda()
else:
return Variable(tmp)