<s> import lasagne . layers as L <EOL> import lasagne . nonlinearities as NL <EOL> import lasagne . init <EOL> import numpy as np <EOL> import theano . tensor as TT <EOL> from rllab . core . lasagne_layers import ParamLayer <EOL> from rllab . core . lasagne_powered import LasagnePowered <EOL> from rllab . core . network import GRUNetwork <EOL> from rllab . core . serializable import Serializable <EOL> from rllab . distributions . recurrent_diagonal_gaussian import RecurrentDiagonalGaussian <EOL> from rllab . misc import ext <EOL> from rllab . spaces import Discrete <EOL> from rllab . misc . overrides import overrides <EOL> from rllab . policies . base import StochasticPolicy <EOL> class GaussianGRUPolicy ( StochasticPolicy , LasagnePowered , Serializable ) : <EOL> def __init__ ( <EOL> self , <EOL> env_spec , <EOL> hidden_sizes = ( <NUM_LIT:32> , ) , <EOL> state_include_action = True , <EOL> hidden_nonlinearity = NL . tanh , <EOL> learn_std = True , <EOL> init_std = <NUM_LIT:1.0> , <EOL> output_nonlinearity = None , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> Serializable . quick_init ( self , locals ( ) ) <EOL> super ( GaussianGRUPolicy , self ) . __init__ ( env_spec ) <EOL> assert len ( hidden_sizes ) == <NUM_LIT:1> <EOL> if state_include_action : <EOL> obs_dim = env_spec . observation_space . flat_dim + env_spec . action_space . flat_dim <EOL> else : <EOL> obs_dim = env_spec . observation_space . flat_dim <EOL> action_dim = env_spec . action_space . flat_dim <EOL> mean_network = GRUNetwork ( <EOL> input_shape = ( obs_dim , ) , <EOL> output_dim = action_dim , <EOL> hidden_dim = hidden_sizes [ <NUM_LIT:0> ] , <EOL> hidden_nonlinearity = hidden_nonlinearity , <EOL> output_nonlinearity = NL . softmax , <EOL> ) <EOL> l_mean = mean_network . output_layer <EOL> obs_var = mean_network . input_var <EOL> l_log_std = ParamLayer ( <EOL> mean_network . input_layer , <EOL> num_units = action_dim , <EOL> param = lasagne . init . Constant ( np . log ( init_std ) ) , <EOL> name = "<STR_LIT>" , <EOL> trainable = learn_std , <EOL> ) <EOL> l_step_log_std = ParamLayer ( <EOL> mean_network . step_input_layer , <EOL> num_units = action_dim , <EOL> param = l_log_std . param , <EOL> name = "<STR_LIT>" , <EOL> trainable = learn_std , <EOL> ) <EOL> self . _mean_network = mean_network <EOL> self . _l_log_std = l_log_std <EOL> self . _state_include_action = state_include_action <EOL> self . _f_step_mean_std = ext . compile_function ( <EOL> [ <EOL> mean_network . step_input_layer . input_var , <EOL> mean_network . step_prev_hidden_layer . input_var <EOL> ] , <EOL> L . get_output ( [ <EOL> mean_network . step_output_layer , <EOL> l_step_log_std , <EOL> mean_network . step_hidden_layer <EOL> ] ) <EOL> ) <EOL> self . _prev_action = None <EOL> self . _prev_hidden = None <EOL> self . _hidden_sizes = hidden_sizes <EOL> self . _dist = RecurrentDiagonalGaussian ( ) <EOL> self . reset ( ) <EOL> LasagnePowered . __init__ ( self , [ mean_network . output_layer , l_log_std ] ) <EOL> @ overrides <EOL> def dist_info_sym ( self , obs_var , state_info_vars ) : <EOL> n_batches , n_steps = obs_var . shape [ : <NUM_LIT:2> ] <EOL> obs_var = obs_var . reshape ( ( n_batches , n_steps , - <NUM_LIT:1> ) ) <EOL> if self . _state_include_action : <EOL> prev_action_var = state_info_vars [ "<STR_LIT>" ] <EOL> all_input_var = TT . concatenate ( <EOL> [ obs_var , prev_action_var ] , <EOL> axis = <NUM_LIT:2> <EOL> ) <EOL> else : <EOL> all_input_var = obs_var <EOL> means , log_stds = L . get_output ( [ self . _mean_network . output_layer , self . _l_log_std ] , all_input_var ) <EOL> return dict ( mean = means , log_std = log_stds ) <EOL> def reset ( self ) : <EOL> self . _prev_action = None <EOL> self . _prev_hidden = self . _mean_network . hid_init_param . get_value ( ) <EOL> @ overrides <EOL> def get_action ( self , observation ) : <EOL> if self . _state_include_action : <EOL> if self . _prev_action is None : <EOL> prev_action = np . zeros ( ( self . action_space . flat_dim , ) ) <EOL> else : <EOL> prev_action = self . action_space . flatten ( self . _prev_action ) <EOL> all_input = np . concatenate ( [ <EOL> self . observation_space . flatten ( observation ) , <EOL> prev_action <EOL> ] ) <EOL> else : <EOL> all_input = self . observation_space . flatten ( observation ) <EOL> prev_action = np . nan <EOL> mean , log_std , hidden_vec = [ x [ <NUM_LIT:0> ] for x in self . _f_step_mean_std ( [ all_input ] , [ self . _prev_hidden ] ) ] <EOL> rnd = np . random . randn ( len ( mean ) ) <EOL> action = rnd * np . exp ( log_std ) + mean <EOL> self . _prev_action = action <EOL> self . _prev_hidden = hidden_vec <EOL> agent_info = dict ( mean = mean , log_std = log_std ) <EOL> if self . _state_include_action : <EOL> agent_info [ "<STR_LIT>" ] = prev_action <EOL> return action , agent_info <EOL> @ property <EOL> @ overrides <EOL> def recurrent ( self ) : <EOL> return True <EOL> @ property <EOL> def distribution ( self ) : <EOL> return self . _dist <EOL> @ property <EOL> def state_info_keys ( self ) : <EOL> if self . _state_include_action : <EOL> return [ "<STR_LIT>" ] <EOL> else : <EOL> return [ ] </s>
