-
Notifications
You must be signed in to change notification settings - Fork 1
/
quantum-wgan.py
executable file
·302 lines (227 loc) · 9.47 KB
/
quantum-wgan.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Library imports
import math
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import pennylane as qml
# Pytorch imports
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
# Set the random seed for reproducibility
seed = 42
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
class GetDataset(Dataset):
"""Pytorch loader of transactions dataset"""
def __init__(self, csv_file):
"""
Args:
csv_file (string): Path to the csv file with annotations.
"""
self.csv_file = csv_file
self.df = pd.read_csv(self.csv_file)
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
transaction = self.df.iloc[idx, :-1]
# Return image and label
return transaction
# Loading the data from file
batch_size = 64
dataset = GetDataset(csv_file="creditcard.csv")
dataset.df.drop('Time', inplace=True, axis=1)
# selection only good (i.e. non-fraudulent data) for WGAN training
good = dataset.df[dataset.df['Class'] == 0]
dataset.df = pd.concat([good])
for col in dataset.df.columns:
dataset.df[col] = (dataset.df[col] - dataset.df[col].min()) / (dataset.df[col].max() - dataset.df[col].min())
dataloader = torch.utils.data.DataLoader(
torch.tensor(dataset).float(), batch_size=batch_size, shuffle=True
)
def init_weights(m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight)
m.bias.data.fill_(0.01)
class Discriminator(nn.Module):
"""
Discriminator with fully connected layers
"""
def __init__(self, n_features):
"""
Args:
n_features (int): Number of features of input data.
"""
super(Discriminator, self).__init__()
self.discriminator = nn.Sequential(
nn.Linear(n_features, n_features),
nn.Linear(n_features, 16),
nn.Linear(16, 8),
nn.Linear(8, 1),
)
for module in self.modules():
if isinstance(module, nn.Linear):
module.apply(init_weights)
def forward(self, x):
return self.discriminator(x)
def discriminator_loss(disc_fake_pred, disc_real_pred, gp, g_lambda):
"""
Calculates the discrimination loss
Args:
disc_fake_pred (Tensor): Prediction of discriminator based on generated data.
disc_real_pred (Tensor): Prediction of discriminator based on real data.
gp (Tensor): Value of gradient penalty.
"""
discriminator_loss = torch.mean(disc_fake_pred) - torch.mean(disc_real_pred) + g_lambda * gp
return discriminator_loss
def get_gen_loss(disc_fake_pred):
"""
Calculates the generator loss.
Args:
disc_fake_pred (Tensor): Prediction of discriminator based on generated data.
"""
gen_loss = -1. * torch.mean(disc_fake_pred)
return gen_loss
def gradient_penalty(disc, real_data, fake_data):
"""
Calculates the gradient penalty in WGAN network.
Args:
disc (function): Discriminator used in network training.
real_data (Tensor): Real data from training data
fake_data (Tensor): Data obtained from the generator.
"""
batch_size = real_data.size(0)
eps = torch.rand(batch_size, 1).to(real_data.device)
eps = eps.expand_as(real_data)
interpolation = eps * real_data + (1 - eps) * fake_data
interp_logits = disc(interpolation)
grad_outputs = torch.ones_like(interp_logits)
gradients = torch.autograd.grad(
inputs=interpolation,
outputs=interp_logits,
grad_outputs=grad_outputs,
create_graph=True,
retain_graph=True,
)[0]
gradients = gradients.view(batch_size, -1)
grad_norm = gradients.norm(2, 1)
return torch.mean((grad_norm - 1) ** 2)
# define parameters of quantum circuit
n_qubits = 8
n_layers = 3
# Choice of quantum simulator
dev = qml.device("lightning.qubit", wires=n_qubits)
# Run simulation on CUDA if available
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
@qml.qnode(dev, interface="torch", diff_method="parameter-shift")
def quantum_circuit(latents, theta, n_layers, rotation='Z'):
"""
Quantum circuit used as a input part of generator.
Args:
latents (Parameter): Tensor of latent parameters used in first layer of circuit
theta (Parameter): Parameters optimized during training of WGAN network
n_layers (int): Number of layers in quantum circuit
rotation (str): Rotation gate used in quantum circuit (default: 'Z')
"""
theta = theta.reshape(n_layers, n_qubits)
rot_gates = {'X': qml.RX, 'Y': qml.RY, 'Z': qml.RZ}
rot_gate = rot_gates[rotation]
# Initialize latent vectors
for i in range(n_qubits):
qml.RX(latents[i], wires=i)
# Repeated layer
for n in range(n_layers):
for q in range(n_qubits):
rot_gate(theta[n][q], wires=q)
for q in range(1, n_qubits, 2):
qml.CNOT(wires=[q - 1, q])
for q in range(1, n_qubits - 1, 2):
qml.CNOT(wires=[q, q + 1])
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
class QuantumGenerator(nn.Module):
"""
Quantum Generator with first part replaced with quantum circuit
"""
def __init__(self, latents, n_features, n_layers, rotation='Z'):
"""
Args:
latents: Tensor of latent variables as input to the Generator.
n_features (int): Number of features of input data.
n_layers (int): Number of layers in quantum_circuit
"""
super().__init__()
self.n_layers = n_layers
self.latents = latents
self.rotation = rotation
self.q_params = nn.Parameter(torch.rand(n_layers * n_qubits), requires_grad=True)
self.l_params = nn.Parameter(torch.rand(n_qubits), requires_grad=False)
self.generator = nn.Sequential(
nn.Linear(n_qubits, n_features),
nn.Sigmoid()
)
for module in self.modules():
if isinstance(module, nn.Linear):
module.apply(init_weights)
def forward(self, x):
h1 = torch.as_tensor(self.latents)
for i in range(batch_size):
h1[i] = quantum_circuit(self.l_params, self.q_params, self.n_layers, self.rotation)
h1.reshape(batch_size, self.latents.size(1))
output = self.generator(h1)
return output
lr = 0.0002 # learning rate for the Generator
beta_1 = 0.5 # optimizer coefficient for computing running average of gradient
beta_2 = 0.999 # optimizer coefficient for computing running average of gradient squared
adam_eps = 1e-7 # coefficient for improving numerical stability
num_iter = 1500 # number of training iterations
n_disc = 5 # number of discriminator training steps per one
discriminator = Discriminator(n_features=29).to(device)
opt_disc = optim.Adam(discriminator.parameters(), lr=lr, eps=adam_eps, betas=(beta_1, beta_2))
real_labels = torch.full((batch_size,), 1.0, dtype=torch.float, device=device)
fake_labels = torch.full((batch_size,), 0.0, dtype=torch.float, device=device)
g_lambda = 10
results = []
gen_losses = torch.zeros(num_iter)
disc_losses = torch.zeros(num_iter)
for it in range(num_iter):
for i, data in enumerate(dataloader):
for k in range(n_disc):
for p in discriminator.parameters():
p.requires_grad = True
real_data = data.to(device)
theta = -2* np.pi * torch.rand(batch_size, n_qubits*n_layers, device=device, requires_grad=True) + np.pi
latents = -2* np.pi * torch.rand(batch_size, n_qubits, device=device, requires_grad=False) + np.pi
generator = QuantumGenerator(latents, n_features=29, n_layers=n_layers).to(device)
opt_gen = optim.Adam(generator.parameters(), lr=0.01, eps=adam_eps, betas=(beta_1, beta_2))
eps = torch.rand(batch_size, len(real_data[0]), device=device, requires_grad=True)
fake_data = generator(theta)
fake_data_hat = real_data + eps * (fake_data - real_data)
# training the discriminator
discriminator.zero_grad()
disc_real = discriminator(real_data.float()).view(-1).to(device)
disc_fake = discriminator(fake_data_hat.detach().float()).view(-1).to(device)
gp = gradient_penalty(discriminator, real_data, fake_data)
disc_loss = discriminator_loss(disc_fake, disc_real, gp, g_lambda)
disc_losses[i] = disc_loss
disc_loss.backward()
opt_disc.step()
# training the generator
for p in discriminator.parameters():
p.requires_grad = False # to avoid computation
generator.zero_grad()
discr_fake = discriminator(fake_data).view(-1)
err_gen = get_gen_loss(discr_fake)
gen_losses[i] = err_gen
err_gen.backward()
opt_gen.step()
# show loss value
if it % 10 == 0:
print(f"Iteration: {it}, Discriminator loss: {disc_loss:0.3f}, Generator Loss: {err_gen:0.3f}")