-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoencoder.py
More file actions
133 lines (104 loc) · 4.3 KB
/
autoencoder.py
File metadata and controls
133 lines (104 loc) · 4.3 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import torch
from torch import nn
import torch.utils.data as D
import torch.nn.functional as F
import h5py
class Encoder(nn.Module):
def __init__(self, input_shape, reduced_shape, device, dtype):
super().__init__()
self._linear_stack = nn.Sequential(
nn.Linear(input_shape, reduced_shape),
nn.ReLU(),
nn.Linear(reduced_shape, reduced_shape),
nn.ReLU(),
).to(device=device, dtype=dtype)
def forward(self, x):
return self._linear_stack(x)
class Decoder(nn.Module):
def __init__(self, input_shape, reduced_shape, device, dtype):
super().__init__()
self._linear_stack = nn.Sequential(
nn.Linear(reduced_shape, reduced_shape),
nn.ReLU(),
nn.Linear(reduced_shape, input_shape),
nn.ReLU(),
).to(device=device, dtype=dtype)
def forward(self, x):
return self._linear_stack(x)
class AutoEncoder(nn.Module):
def __init__(self, input_shape, device="cuda:0", dtype=torch.double, reduction=0.50):
super().__init__()
self._reduced_dim = int(input_shape * reduction)
self._latent_weights = nn.Linear(self._reduced_dim,
self._reduced_dim)\
.to(device=device, dtype=dtype)
self.encoder = Encoder(input_shape, self._reduced_dim, device, dtype)
self.decoder = Decoder(input_shape, self._reduced_dim, device, dtype)
def forward(self, x):
encode = self.encoder(x)
latent = self._latent_weights(encode)
decode = self.decoder(latent)
return F.log_softmax(decode, dim=1)
def fit(self,
loader: D.DataLoader,
optimizer,
scheduler,
criterion,
epochs=100):
history = []
num_batches = loader.dataset.shape[0] // loader.batch_size
for epoch in range(epochs):
avg_loss = 0
for bi, batch in enumerate(loader):
# Forward
loss = criterion(
self.forward(batch),
F.log_softmax(batch, dim=1)
)
avg_loss += loss.item()
# Make output
num_bars = int(((bi / num_batches) * 20)) + 1
completion_string = "="*num_bars
completion_string += "-"*(20 - num_bars)
print(
"Epoch: {} \t {} \t Train loss: {} Lr: {}"\
.format(epoch,
completion_string,
loss,
scheduler.get_last_lr()),
end="\r"
)
# Backpropogate
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Adjust LR
scheduler.step()
# Record
avg_loss /= num_batches
history.append(avg_loss)
return history
@torch.inference_mode()
def encode(self, X):
encode = self.encoder(X)
latent = self._latent_weights(encode)
return latent
if __name__ == "__main__":
with h5py.File("C:Users\kylei\hetrec_gpt3_embed.h5", "r") as f:
all_embed = f["Embedding"][:]
movie_ids = f["MovieID"][:]
titles = f["MovieTitle"][:]
tags = f["Tags"][:]
genres = f["Genres"][:]
ae = AutoEncoder(all_embed.shape[1], reduction=0.50, dtype=torch.bfloat16)
rand_gen = torch.Generator().manual_seed(int(b"101101010"))
embed_tensor = torch.tensor(all_embed, device="cuda:0", dtype=torch.bfloat16)
loader = D.DataLoader(embed_tensor,
generator=rand_gen,
batch_size=64,
shuffle=True,
)
optimizer = torch.optim.RMSprop(ae.parameters(), lr=1)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=15, gamma=0.1)
criterion = torch.nn.KLDivLoss(log_target=True, reduction="batchmean")
hist = ae.fit(loader, optimizer, scheduler, criterion, epochs=50)