-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
56 lines (47 loc) · 2 KB
/
main.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
import numpy as np
import argparse
from datasets.dual_dataset import create_loader
from models.mlp import Regressor
from models.policy_gradient import PolicyGradient
from models.training import train
from models.optimizers import adam
from losses.continuous import mse
from losses.discrete import cross_entropy_logits
if __name__ == "__main__":
# Default parameters
architecture = 'pg'
classify = True
samples = 10000
features = 128
responses = 8
batch_size = 128
epochs = 10
framework = 'pytorch'
# Parse command line args
parser = argparse.ArgumentParser()
parser.add_argument('--architecture', type=str, default=architecture, required=False)
parser.add_argument('--classify', action='store_true', default=False)
parser.add_argument('--samples', type=int, default=samples, required=False)
parser.add_argument('--features', type=int, default=features, required=False)
parser.add_argument('--responses', type=int, default=responses, required=False)
parser.add_argument('--batch_size', type=int, default=batch_size, required=False)
parser.add_argument('--epochs', type=int, default=epochs, required=False)
parser.add_argument('--framework', type=str, default=framework, required=False)
args = parser.parse_args()
locals().update(vars(args))
# Determine whether to build a RL policy gradient model or simple MLP
Model = PolicyGradient if architecture == 'pg' else Regressor
# Generate some random data
x = np.random.rand(samples, features).astype(np.float32)
y = np.random.rand(samples, responses).astype(np.float32)
loss = mse
if classify:
y = np.argmax(y, axis=1).astype(np.int64)
loss = cross_entropy_logits
# Create a data loader
loader = create_loader(x, y, batch_size, framework)
# Create a model and optimizer
model = Model(features, responses, framework)
optimizer = adam(model, learning_rate=0.01)
# Train model
losses = train(model, optimizer, loss, loader, epochs)