Week 11 Notes
Dataset
x_train = torch.tensor(x_train/255, dtype=torch.float32)
x_test = torch.tensor(x_test/255, dtype=torch.float32)
:y_train = torch.tensor(y_train, dtype=torch.long)
y_test = torch.tensor(y_test, dtype=torch.int64)
- Divide by 255 to change the color values to 0 to 1
- X_train.shape -> [60000, 28, 28] y_train.shape -> [60000]
- y_train.unsqueeze(-1) -> [60000, 1]
class TestDataset(Dataset):
def __init__(self, data):
self.data_x = data[0]
self.data_y = data[1]
def __len__(self):
return self.data_x.size(0)
def __getitem__(self, index):
return self.data_x[index], self.data_y[index]
- When defining a class which is an inheritance from other class, we need to atleast have
this three functions inside the class
- How to initialize - ds = TestDataset((x_test, y_test)
-
class MyDataset(Dataset):
def __init__(self, data):
self.data = torch.cat(data, dim=1)
def __len__(self):
return self.data.size(2)
def __getitem__(self, index):
return self.data[:, :, index].unsqueeze(1)
lm = moneyness(spot, 1.1)
t = time_to_maturity(spot, 0.004)
v = volatility(spot, 0.2)
ds = MyDataset([lm, t, v])
- Unsqueeze in getitem to return back all the three features of all in the dataset.
- Dataset would be in the size of [2,1,5]
Compute Hedge
def compute_hedge(model, ds):
outputs = []
for i in ds:
outputs.append(model(i))
return torch.cat(outputs, dim=-1)
- Input each of the tuple in the dataset into the model to compute hedge
- Concat each of the results together,
- The final shape would be [2,1,5] <- for each of the item, like it is [2,1,1] - one val, then
total will be of 10 vals
Compute Portfolio
def compute_portfolio(model, ds, payoff):
unit = compute_hedge(model, ds)
return pl(spot, unit)
def compute_portfolio_2(model, ds, payoff):
unit = compute_hedge(model, ds)
return pl(spot, unit, payoff=payoff)
Optimizer and Training
optimizer = torch.optim.Adam(m.parameters())
- We use adam optimizer
for i in range(10):
optimizer.zero_grad()
cash = compute_portfolio_2(m, ds, european_payoff(spot))
loss = torch.mean(cash*cash)
loss.backward()
optimizer.step()
print(loss)
- Loss.backward is used to calculate the gradients for each step
- Optimizer.zero_grad -> used to reset the grad to 0 back
GPU
torch.cuda.is_available():
- Checking if the gpu is available to be used
# Specify the device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Example: Creating a tensor and moving it to GPU
tensor = torch.randn(3, 3).to(device)
# You can also use tensor.cuda() as a shorthand
tensor = torch.randn(3, 3).cuda() if torch.cuda.is_available() else
torch.randn(3, 3)
- Using to to move the tensor to gpu
model.to(device)
# Example for CUDA shorthand
model.cuda() if torch.cuda.is_available() else model.cpu()
- Moving the model to gpu
inputs, labels = inputs.to(device), labels.to(device)
- All of these need to move to the GPU
To check if our data has been moved to GPU or not, has device=’cuda:0’ at the end of the dataset
- When using GPU, we need to transfer all of the model, data and each of the values to
gpu first
- First, we transfer the model to GPU
m = MLP(30.to(device)
For the dataset, thegetitem is added with to device to make sure thr dataset is transferred to
GPU
class TestDataset(Dataset):
def __init__(self, data):
self.data = torch.cat(data, dim=1)
def __len__(self):
return self.data.size(2)
def __getitem__(self, index):
return self.data[:, :, index].unsqueeze(1).to(device)
Compute Portfolio
def compute_portfolio(..):
Unit = compute_hedge(model, ds)
Return pl(spot.to(device), unit)
For compute portfolio 2, use the same but also need to transfer payoff to GPU (
payoff.to(device))
In conclusion, EVERYTHING NEW that we have made NEED TO BE TRANSFERRED TO
GPU FIRST THEN CALCULATED!