-
Notifications
You must be signed in to change notification settings - Fork 102
/
example.py
74 lines (58 loc) · 1.93 KB
/
example.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
# Sparsely-Gated Mixture-of-Experts Layers.
# See "Outrageously Large Neural Networks"
# https://arxiv.org/abs/1701.06538
#
# Author: David Rau
#
import torch
from torch import nn
from torch.optim import Adam
from moe import MoE
def train(x, y, model, loss_fn, optim):
# model returns the prediction and the loss that encourages all experts to have equal importance and load
y_hat, aux_loss = model(x.float())
# calculate prediction loss
loss = loss_fn(y_hat, y)
# combine losses
total_loss = loss + aux_loss
optim.zero_grad()
total_loss.backward()
optim.step()
print("Training Results - loss: {:.2f}, aux_loss: {:.3f}".format(loss.item(), aux_loss.item()))
return model
def eval(x, y, model, loss_fn):
model.eval()
# model returns the prediction and the loss that encourages all experts to have equal importance and load
y_hat, aux_loss = model(x.float())
loss = loss_fn(y_hat, y)
total_loss = loss + aux_loss
print("Evaluation Results - loss: {:.2f}, aux_loss: {:.3f}".format(loss.item(), aux_loss.item()))
def dummy_data(batch_size, input_size, num_classes):
# dummy input
x = torch.rand(batch_size, input_size)
# dummy target
y = torch.randint(num_classes, (batch_size, 1)).squeeze(1)
return x, y
# arguments
input_size = 1000
num_classes = 20
num_experts = 10
hidden_size = 64
batch_size = 5
k = 4
# determine device
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
# instantiate the MoE layer
model = MoE(input_size, num_classes, num_experts, hidden_size, k=k, noisy_gating=True)
model = model.to(device)
loss_fn = nn.CrossEntropyLoss()
optim = Adam(model.parameters())
x, y = dummy_data(batch_size, input_size, num_classes)
# train
model = train(x.to(device), y.to(device), model, loss_fn, optim)
# evaluate
x, y = dummy_data(batch_size, input_size, num_classes)
eval(x.to(device), y.to(device), model, loss_fn)