示例#1
0
    def __init__(
            self,
            output_dim,
            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,
            input_shape=None,
            batch_normalization=False,
            weight_normalization=False,
    ):

        Serializable.quick_init(self, locals())

        with tf.variable_scope(name, "MLP"):
            if input_layer is None:
                l_in = ly.InputLayer(
                    shape=(None, ) + input_shape,
                    input_var=input_var,
                    name="input")
            else:
                l_in = input_layer
            self._layers = [l_in]
            l_hid = l_in
            if batch_normalization:
                l_hid = ly.batch_norm(l_hid)
            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)
                self._layers.append(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._layers.append(l_out)
            self._l_in = l_in
            self._l_out = l_out

            LayersPowered.__init__(self, l_out)
示例#2
0
    def _build_net(self, reuse=None, custom_getter=None, trainable=None):
        """
        Set up q network based on class attributes. This function uses layers
        defined in rllab.tf.

        Args:
            reuse: A bool indicates whether reuse variables in the same scope.
            custom_getter: A customized getter object used to get variables.
            trainable: A bool indicates whether variables are trainable.
        """
        with tf.variable_scope(self.name,
                               reuse=reuse,
                               custom_getter=custom_getter):
            l_obs = L.InputLayer(shape=(None, self._obs_dim), name="obs")
            l_action = L.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

            l_hidden = l_obs

            for idx, size in enumerate(self._hidden_sizes):
                if self._batch_norm:
                    l_hidden = batch_norm(l_hidden)

                if idx == action_merge_layer:
                    l_hidden = L.ConcatLayer([l_hidden, l_action])

                l_hidden = L.DenseLayer(l_hidden,
                                        num_units=size,
                                        nonlinearity=self._hidden_nonlinearity,
                                        trainable=trainable,
                                        name="hidden_%d" % (idx + 1))

            if action_merge_layer == n_layers:
                l_hidden = L.ConcatLayer([l_hidden, l_action])

            l_output = L.DenseLayer(l_hidden,
                                    num_units=1,
                                    nonlinearity=self._output_nonlinearity,
                                    trainable=trainable,
                                    name="output")

            output_var = L.get_output(l_output)

        self._f_qval = tensor_utils.compile_function(
            [l_obs.input_var, l_action.input_var], output_var)
        self._output_layer = l_output
        self._obs_layer = l_obs
        self._action_layer = l_action

        LayersPowered.__init__(self, [l_output])
    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.
        """
        with tf.variable_scope(name):
            l_obs = L.InputLayer(shape=(None, self._obs_dim), name="obs")
            l_action = L.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

            l_hidden = l_obs

            for idx, size in enumerate(self._hidden_sizes):
                if self._batch_norm:
                    l_hidden = batch_norm(l_hidden)

                if idx == action_merge_layer:
                    l_hidden = L.ConcatLayer([l_hidden, l_action])

                l_hidden = L.DenseLayer(l_hidden,
                                        num_units=size,
                                        nonlinearity=self._hidden_nonlinearity,
                                        trainable=trainable,
                                        name="hidden_%d" % (idx + 1))

            if action_merge_layer == n_layers:
                l_hidden = L.ConcatLayer([l_hidden, l_action])

            l_output = L.DenseLayer(l_hidden,
                                    num_units=1,
                                    nonlinearity=self._output_nonlinearity,
                                    trainable=trainable,
                                    name="output")

            output_var = L.get_output(l_output)

        f_qval = tensor_utils.compile_function(
            [l_obs.input_var, l_action.input_var], output_var)
        output_layer = l_output
        obs_layer = l_obs
        action_layer = l_action

        return f_qval, output_layer, obs_layer, action_layer
示例#4
0
    def _build_net(self, reuse=None, custom_getter=None, trainable=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.
            custom_getter: A customized getter object used to get variables.
            trainable: A bool indicates whether variables are trainable.
        """
        with tf.variable_scope(self.name,
                               reuse=reuse,
                               custom_getter=custom_getter):
            l_in = layers.InputLayer(shape=(None, self._obs_dim), name="obs")

            l_hidden = l_in
            for idx, hidden_size in enumerate(self._hidden_sizes):
                if self._batch_norm:
                    l_hidden = batch_norm(l_hidden)

                l_hidden = layers.DenseLayer(
                    l_hidden,
                    hidden_size,
                    nonlinearity=self._hidden_nonlinearity,
                    trainable=trainable,
                    name="hidden_%d" % idx)

            l_output = layers.DenseLayer(
                l_hidden,
                self._action_dim,
                nonlinearity=self._output_nonlinearity,
                trainable=trainable,
                name="output")

            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")

        self._f_prob_online = tensor_utils.compile_function(
            inputs=[l_in.input_var], outputs=scaled_action)
        self._output_layer = l_output
        self._obs_layer = l_in

        LayersPowered.__init__(self, [l_output])
示例#5
0
    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.
        """
        with tf.variable_scope(name):
            l_in = layers.InputLayer(shape=(None, self._obs_dim), name='obs')

            l_hidden = l_in
            for idx, hidden_size in enumerate(self._hidden_sizes):
                if self._batch_norm:
                    l_hidden = batch_norm(l_hidden)

                l_hidden = layers.DenseLayer(
                    l_hidden,
                    hidden_size,
                    nonlinearity=self._hidden_nonlinearity,
                    trainable=trainable,
                    name='hidden_%d' % idx)

            l_output = layers.DenseLayer(
                l_hidden,
                self._action_dim,
                nonlinearity=self._output_nonlinearity,
                trainable=trainable,
                name='output')

            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=scaled_action)
        output_layer = l_output
        obs_layer = l_in

        return f_prob_online, output_layer, obs_layer
示例#6
0
文件: network.py 项目: xht033/garage
    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
示例#8
0
    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