def __init__(self, input_shape, output_dim, conv_filters, conv_filter_sizes, conv_strides, conv_pads, hidden_sizes, hidden_nonlinearity, output_nonlinearity, name=None, hidden_w_init=ly.XavierUniformInitializer(), hidden_b_init=tf.zeros_initializer(), output_w_init=ly.XavierUniformInitializer(), output_b_init=tf.zeros_initializer(), input_var=None, input_layer=None, batch_normalization=False, weight_normalization=False): Serializable.quick_init(self, locals()) """ A network composed of several convolution layers followed by some fc layers. input_shape: (width,height,channel) HOWEVER, network inputs are assumed flattened. This network will first unflatten the inputs and then apply the standard convolutions and so on. conv_filters: a list of numbers of convolution kernel conv_filter_sizes: a list of sizes (int) of the convolution kernels conv_strides: a list of strides (int) of the conv kernels conv_pads: a list of pad formats (either 'SAME' or 'VALID') hidden_nonlinearity: a nonlinearity from tf.nn, shared by all conv and fc layers hidden_sizes: a list of numbers of hidden units for all fc layers """ with tf.variable_scope(name, 'ConvNetwork'): if input_layer is not None: l_in = input_layer l_hid = l_in elif len(input_shape) == 3: l_in = ly.InputLayer(shape=(None, np.prod(input_shape)), input_var=input_var, name='input') l_hid = ly.reshape(l_in, ([0], ) + input_shape, name='reshape_input') elif len(input_shape) == 2: l_in = ly.InputLayer(shape=(None, np.prod(input_shape)), input_var=input_var, name='input') input_shape = (1, ) + input_shape l_hid = ly.reshape(l_in, ([0], ) + input_shape, name='reshape_input') else: l_in = ly.InputLayer(shape=(None, ) + input_shape, input_var=input_var, name='input') l_hid = l_in if batch_normalization: l_hid = ly.batch_norm(l_hid) for idx, conv_filter, filter_size, stride, pad in zip( range(len(conv_filters)), conv_filters, conv_filter_sizes, conv_strides, conv_pads, ): l_hid = ly.Conv2DLayer( l_hid, num_filters=conv_filter, filter_size=filter_size, stride=(stride, stride), pad=pad, nonlinearity=hidden_nonlinearity, name='conv_hidden_%d' % idx, weight_normalization=weight_normalization, ) if batch_normalization: l_hid = ly.batch_norm(l_hid) if output_nonlinearity == ly.spatial_expected_softmax: assert not hidden_sizes assert output_dim == conv_filters[-1] * 2 l_hid.nonlinearity = tf.identity l_out = ly.SpatialExpectedSoftmaxLayer(l_hid) else: l_hid = ly.flatten(l_hid, name='conv_flatten') for idx, hidden_size in enumerate(hidden_sizes): l_hid = ly.DenseLayer( l_hid, num_units=hidden_size, nonlinearity=hidden_nonlinearity, name='hidden_%d' % idx, w=hidden_w_init, b=hidden_b_init, weight_normalization=weight_normalization, ) if batch_normalization: l_hid = ly.batch_norm(l_hid) l_out = ly.DenseLayer( l_hid, num_units=output_dim, nonlinearity=output_nonlinearity, name='output', w=output_w_init, b=output_b_init, weight_normalization=weight_normalization, ) if batch_normalization: l_out = ly.batch_norm(l_out) self._l_in = l_in self._l_out = l_out # self._input_var = l_in.input_var LayersPowered.__init__(self, l_out)
def build_net(self, trainable=True, name=None): """ Set up q network based on class attributes. This function uses layers defined in garage.tf. Args: reuse: A bool indicates whether reuse variables in the same scope. trainable: A bool indicates whether variables are trainable. """ input_shape = self._env_spec.observation_space.shape assert len(input_shape) in [2, 3] if len(input_shape) == 2: input_shape = (1, ) + input_shape with tf.variable_scope(name): l_in = layers.InputLayer(shape=(None, self._obs_dim), name="obs") l_hid = layers.reshape(l_in, ([0], ) + input_shape, name="reshape_input") if self._batch_norm: l_hid = layers.batch_norm(l_hid) for idx, conv_filter, filter_size, stride, pad in zip( range(len(self._conv_filters)), self._conv_filters, self._conv_filter_sizes, self._conv_strides, self._conv_pads, ): l_hid = layers.Conv2DLayer( l_hid, num_filters=conv_filter, filter_size=filter_size, stride=(stride, stride), pad=pad, nonlinearity=self._hidden_nonlinearity, name="conv_hidden_%d" % idx, weight_normalization=self._weight_normalization, trainable=trainable, ) if self._pooling: l_hid = layers.Pool2DLayer(l_hid, pool_size=self._pool_size) if self._batch_norm: l_hid = layers.batch_norm(l_hid) l_hid = layers.flatten(l_hid, name="conv_flatten") l_action = layers.InputLayer(shape=(None, self._action_dim), name="actions") n_layers = len(self._hidden_sizes) + 1 if n_layers > 1: action_merge_layer = \ (self._action_merge_layer % n_layers + n_layers) % n_layers else: action_merge_layer = 1 for idx, size in enumerate(self._hidden_sizes): if self._batch_norm: l_hid = batch_norm(l_hid) if idx == action_merge_layer: l_hid = layers.ConcatLayer([l_hid, l_action]) l_hid = layers.DenseLayer( l_hid, num_units=size, nonlinearity=self._hidden_nonlinearity, trainable=trainable, name="hidden_%d" % (idx + 1)) if action_merge_layer == n_layers: l_hid = layers.ConcatLayer([l_hid, l_action]) l_output = layers.DenseLayer( l_hid, num_units=1, nonlinearity=self._output_nonlinearity, trainable=trainable, name="output") output_var = layers.get_output(l_output) f_qval = tensor_utils.compile_function( [l_in.input_var, l_action.input_var], output_var) output_layer = l_output obs_layer = l_in action_layer = l_action return f_qval, output_layer, obs_layer, action_layer
def __init__(self, input_shape, extra_input_shape, output_dim, hidden_sizes, conv_filters, conv_filter_sizes, conv_strides, conv_pads, name=None, extra_hidden_sizes=None, hidden_w_init=ly.XavierUniformInitializer(), hidden_b_init=tf.zeros_initializer(), output_w_init=ly.XavierUniformInitializer(), output_b_init=tf.zeros_initializer(), hidden_nonlinearity=tf.nn.relu, output_nonlinearity=None, input_var=None, input_layer=None): Serializable.quick_init(self, locals()) if extra_hidden_sizes is None: extra_hidden_sizes = [] with tf.variable_scope(name, 'ConvMergeNetwork'): input_flat_dim = np.prod(input_shape) extra_input_flat_dim = np.prod(extra_input_shape) total_input_flat_dim = input_flat_dim + extra_input_flat_dim if input_layer is None: l_in = ly.InputLayer(shape=(None, total_input_flat_dim), input_var=input_var, name='input') else: l_in = input_layer l_conv_in = ly.reshape(ly.SliceLayer(l_in, indices=slice(input_flat_dim), name='conv_slice'), ([0], ) + input_shape, name='conv_reshaped') l_extra_in = ly.reshape(ly.SliceLayer(l_in, indices=slice( input_flat_dim, None), name='extra_slice'), ([0], ) + extra_input_shape, name='extra_reshaped') l_conv_hid = l_conv_in for idx, conv_filter, filter_size, stride, pad in zip( range(len(conv_filters)), conv_filters, conv_filter_sizes, conv_strides, conv_pads, ): l_conv_hid = ly.Conv2DLayer( l_conv_hid, num_filters=conv_filter, filter_size=filter_size, stride=(stride, stride), pad=pad, nonlinearity=hidden_nonlinearity, name='conv_hidden_%d' % idx, ) l_extra_hid = l_extra_in for idx, hidden_size in enumerate(extra_hidden_sizes): l_extra_hid = ly.DenseLayer( l_extra_hid, num_units=hidden_size, nonlinearity=hidden_nonlinearity, name='extra_hidden_%d' % idx, w=hidden_w_init, b=hidden_b_init, ) l_joint_hid = ly.concat( [ly.flatten(l_conv_hid, name='conv_hidden_flat'), l_extra_hid], name='joint_hidden') for idx, hidden_size in enumerate(hidden_sizes): l_joint_hid = ly.DenseLayer( l_joint_hid, num_units=hidden_size, nonlinearity=hidden_nonlinearity, name='joint_hidden_%d' % idx, w=hidden_w_init, b=hidden_b_init, ) l_out = ly.DenseLayer( l_joint_hid, num_units=output_dim, nonlinearity=output_nonlinearity, name='output', w=output_w_init, b=output_b_init, ) self._l_in = l_in self._l_out = l_out LayersPowered.__init__(self, [l_out], input_layers=[l_in])
def build_net(self, trainable=True, name=None): """ Set up policy network based on class attributes. This function uses layers defined in garage.tf. Args: reuse: A bool indicates whether reuse variables in the same scope. trainable: A bool indicates whether variables are trainable. """ input_shape = self._env_spec.observation_space.shape assert len(input_shape) in [2, 3] if len(input_shape) == 2: input_shape = (1, ) + input_shape with tf.variable_scope(name): l_in = layers.InputLayer(shape=(None, self._obs_dim), name="obs") l_hid = layers.reshape( l_in, ([0], ) + input_shape, name="reshape_input") if self._batch_norm: l_hid = layers.batch_norm(l_hid) for idx, conv_filter, filter_size, stride, pad in zip( range(len(self._conv_filters)), self._conv_filters, self._conv_filter_sizes, self._conv_strides, self._conv_pads, ): l_hid = layers.Conv2DLayer( l_hid, num_filters=conv_filter, filter_size=filter_size, stride=(stride, stride), pad=pad, nonlinearity=self._hidden_nonlinearity, name="conv_hidden_%d" % idx, weight_normalization=self._weight_normalization, trainable=trainable, ) if self._pooling: l_hid = layers.Pool2DLayer(l_hid, pool_size=self._pool_size) if self._batch_norm: l_hid = layers.batch_norm(l_hid) l_hid = layers.flatten(l_hid, name="conv_flatten") for idx, hidden_size in enumerate(self._hidden_sizes): l_hid = layers.DenseLayer( l_hid, num_units=hidden_size, nonlinearity=self._hidden_nonlinearity, name="hidden_%d" % idx, weight_normalization=self._weight_normalization, trainable=trainable, ) if self._batch_norm: l_hid = layers.batch_norm(l_hid) l_output = layers.DenseLayer( l_hid, num_units=self._action_dim, nonlinearity=self._output_nonlinearity, name="output", weight_normalization=self._weight_normalization, trainable=trainable, ) with tf.name_scope(self._policy_network_name): action = layers.get_output(l_output) # scaled_action = tf.multiply( # action, self._action_bound, name="scaled_action") f_prob_online = tensor_utils.compile_function( inputs=[l_in.input_var], outputs=action) output_layer = l_output obs_layer = l_in return f_prob_online, output_layer, obs_layer