Ejemplo n.º 1
0
def run(architecture_file, model_file, num_batches=10, pattern="centralModel-*"):
    print "evaluating Q values..."
    redisInstance = Redis(host='localhost', port=6379, db=0)
    model_keys = redisInstance.keys(pattern)
    results = {}

    net = BaristaNet(architecture_file, model_file, None)
    replay_dataset = ReplayDataset("temp-q-converge-dset.hdf5",
                                   net.state[0].shape,
                                   dset_size=1000,
                                   overwrite=True)
    net.add_dataset(replay_dataset)

    game = SnakeGame()
    preprocessor = generate_preprocessor(net.state.shape[2:], gray_scale)
    exp_gain = ExpGain(net, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    print "Generating new experiences..."
    for _ in xrange(100):
        exp_gain.generate_experience(1e5)
    print "Done"

    for key in model_keys:
        print "Evaluating model:", key
        model = dict(redisC.Dict(key=key, redis=redisInstance))
        q_avg = evaluate_model(net, model, num_batches)
        results[key] = q_avg

    for key in sorted(results.keys()):
        print key.ljust(25) + "%0.4f" % results[key]
Ejemplo n.º 2
0
def main():
    args = get_args()

    caffe.set_phase_test()
    if args.mode == "cpu":
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()

    if not os.path.isfile(args.model):
        if not args.solver:
            print "Error: Model does not exist. No solver specified."
            sys.exit(1)

        print "Warning: model %s does not exist. Creating..."
        solver = SGDSolver(args.solver)
        solver.net.save(args.model)

    # Initialize objects
    net = BaristaNet(args.architecture, args.model, args.driver,
                     reset_log=True)

    replay_dataset = ReplayDataset(args.dataset, net.state[0].shape,
                                   dset_size=args.dset_size,
                                   overwrite=args.overwrite)
    net.add_dataset(replay_dataset)

    game = SnakeGame()
    preprocessor = generate_preprocessor(net.state.shape[2:], gray_scale)
    exp_gain = ExpGain(net, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    if(args.overwrite):
        for _ in xrange(min(args.initial_replay, args.dset_size)):
            exp_gain.generate_experience(0)

    # Start server loop
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serversocket.bind(('127.0.0.1', args.port))
    serversocket.listen(5)

    print
    print "*"*80
    print "* Starting BARISTA server: listening on port %d." % args.port
    print "*"*80
    # Signal Spark Executor that Barista is ready to receive connections
    issue_ready_signal(args.port)
    while True:
        (clientsocket, address) = serversocket.accept()
        if args.debug:
            handler = debug_process_connection
        else:
            handler = process_connection

        client_thread = threading.Thread(
                            target=handler,
                            args=(clientsocket, net, exp_gain))
        client_thread.run()
Ejemplo n.º 3
0
def main(solver):
    barista_net = BaristaNet(solver)
    write_ipc_interface(barista_net, 'barista-ipc-interface.txt')

    print "Barista running. Waiting on compute semaphore:",
    print barista_net.compute_semaphore

    print "Conv1_dW norm:", np.linalg.norm(barista_net.net.params['conv1'][0].diff)
    print "Data norm:", np.linalg.norm(barista_net.net.blobs['data'].data)
    for i in range(10):
        barista_net.full_pass()
        print "Completed full pass #%d" % i
        print "Conv1_dW norm:", np.linalg.norm(barista_net.net.params['conv1'][0].diff)
        print "Data norm:", np.linalg.norm(barista_net.net.blobs['data'].data)
Ejemplo n.º 4
0
def test():
    from barista.baristanet import BaristaNet
    baristanet = BaristaNet('models/deepq/train_val.prototxt',
                            'models/deepq/deepq16.caffemodel',
                            'Augustus')

    print "\nData Norms (before loading):"
    print "-"*40
    data_norms = compute_data_norms(baristanet.net, ('state', 'next_state'))
    pretty_print(data_norms)
    baristanet.dummy_load_minibatch()
    print "\nData Norms (after loading):"
    print "-"*40
    data_norms = compute_data_norms(baristanet.net, ('state', 'next_state'))
    pretty_print(data_norms)

    print "\nGradient norms (before computing):"
    print "-"*40
    grad_norms = compute_gradient_norms(baristanet.net, ord=2)
    pretty_print(grad_norms)

    baristanet.full_pass()

    print "\nGradient norms (after computing):"
    print "-"*40
    grad_norms = compute_gradient_norms(baristanet.net, ord=2)
    pretty_print(grad_norms)

    data = extract_net_data(
               baristanet.net,
               ('Q_sa', 'action', 'reward', 'P_sa', 'loss'))
    Q, P = data['Q_sa'], data['P_sa']
    action, reward, loss = data['action'], data['reward'], data['loss']
    print "\nQ_sa:"
    print "-"*40
    print Q.reshape((Q.size, 1))
    print "P_sa:"
    print P
    print "Action:"
    print action
    print "Reward:"
    print reward
    print "Loss:", loss
Ejemplo n.º 5
0
def start_barista(solver_filename):
    pid = os.getpid()
    print("Daemon PID:", pid, file=sys.stderr)

    net = BaristaNet(solver_filename)

    ipc_interface = net.get_ipc_interface()
    ipc_interface_filename = os.path.join(os.getcwd(),
                                          "barista-%d.interface" % pid)
    write_ipc_interface(ipc_interface, ipc_interface_filename)
    print(ipc_interface_filename + "," + str(pid), file=sys.stdout)

    # Close stdout so the pipe can return
    sys.stdout.close()
    os.close(1)

    # Compute until the cows come home
    while True:
        net.full_pass()
Ejemplo n.º 6
0
    def __init__(self, architecture_file, model_file):
        # Initialize objects
        self.net = BaristaNet(architecture_file, model_file, None)
        self.batch_size = self.net.batch_size

        game = SnakeGame()
        preprocessor = generate_preprocessor(self.net.state.shape[2:],
                                             gray_scale)
        self.engines = [ExpGain(self.net, ['w', 'a', 's', 'd'],
                                preprocessor, game.cpu_play,
                                None, game.encode_state())
                        for _ in range(self.batch_size)]
Ejemplo n.º 7
0
class PolicyEvaluator(object):
    def __init__(self, architecture_file, model_file):
        # Initialize objects
        self.net = BaristaNet(architecture_file, model_file, None)
        self.batch_size = self.net.batch_size

        game = SnakeGame()
        preprocessor = generate_preprocessor(self.net.state.shape[2:],
                                             gray_scale)
        self.engines = [ExpGain(self.net, ['w', 'a', 's', 'd'],
                                preprocessor, game.cpu_play,
                                None, game.encode_state())
                        for _ in range(self.batch_size)]

    def evaluate(self, model, num_trials):
        """ Runs |num_trials| games and returns average score. """
        for eg in self.engines:
            set_net_params(eg.net.net, model)
            eg.reset_game()

        total_score = 0
        trials_completed = 0
        scores = [0] * self.batch_size
        while trials_completed < num_trials:
            states = [eg.get_preprocessed_state() for eg in self.engines]
            actions = self.net.select_action(states,
                                             batch_size=self.batch_size)
            for i, (action, eg) in enumerate(zip(actions, self.engines)):
                scores[i] += eg.play_action(eg.actions[action])
                if eg.game_over:
                    total_score += scores[i]
                    trials_completed += 1
                    if trials_completed == num_trials:
                        break
                    eg.reset_game()
                    scores[i] = 0

        return float(total_score)/num_trials
Ejemplo n.º 8
0
def test():
    from barista.baristanet import BaristaNet
    baristanet = BaristaNet('models/deepq/train_val.prototxt',
                            'models/deepq/deepq16.caffemodel', 'Augustus')

    print "\nData Norms (before loading):"
    print "-" * 40
    data_norms = compute_data_norms(baristanet.net, ('state', 'next_state'))
    pretty_print(data_norms)
    baristanet.dummy_load_minibatch()
    print "\nData Norms (after loading):"
    print "-" * 40
    data_norms = compute_data_norms(baristanet.net, ('state', 'next_state'))
    pretty_print(data_norms)

    print "\nGradient norms (before computing):"
    print "-" * 40
    grad_norms = compute_gradient_norms(baristanet.net, ord=2)
    pretty_print(grad_norms)

    baristanet.full_pass()

    print "\nGradient norms (after computing):"
    print "-" * 40
    grad_norms = compute_gradient_norms(baristanet.net, ord=2)
    pretty_print(grad_norms)

    data = extract_net_data(baristanet.net,
                            ('Q_sa', 'action', 'reward', 'P_sa', 'loss'))
    Q, P = data['Q_sa'], data['P_sa']
    action, reward, loss = data['action'], data['reward'], data['loss']
    print "\nQ_sa:"
    print "-" * 40
    print Q.reshape((Q.size, 1))
    print "P_sa:"
    print P
    print "Action:"
    print action
    print "Reward:"
    print reward
    print "Loss:", loss
Ejemplo n.º 9
0
def main():
    args = get_args()

    caffe.set_phase_test()
    if args.mode == "cpu":
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()

    if not os.path.isfile(args.model):
        if not args.solver:
            print "Error: Model does not exist. No solver specified."
            sys.exit(1)

        print "Warning: model %s does not exist. Creating..."
        solver = SGDSolver(args.solver)
        solver.net.save(args.model)

    # Initialize objects
    net = BaristaNet(args.architecture,
                     args.model,
                     args.driver,
                     reset_log=True)

    replay_dataset = ReplayDataset(args.dataset,
                                   net.state[0].shape,
                                   dset_size=args.dset_size,
                                   overwrite=args.overwrite)
    net.add_dataset(replay_dataset)

    game = SnakeGame()
    preprocessor = generate_preprocessor(net.state.shape[2:], gray_scale)
    exp_gain = ExpGain(net, ['w', 'a', 's', 'd'], preprocessor, game.cpu_play,
                       replay_dataset, game.encode_state())

    if (args.overwrite):
        for _ in xrange(min(args.initial_replay, args.dset_size)):
            exp_gain.generate_experience(0)

    # Start server loop
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serversocket.bind(('127.0.0.1', args.port))
    serversocket.listen(5)

    print
    print "*" * 80
    print "* Starting BARISTA server: listening on port %d." % args.port
    print "*" * 80
    # Signal Spark Executor that Barista is ready to receive connections
    issue_ready_signal(args.port)
    while True:
        (clientsocket, address) = serversocket.accept()
        if args.debug:
            handler = debug_process_connection
        else:
            handler = process_connection

        client_thread = threading.Thread(target=handler,
                                         args=(clientsocket, net, exp_gain))
        client_thread.run()