예제 #1
0
    def initialize_given_variables(self, *, variables):
        assert not self.root.is_initialized and not self.is_initialized_given_variables

        for module in self.this_submodules:
            if isinstance(module, Optimizer):
                module.initialize_given_variables(variables=variables)

        # Replace "/" with "_" to ensure TensorDict is flat
        self.variables_spec = TensorsSpec(
            ((var.name[:-2].replace('/', '_'),
              TensorSpec(type=tf_util.dtype(x=var, fallback_tf_dtype=True),
                         shape=tf_util.shape(x=var))) for var in variables))

        self.is_initialized_given_variables = True

        if self.config.create_debug_assertions:
            self.is_initialized = False
            for variable in variables:
                self.zero_check_history = self.variable(
                    name='zero_check_history',
                    spec=TensorSpec(type='bool', shape=(3, len(variables))),
                    initializer='zeros',
                    is_trainable=False,
                    is_saved=False)
                self.zero_check_index = self.variable(
                    name='zero_check_index',
                    spec=TensorSpec(type='int', shape=()),
                    initializer='zeros',
                    is_trainable=False,
                    is_saved=False)
            self.is_initialized = True
예제 #2
0
    def initialize_given_variables(self, *, variables, register_summaries):
        super().initialize_given_variables(
            variables=variables, register_summaries=register_summaries)

        values_spec = TensorsSpec(((var.name,
                                    TensorSpec(type=tf_util.dtype(x=var),
                                               shape=tf_util.shape(x=var)))
                                   for var in variables))
        self.line_search.complete_initialize(
            arguments_spec=self.arguments_spec, values_spec=values_spec)
예제 #3
0
    def initialize_given_variables(self, *, variables):
        assert not self.root.is_initialized and not self.is_initialized_given_variables

        for module in self.this_submodules:
            if isinstance(module, Optimizer):
                module.initialize_given_variables(variables=variables)

        # Replace "/" with "_" to ensure TensorDict is flat
        self.variables_spec = TensorsSpec(
            ((var.name[:-2].replace('/', '_'),
              TensorSpec(type=tf_util.dtype(x=var, fallback_tf_dtype=True),
                         shape=tf_util.shape(x=var))) for var in variables))

        self.is_initialized_given_variables = True
