|
| 1 | +import numpy as np |
| 2 | +#import ipdb |
| 3 | +import torch |
| 4 | +import torch.optim as optim |
| 5 | +import torch.nn as nn |
| 6 | +import torch.utils.data |
| 7 | +import torch.nn.functional as F |
| 8 | +import tensor_comprehensions as tc |
| 9 | +from visdom import Visdom |
| 10 | + |
| 11 | +import utils |
| 12 | + |
| 13 | +NB_EPOCHS = 1000 |
| 14 | +BATCH_SZ = 1 |
| 15 | + |
| 16 | +viz = Visdom() |
| 17 | +win0 = viz.line(X=np.arange(NB_EPOCHS), Y=np.random.rand(NB_EPOCHS)) |
| 18 | + |
| 19 | +(tc_code, tc_name, inp, init_input_sz) = utils.get_convolution_example() |
| 20 | + |
| 21 | +NB_HYPERPARAMS, INIT_INPUT_SZ = utils.NB_HYPERPARAMS, utils.INIT_INPUT_SZ |
| 22 | + |
| 23 | +def getRandom(): |
| 24 | + opt_v = np.zeros(NB_HYPERPARAMS).astype(int) |
| 25 | + for i in range(opt_v.shape[0]): |
| 26 | + opt_v[i] = np.random.randint(utils.cat_sz[i]) |
| 27 | + return opt_v |
| 28 | + |
| 29 | + |
| 30 | +INTER_DISP = 20 |
| 31 | + |
| 32 | +running_reward = -0.5 |
| 33 | +tab_rewards=[] |
| 34 | +tab_best=[] |
| 35 | +best=-12 |
| 36 | +best_options = -1 |
| 37 | +for i in range(NB_EPOCHS): |
| 38 | + rewards = [] |
| 39 | + opts=[] |
| 40 | + for j in range(BATCH_SZ): |
| 41 | + out = getRandom() |
| 42 | + reward = utils.evalTime(out.astype(int), prune=2, curr_best=np.exp(-best)) |
| 43 | + reward = -np.log(reward) |
| 44 | + rewards.append(reward) |
| 45 | + opts.append(out.astype(int)) |
| 46 | + if(best < np.max(rewards) or i==0): |
| 47 | + best = np.max(rewards) |
| 48 | + ind=np.argmax(rewards) |
| 49 | + best_options = opts[ind] |
| 50 | + utils.print_opt(best_options) |
| 51 | + if(i==0): |
| 52 | + running_reward = reward |
| 53 | + running_reward = running_reward * 0.99 + np.mean(rewards) * 0.01 |
| 54 | + tab_rewards.append(-running_reward) |
| 55 | + tab_best.append(-best) |
| 56 | + if i % INTER_DISP == 0: |
| 57 | + viz.line(X=np.column_stack((np.arange(i+1), np.arange(i+1))), Y=np.column_stack((np.array(tab_rewards), np.array(tab_best))), win=win0, opts=dict(legend=["Geometric run", "Best time"])) |
| 58 | + print(-running_reward) |
| 59 | + print(-best) |
| 60 | +tab_best = np.array(tab_best) |
| 61 | +np.save("randomsearch.npy", tab_best) |
| 62 | +print("Finally, best options are:") |
| 63 | +utils.print_opt(best_options) |
0 commit comments