forked from facebookresearch/EGG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
179 lines (141 loc) · 9.26 KB
/
train.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
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import json
import argparse
import numpy as np
import torch.utils.data
import torch.nn.functional as F
import egg.core as core
from egg.core import EarlyStopperAccuracy
from egg.zoo.channel.features import OneHotLoader, UniformLoader
from egg.zoo.channel.archs import Sender, Receiver
def get_params(params):
parser = argparse.ArgumentParser()
parser.add_argument('--n_features', type=int, default=10,
help='Dimensionality of the "concept" space (default: 10)')
parser.add_argument('--batches_per_epoch', type=int, default=1000,
help='Number of batches per epoch (default: 1000)')
parser.add_argument('--dim_dataset', type=int, default=10240,
help='Dim of constructing the data (default: 10240)')
parser.add_argument('--force_eos', type=int, default=0,
help='Force EOS at the end of the messages (default: 0)')
parser.add_argument('--sender_hidden', type=int, default=10,
help='Size of the hidden layer of Sender (default: 10)')
parser.add_argument('--receiver_hidden', type=int, default=10,
help='Size of the hidden layer of Receiver (default: 10)')
parser.add_argument('--receiver_num_layers', type=int, default=1,
help='Number hidden layers of receiver. Only in reinforce (default: 1)')
parser.add_argument('--sender_num_layers', type=int, default=1,
help='Number hidden layers of receiver. Only in reinforce (default: 1)')
parser.add_argument('--receiver_num_heads', type=int, default=8,
help='Number of attention heads for Transformer Receiver (default: 8)')
parser.add_argument('--sender_num_heads', type=int, default=8,
help='Number of self-attention heads for Transformer Sender (default: 8)')
parser.add_argument('--sender_embedding', type=int, default=10,
help='Dimensionality of the embedding hidden layer for Sender (default: 10)')
parser.add_argument('--receiver_embedding', type=int, default=10,
help='Dimensionality of the embedding hidden layer for Receiver (default: 10)')
parser.add_argument('--causal_sender', default=False, action='store_true')
parser.add_argument('--causal_receiver', default=False, action='store_true')
parser.add_argument('--sender_generate_style', type=str, default='in-place', choices=['standard', 'in-place'],
help='How the next symbol is generated within the TransformerDecoder (default: in-place)')
parser.add_argument('--sender_cell', type=str, default='rnn',
help='Type of the cell used for Sender {rnn, gru, lstm, transformer} (default: rnn)')
parser.add_argument('--receiver_cell', type=str, default='rnn',
help='Type of the model used for Receiver {rnn, gru, lstm, transformer} (default: rnn)')
parser.add_argument('--sender_entropy_coeff', type=float, default=1e-1,
help='The entropy regularisation coefficient for Sender (default: 1e-1)')
parser.add_argument('--receiver_entropy_coeff', type=float, default=1e-1,
help='The entropy regularisation coefficient for Receiver (default: 1e-1)')
parser.add_argument('--probs', type=str, default='uniform',
help="Prior distribution over the concepts (default: uniform)")
parser.add_argument('--length_cost', type=float, default=0.0,
help="Penalty for the message length, each symbol would before <EOS> would be "
"penalized by this cost (default: 0.0)")
parser.add_argument('--name', type=str, default='model',
help="Name for your checkpoint (default: model)")
parser.add_argument('--early_stopping_thr', type=float, default=0.9999,
help="Early stopping threshold on accuracy (default: 0.9999)")
args = core.init(parser, params)
return args
def loss(sender_input, _message, _receiver_input, receiver_output, _labels):
acc = (receiver_output.argmax(dim=1) == sender_input.argmax(dim=1)).detach().float()
loss = F.cross_entropy(receiver_output, sender_input.argmax(dim=1), reduction="none")
return loss, {'acc': acc}
def dump(game, n_features, device, gs_mode):
# tiny "dataset"
dataset = [[torch.eye(n_features).to(device), None]]
sender_inputs, messages, receiver_inputs, receiver_outputs, _ = \
core.dump_sender_receiver(game, dataset, gs=gs_mode, device=device, variable_length=True)
unif_acc = 0.
powerlaw_acc = 0.
powerlaw_probs = 1 / np.arange(1, n_features+1, dtype=np.float32)
powerlaw_probs /= powerlaw_probs.sum()
for sender_input, message, receiver_output in zip(sender_inputs, messages, receiver_outputs):
input_symbol = sender_input.argmax()
output_symbol = receiver_output.argmax()
acc = (input_symbol == output_symbol).float().item()
unif_acc += acc
powerlaw_acc += powerlaw_probs[input_symbol] * acc
print(f'input: {input_symbol.item()} -> message: {",".join([str(x.item()) for x in message])} -> output: {output_symbol.item()}', flush=True)
unif_acc /= n_features
print(f'Mean accuracy wrt uniform distribution is {unif_acc}')
print(f'Mean accuracy wrt powerlaw distribution is {powerlaw_acc}')
print(json.dumps({'powerlaw': powerlaw_acc, 'unif': unif_acc}))
def main(params):
opts = get_params(params)
print(opts, flush=True)
device = opts.device
force_eos = opts.force_eos == 1
if opts.probs == 'uniform':
probs = np.ones(opts.n_features)
elif opts.probs == 'powerlaw':
probs = 1 / np.arange(1, opts.n_features+1, dtype=np.float32)
else:
probs = np.array([float(x) for x in opts.probs.split(',')], dtype=np.float32)
probs /= probs.sum()
print('the probs are: ', probs, flush=True)
train_loader = OneHotLoader(n_features=opts.n_features, batch_size=opts.batch_size,
batches_per_epoch=opts.batches_per_epoch, probs=probs)
# single batches with 1s on the diag
test_loader = UniformLoader(opts.n_features)
if opts.sender_cell == 'transformer':
sender = Sender(n_features=opts.n_features, n_hidden=opts.sender_embedding)
sender = core.TransformerSenderReinforce(agent=sender, vocab_size=opts.vocab_size,
embed_dim=opts.sender_embedding, max_len=opts.max_len,
num_layers=opts.sender_num_layers, num_heads=opts.sender_num_heads,
hidden_size=opts.sender_hidden,
force_eos=opts.force_eos,
generate_style=opts.sender_generate_style,
causal=opts.causal_sender)
else:
sender = Sender(n_features=opts.n_features, n_hidden=opts.sender_hidden)
sender = core.RnnSenderReinforce(sender,
opts.vocab_size, opts.sender_embedding, opts.sender_hidden,
cell=opts.sender_cell, max_len=opts.max_len, num_layers=opts.sender_num_layers,
force_eos=force_eos)
if opts.receiver_cell == 'transformer':
receiver = Receiver(n_features=opts.n_features, n_hidden=opts.receiver_embedding)
receiver = core.TransformerReceiverDeterministic(receiver, opts.vocab_size, opts.max_len,
opts.receiver_embedding, opts.receiver_num_heads, opts.receiver_hidden,
opts.receiver_num_layers, causal=opts.causal_receiver)
else:
receiver = Receiver(n_features=opts.n_features, n_hidden=opts.receiver_hidden)
receiver = core.RnnReceiverDeterministic(receiver, opts.vocab_size, opts.receiver_embedding,
opts.receiver_hidden, cell=opts.receiver_cell,
num_layers=opts.receiver_num_layers)
game = core.SenderReceiverRnnReinforce(sender, receiver, loss, sender_entropy_coeff=opts.sender_entropy_coeff,
receiver_entropy_coeff=opts.receiver_entropy_coeff,
length_cost=opts.length_cost)
optimizer = core.build_optimizer(game.parameters())
trainer = core.Trainer(game=game, optimizer=optimizer, train_data=train_loader,
validation_data=test_loader, callbacks=[EarlyStopperAccuracy(opts.early_stopping_thr)])
trainer.train(n_epochs=opts.n_epochs)
if opts.checkpoint_dir:
trainer.save_checkpoint(name=f'{opts.name}_vocab{opts.vocab_size}_rs{opts.random_seed}_lr{opts.lr}_shid{opts.sender_hidden}_rhid{opts.receiver_hidden}_sentr{opts.sender_entropy_coeff}_reg{opts.length_cost}_max_len{opts.max_len}')
dump(trainer.game, opts.n_features, device, False)
core.close()
if __name__ == "__main__":
import sys
main(sys.argv[1:])