예제 #4
0
    def variable(self,
                 *,
                 name,
                 spec,
                 initializer,
                 is_trainable,
                 is_saved,
                 initialization_scale=None):
        assert self.is_initialized is False
        # name
        if not isinstance(name, str):
            raise TensorforceError.type(name='variable',
                                        argument='name',
                                        dtype=type(name))
        # spec
        if not isinstance(spec, TensorSpec):
            raise TensorforceError.dtype(name='variable',
                                         argument='spec',
                                         dtype=type(spec))
        if spec.is_underspecified():
            raise TensorforceError.value(name='variable',
                                         argument='spec',
                                         value=spec,
                                         hint='underspecified')
        # initializer
        initializer_names = ('constant', 'normal', 'normal-relu', 'ones',
                             'orthogonal', 'orthogonal-relu', 'zeros')
        if not isinstance(initializer, (spec.py_type(), np.ndarray, tf.Tensor)) and \
                initializer not in initializer_names:
            raise TensorforceError.value(name='variable',
                                         argument='initializer',
                                         value=initializer)
        elif isinstance(initializer,
                        np.ndarray) and initializer.dtype != spec.np_type():
            raise TensorforceError.type(name='variable',
                                        argument='initializer',
                                        dtype=initializer.dtype)
        elif isinstance(
                initializer,
                tf.Tensor) and tf_util.dtype(x=initializer) != spec.tf_type():
            raise TensorforceError.type(name='variable',
                                        argument='initializer',
                                        dtype=tf_util.dtype(x=initializer))
        # initialization_scale
        if initialization_scale is not None:
            if isinstance(initializer, (spec.py_type(), np.ndarray, tf.Tensor)) or \
                    initializer not in ('constant', 'orthogonal', 'orthogonal-relu'):
                raise TensorforceError.invalid(
                    name='variable',
                    argument='initialization_scale',
                    condition='initializer not orthogonal')
            elif not isinstance(initialization_scale, spec.py_type()):
                raise TensorforceError.type(name='variable',
                                            argument='initialization_scale',
                                            dtype=type(initialization_scale),
                                            hint='!= float')
        # is_trainable
        if not isinstance(is_trainable, bool):
            raise TensorforceError.type(name='variable',
                                        argument='is_trainable',
                                        dtype=type(is_trainable))
        elif is_trainable and spec.type != 'float':
            raise TensorforceError.value(name='variable',
                                         argument='is_trainable',
                                         value=is_trainable,
                                         condition='spec.type != float')
        # is_saved
        if not isinstance(is_saved, bool):
            raise TensorforceError.type(name='variable',
                                        argument='is_saved',
                                        dtype=type(is_saved))

        # Variable initializer
        if isinstance(initializer, spec.py_type()):
            initializer = tf_util.constant(value=initializer,
                                           dtype=spec.type,
                                           shape=spec.shape)
        elif isinstance(initializer, np.ndarray):
            if initializer.shape != spec.shape:
                raise TensorforceError.mismatch(name='Module.variable',
                                                value1='shape',
                                                value2='initializer')
            initializer = tf_util.constant(value=initializer, dtype=spec.type)
        elif isinstance(initializer, tf.Tensor):
            if tf_util.shape(x=initializer) != spec.shape:
                raise TensorforceError.mismatch(name='Module.variable',
                                                value1='shape',
                                                value2='initializer')
            initializer = initializer
        elif not isinstance(initializer, str):
            raise TensorforceError(
                "Invalid variable initializer: {}".format(initializer))
        elif initializer.startswith('normal'):
            if spec.type != 'float':
                raise TensorforceError(
                    message=
                    "Invalid variable initializer value for non-float variable: {}."
                    .format(initializer))
            if initializer.endswith('-relu'):
                stddev = min(0.1,
                             np.sqrt(2.0 / util.product(xs=spec.shape[:-1])))
            else:
                stddev = min(
                    0.1,
                    np.sqrt(
                        2.0 /
                        (util.product(xs=spec.shape[:-1]) + spec.shape[-1])))
            initializer = tf.random.normal(shape=spec.shape,
                                           stddev=stddev,
                                           dtype=spec.tf_type())
        elif initializer.startswith('orthogonal'):
            if spec.type != 'float':
                raise TensorforceError(
                    message=
                    "Invalid variable initializer value for non-float variable: {}."
                    .format(initializer))
            if spec.rank < 2:
                raise TensorforceError(
                    message=
                    "Invalid variable initializer value for 0/1-rank variable: {}."
                    .format(initializer))
            normal = np.random.normal(size=(util.product(xs=spec.shape[:-1]),
                                            spec.shape[-1]))
            u, _, v = np.linalg.svd(a=normal, full_matrices=False)
            orthogonal = u if u.shape[1] == spec.shape[-1] else v
            if initializer.endswith('-relu'):
                orthogonal = orthogonal * np.sqrt(2.0)
            if initialization_scale is not None and initialization_scale != 1.0:
                if initialization_scale <= 0.0:
                    raise TensorforceError.value(
                        name='variable',
                        argument='initialization_scale',
                        value=initialization_scale,
                        hint='<= 0.0')
                orthogonal = orthogonal * initialization_scale
            initializer = tf_util.constant(value=orthogonal.reshape(
                spec.shape),
                                           dtype=spec.type)
        elif initializer == 'zeros':
            initializer = tf_util.zeros(shape=spec.shape, dtype=spec.type)
        elif initializer == 'ones':
            initializer = tf_util.ones(shape=spec.shape, dtype=spec.type)
        elif initializer == 'constant':
            initializer = tf.fill(dims=spec.shape,
                                  value=tf_util.constant(
                                      value=initialization_scale,
                                      dtype=spec.type))

        # Variable
        variable = tf.Variable(initial_value=initializer,
                               trainable=is_trainable,
                               validate_shape=True,
                               name=name,
                               dtype=spec.tf_type(),
                               shape=spec.shape)
        variable.is_saved = is_saved

        return variable