-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfm_param_ae.py
180 lines (125 loc) · 4.76 KB
/
fm_param_ae.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#%%
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim.lr_scheduler import StepLR
from pathlib import Path
from torch.utils.data import Subset as DataSubset
from sklearn.model_selection import train_test_split
from dx7_constants import VOICE_PARAMETER_RANGES, ARTIFACTS_ROOT, VOICE_KEYS
import numpy as np
N_PARAMS = len(VOICE_PARAMETER_RANGES)
MAX_VALUE = max([max(i) for i in VOICE_PARAMETER_RANGES.values()]) + 1
#%%
# class DataHandler()
# def __init__(self, data_file, root=ARTIFACTS_ROOT):
# if not isinstance(root, Path):
# root = Path(root).expanduser()
# data = np.load(ARTIFACTS_ROOT.joinpath(patch_file))
class DX7Dataset():
def __init__(self, data_file='dx7.npy', root=ARTIFACTS_ROOT):
if not isinstance(root, Path):
root = Path(root).expanduser()
self.data = np.load(root.joinpath(data_file))
def __getitem__(self, index):
item = torch.tensor(self.data[index].item()).long()
return item
def __len__(self):
return len(self.data)
#%%
class Net(nn.Module):
def __init__(self, latent_dim=16, n_params=N_PARAMS, max_value=MAX_VALUE):
super(Net, self).__init__()
self.n_params = n_params
self.max_value = max_value
self.embedder = nn.Embedding(max_value, 8)
self.enc = nn.Sequential(
nn.Linear(8*n_params, 512),
nn.GELU(),
nn.Dropout(0.4),
nn.Linear(512, latent_dim),
)
self.dec = nn.Sequential(
nn.Linear(latent_dim, 512),
nn.GELU(),
nn.Dropout(0.4),
nn.Linear(512, max_value*n_params),
)
self.register_buffer('mask', self.generate_mask())
@staticmethod
def generate_mask():
mask_item_f = lambda x: torch.arange(MAX_VALUE) <= max(x)
mapper = map(mask_item_f, map(VOICE_PARAMETER_RANGES.get, VOICE_KEYS))
return torch.stack(list(mapper))
def forward(self, x):
x = self.embedder(x)
x = x.flatten(-2, -1)
z = self.enc(x)
x_hat = self.dec(z)
x_hat = x_hat.reshape(-1, self.n_params, self.max_value)
x_hat = torch.masked_fill(x_hat, ~self.mask, -1e9)
return x_hat
#%%
def train(model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data) in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.cross_entropy(output.transpose(-1,-2), data)
loss.backward()
optimizer.step()
if batch_idx % 100 == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data in test_loader:
data = data.to(device)
output = model(data)
test_loss += F.cross_entropy(output.transpose(-1,-2), data, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=-1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(data.view_as(pred)).sum().item() / 155
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
if __name__=="__main__":
# Training settings
use_cuda = False
batch_size = 32
lr = 1
gamma = 0.7
epochs = 100
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
dataset = DX7Dataset()
train_idxs, test_idxs = train_test_split(range(len(dataset)), random_state=42)
train_dataset = DataSubset(dataset, train_idxs)
test_dataset = DataSubset(dataset, test_idxs)
train_loader = torch.utils.data.DataLoader(
train_dataset,
batch_size=batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
test_dataset,
batch_size=batch_size, shuffle=True, **kwargs)
model = Net().to(device)
optimizer = optim.Adadelta(model.parameters(), lr=lr)
scheduler = StepLR(optimizer, step_size=1, gamma=gamma)
for epoch in range(1, epochs + 1):
train(model, device, train_loader, optimizer, epoch)
test(model, device, test_loader)
scheduler.step()
# if args.save_model:
# torch.save(model.state_dict(), "mnist_cnn.pt")
# if __name__ == '__main__':
# main()
# %%
# %%
# %%
# %%