|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import project_root |
| 5 | +import numpy as np |
| 6 | +import tensorflow as tf |
| 7 | +from os import path |
| 8 | +from env.sender import Sender |
| 9 | +from models import DaggerNetwork |
| 10 | +from helpers.helpers import ewma |
| 11 | + |
| 12 | + |
| 13 | +def softmax(x): |
| 14 | + e_x = np.exp(x - np.max(x)) |
| 15 | + return e_x / e_x.sum(axis=0) |
| 16 | + |
| 17 | + |
| 18 | +class Learner(object): |
| 19 | + def __init__(self, state_dim, action_cnt, restore_vars): |
| 20 | + |
| 21 | + with tf.variable_scope('local'): |
| 22 | + self.pi = DaggerNetwork(state_dim=state_dim, action_cnt=action_cnt) |
| 23 | + |
| 24 | + self.ewma_window = 3 # alpha = 2 / (window + 1) |
| 25 | + self.session = tf.Session() |
| 26 | + |
| 27 | + # restore saved variables |
| 28 | + saver = tf.train.Saver(self.pi.trainable_vars) |
| 29 | + saver.restore(self.session, restore_vars) |
| 30 | + |
| 31 | + # init the remaining vars, especially those created by optimizer |
| 32 | + uninit_vars = set(tf.global_variables()) - set(self.pi.trainable_vars) |
| 33 | + self.session.run(tf.variables_initializer(uninit_vars)) |
| 34 | + |
| 35 | + def sample_action(self, step_state_buf): |
| 36 | + |
| 37 | + # For ewma delay, only want first component, the one-way delay |
| 38 | + # For the cwnd, try only the most recent cwnd |
| 39 | + owd_buf = np.asarray([state[0] for state in step_state_buf]) |
| 40 | + ewma_delay = ewma(owd_buf, self.ewma_window) |
| 41 | + last_cwnd = step_state_buf[-1][1] |
| 42 | + |
| 43 | + # Get probability of each action from the local network. |
| 44 | + pi = self.local_network |
| 45 | + action_probs = self.sess.run(pi.action_probs, |
| 46 | + feed_dict={pi.states: [[ewma_delay, |
| 47 | + last_cwnd]]}) |
| 48 | + |
| 49 | + # action = np.argmax(action_probs[0]) |
| 50 | + # action = np.argmax(np.random.multinomial(1, action_probs[0] - 1e-5)) |
| 51 | + temperature = 1.0 |
| 52 | + temp_probs = softmax(action_probs[0] / temperature) |
| 53 | + action = np.argmax(np.random.multinomial(1, temp_probs - 1e-5)) |
| 54 | + return action |
| 55 | + |
| 56 | + |
| 57 | +def main(): |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + parser.add_argument('port', type=int) |
| 60 | + args = parser.parse_args() |
| 61 | + |
| 62 | + sender = Sender(args.port) |
| 63 | + |
| 64 | + model_path = path.join(project_root.DIR, 'dagger', 'logs', |
| 65 | + '2017-07-31--06-32-01-true-expert-2', |
| 66 | + 'checkpoint-1100') |
| 67 | + |
| 68 | + learner = Learner( |
| 69 | + state_dim=Sender.state_dim, |
| 70 | + action_cnt=Sender.action_cnt, |
| 71 | + restore_vars=model_path) |
| 72 | + |
| 73 | + sender.set_sample_action(learner.sample_action) |
| 74 | + |
| 75 | + try: |
| 76 | + sender.handshake() |
| 77 | + sender.run() |
| 78 | + except KeyboardInterrupt: |
| 79 | + pass |
| 80 | + finally: |
| 81 | + sender.cleanup() |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + main() |
0 commit comments