Exemple #1
0
    def tf_apply(self, x):
        if util.tf_dtype('int') not in (tf.int32, tf.int64):
            x = tf.dtypes.cast(x=x, dtype=tf.int32)
        elif util.dtype(x=x) == 'bool':
            x = tf.dtypes.cast(x=x, dtype=util.tf_dtype('int'))

        x = tf.nn.embedding_lookup(params=self.weights, ids=x, max_norm=self.max_norm)

        return super().tf_apply(x=x)
    def get_parameter_value(self, step):
        parameter = tf.keras.optimizers.schedules.PiecewiseConstantDecay(
            boundaries=self.boundaries, values=self.values)(step=step)

        if util.dtype(x=parameter) != self.dtype:
            parameter = tf.dtypes.cast(x=parameter,
                                       dtype=util.tf_dtype(dtype=self.dtype))

        return parameter
Exemple #3
0
    def get_parameter_value(self, step):
        parameter = tf.compat.v1.train.piecewise_constant(
            x=step, boundaries=self.boundaries, values=self.values)

        if util.dtype(x=parameter) != self.dtype:
            parameter = tf.dtypes.cast(x=parameter,
                                       dtype=util.tf_dtype(dtype=self.dtype))

        return parameter
Exemple #4
0
 def assign_variable(self, *, variable, value):
     if variable.startswith(self.name + '/'):
         variable = variable[len(self.name) + 1:]
     module = self
     scope = variable.split('/')
     for _ in range(len(scope) - 1):
         module = module.modules[scope.pop(0)]
     fetches = util.join_scopes(self.name, variable) + '-assign'
     dtype = util.dtype(x=module.variables[scope[0]])
     feed_dict = {util.join_scopes(self.name, 'assignment-') + dtype + '-input:0': value}
     self.monitored_session.run(fetches=fetches, feed_dict=feed_dict)
Exemple #5
0
    def add_placeholder(self, name, dtype, shape, batched, default=None):
        # name
        name = name + '-input'
        if not util.is_valid_name(name=name):
            raise TensorforceError.value(name='placeholder',
                                         argument='name',
                                         value=name)
        # dtype
        if not util.is_valid_type(dtype=dtype):
            raise TensorforceError.value(name='placeholder',
                                         argument='dtype',
                                         value=dtype)
        # shape
        if not util.is_iterable(x=shape) or \
                not all(isinstance(num_dims, int) for num_dims in shape):
            raise TensorforceError.type(name='placeholder',
                                        argument='shape',
                                        value=shape)
        elif not all(num_dims > 0 for num_dims in shape):
            raise TensorforceError.value(name='placeholder',
                                         argument='shape',
                                         value=shape)
        # batched
        if not isinstance(batched, bool):
            raise TensorforceError.type(name='placeholder',
                                        argument='batched',
                                        value=batched)
        # default
        if default is not None:
            if batched:
                raise TensorforceError.unexpected()
            elif not isinstance(default, tf.Tensor):
                raise TensorforceError.unexpected()
            elif util.dtype(x=default) != dtype:
                raise TensorforceError.unexpected()

        # Placeholder
        if batched:
            shape = (None, ) + shape
        if default is None:
            dtype = util.tf_dtype(dtype=dtype)
            placeholder = tf.placeholder(dtype=dtype, shape=shape, name=name)
        else:
            # check dtype and shape !!!
            placeholder = tf.placeholder_with_default(input=default,
                                                      shape=shape,
                                                      name=name)

        return placeholder
    def get_parameter_value(self):
        if self.unit == 'timesteps':
            step = Module.retrieve_tensor(name='timestep')
        elif self.unit == 'episodes':
            step = Module.retrieve_tensor(name='episode')

        parameter = tf.train.piecewise_constant(x=step,
                                                boundaries=self.boundaries,
                                                values=self.values)

        if util.dtype(x=parameter) != self.dtype:
            parameter = tf.dtypes.cast(x=parameter,
                                       dtype=util.tf_dtype(dtype=self.dtype))

        return parameter
Exemple #7
0
    def add_variable(
        self, name, dtype, shape, is_trainable, initializer='zeros', is_saved=True, summarize=None,
        shared=None
    ):
        # name
        if not util.is_valid_name(name=name):
            raise TensorforceError.value(name='Module.add_variable', argument='name', value=name)
        elif name in self.variables:
            raise TensorforceError.exists(name='variable', value=name)
        # dtype
        if not util.is_valid_type(dtype=dtype):
            raise TensorforceError.value(name='Module.add_variable', argument='dtype', value=dtype)
        # shape
        if not util.is_iterable(x=shape) or not all(isinstance(dims, int) for dims in shape):
            raise TensorforceError.value(name='Module.add_variable', argument='shape', value=shape)
        elif not all(dims > 0 for dims in shape):
            raise TensorforceError.value(name='Module.add_variable', argument='shape', value=shape)
        # is_trainable
        if not isinstance(is_trainable, bool):
            raise TensorforceError.type(
                name='Module.add_variable', argument='is_trainable', dtype=type(is_trainable)
            )
        elif is_trainable and dtype != 'float':
            raise TensorforceError.value(
                name='Module.add_variable', argument='is_trainable', value=is_trainable,
                condition='dtype != float'
            )
        # initializer
        initializer_names = (
            'normal', 'normal-relu', 'orthogonal', 'orthogonal-relu', 'zeros', 'ones'
        )
        if not isinstance(initializer, (util.py_dtype(dtype=dtype), np.ndarray, tf.Tensor)) and \
                initializer not in initializer_names:
            raise TensorforceError.value(
                name='Module.add_variable', argument='initializer', value=initializer
            )
        elif isinstance(initializer, np.ndarray) and \
                initializer.dtype != util.np_dtype(dtype=dtype):
            raise TensorforceError.type(
                name='Module.add_variable', argument='initializer', dtype=type(initializer)
            )
        elif isinstance(initializer, tf.Tensor) and util.dtype(x=initializer) != dtype:
            raise TensorforceError.type(
                name='Module.add_variable', argument='initializer', dtype=type(initializer)
            )
        # is_saved
        if not isinstance(is_saved, bool):
            raise TensorforceError.type(
                name='Module.add_variable', argument='is_saved', dtype=type(is_saved)
            )
        # summarize
        if summarize is not None and not isinstance(summarize, bool):
            raise TensorforceError.type(
                name='Module.add_variable', argument='summarize', dtype=type(summarize)
            )
        # shared
        if shared is not None and not isinstance(shared, str):
            raise TensorforceError.type(
                name='Module.add_variable', argument='shared',dtype=type(shared)
            )

        variable = None

        if shared is not None and len(self.graph.get_collection(name=shared)) > 0:
            # Retrieve shared variable from TensorFlow
            collection = self.graph.get_collection(name=shared)
            if len(collection) > 1:
                raise TensorforceError.unexpected()
            variable = collection[0]

        else:
            tf_dtype = util.tf_dtype(dtype=dtype)

            # Variable initializer
            if isinstance(initializer, util.py_dtype(dtype=dtype)):
                initializer = tf.constant(value=initializer, dtype=tf_dtype, shape=shape)
            elif isinstance(initializer, np.ndarray):
                if initializer.shape != shape:
                    raise TensorforceError.mismatch(
                        name='Module.add_variable', value1='shape', value2='initializer'
                    )
                initializer = tf.constant(value=initializer, dtype=tf_dtype)
            elif isinstance(initializer, tf.Tensor):
                if util.shape(x=initializer) != shape:
                    raise TensorforceError.mismatch(
                        name='Module.add_variable', value1='shape', value2='initializer'
                    )
                initializer = initializer
            elif not isinstance(initializer, str):
                raise TensorforceError("Invalid variable initializer: {}".format(initializer))
            elif initializer[:6] == 'normal':
                if dtype != 'float':
                    raise TensorforceError(
                        message="Invalid variable initializer value for non-float variable: {}.".format(
                            initializer
                        )
                    )
                if initializer[6:] == '-relu':
                    stddev = min(0.1, sqrt(2.0 / util.product(xs=shape[:-1])))
                else:
                    stddev = min(0.1, sqrt(2.0 / (util.product(xs=shape[:-1]) + shape[-1])))
                initializer = tf.random.normal(shape=shape, stddev=stddev, dtype=tf_dtype)
            elif initializer[:10] == 'orthogonal':
                if dtype != 'float':
                    raise TensorforceError(
                        message="Invalid variable initializer value for non-float variable: {}.".format(
                            initializer
                        )
                    )
                if len(shape) < 2:
                    raise TensorforceError(
                        message="Invalid variable initializer value for 0/1-rank variable: {}.".format(
                            initializer
                        )
                    )
                normal = np.random.normal(size=(util.product(xs=shape[:-1]), shape[-1]))
                u, _, v = np.linalg.svd(a=normal, full_matrices=False)
                orthogonal = u if u.shape[1] == shape[-1] else v
                if initializer[10:] == '-relu':
                    orthogonal = orthogonal * sqrt(2.0)
                initializer = tf.constant(value=orthogonal.reshape(shape), dtype=tf_dtype)
            elif initializer == 'zeros':
                initializer = tf.zeros(shape=shape, dtype=tf_dtype)
            elif initializer == 'ones':
                initializer = tf.ones(shape=shape, dtype=tf_dtype)

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

            # Register shared variable with TensorFlow
            if shared is not None:
                self.graph.add_to_collection(name=shared, value=variable)

        # Register variable
        self.variables[name] = variable
        if is_trainable:
            self.trainable_variables[name] = variable
        if is_saved:
            self.saved_variables[name] = variable

        # Add summary
        if (summarize is None and is_trainable) or summarize:
            variable = self.add_summary(
                label='variables', name=name, tensor=variable, mean_variance=True
            )
            variable = self.add_summary(label='variables-histogram', name=name, tensor=variable)

        return variable
Exemple #8
0
    def add_variable(self,
                     name,
                     dtype,
                     shape,
                     is_trainable,
                     initializer='zeros',
                     summarize=None,
                     shared=None):
        # name
        if not util.is_valid_name(name=name):
            raise TensorforceError.value(name='variable',
                                         argument='name',
                                         value=name)
        elif name in self.variables:
            raise TensorforceError.exists(name='variable', value=name)
        # dtype
        if not util.is_valid_type(dtype=dtype):
            raise TensorforceError.value(name='variable',
                                         argument='dtype',
                                         value=dtype)
        # shape
        if not util.is_iterable(x=shape) or \
                not all(isinstance(num_dims, int) for num_dims in shape):
            raise TensorforceError.type(name='variable',
                                        argument='shape',
                                        value=shape)
        elif not all(num_dims > 0 for num_dims in shape):
            raise TensorforceError.value(name='variable',
                                         argument='shape',
                                         value=shape)
        # is_trainable
        if not isinstance(is_trainable, bool):
            raise TensorforceError.type(name='variable',
                                        argument='is_trainable',
                                        value=is_trainable)
        # initializer
        if not isinstance(initializer, (util.py_dtype(dtype=dtype), np.ndarray, tf.Tensor)) and \
                initializer not in ('random', 'zeros', 'ones'):
            raise TensorforceError.value(name='variable',
                                         argument='initializer',
                                         value=initializer)
        elif isinstance(initializer, np.ndarray) and \
                initializer.dtype != util.np_dtype(dtype=dtype):
            raise TensorforceError.type(name='variable',
                                        argument='initializer',
                                        value=initializer)
        elif isinstance(initializer,
                        tf.Tensor) and util.dtype(x=initializer) != dtype:
            raise TensorforceError.type(name='variable',
                                        argument='initializer',
                                        value=initializer)
        elif isinstance(initializer,
                        str) and initializer == 'random' and dtype != 'float':
            raise TensorforceError(
                message=
                "Invalid variable initializer value for non-float variable: {}."
                .format(initializer))
        # summarize
        if summarize is not None and not isinstance(summarize, bool):
            raise TensorforceError.type(name='variable',
                                        argument='summarize',
                                        value=summarize)
        # shared
        if shared is not None and not isinstance(shared, str):
            raise TensorforceError.type(name='variable',
                                        argument='shared',
                                        value=shared)

        variable = None

        if shared is not None and len(tf.get_collection(key=shared)) > 0:
            # Retrieve shared variable from TensorFlow
            collection = tf.get_collection(key=shared)
            if len(collection) > 1:
                raise TensorforceError.unexpected()
            variable = collection[0]

        else:
            tf_dtype = util.tf_dtype(dtype=dtype)

            # Variable initializer
            if isinstance(initializer, util.py_dtype(dtype=dtype)):
                initializer = tf.constant(value=initializer,
                                          dtype=tf_dtype,
                                          shape=shape)
            elif isinstance(initializer, np.ndarray):
                if initializer.shape != shape:
                    raise TensorforceError(
                        "Invalid variable initializer shape: {}.".format(
                            initializer.shape))
                initializer = initializer
            elif isinstance(initializer, tf.Tensor):
                if util.shape(x=initializer) != shape:
                    raise TensorforceError(
                        "Invalid variable initializer shape: {}.".format(
                            util.shape(x=initializer)))
                initializer = initializer
            elif not isinstance(initializer, str):
                raise TensorforceError(
                    "Invalid variable initializer: {}".format(initializer))
            elif initializer == 'random':
                stddev = min(
                    0.1, sqrt(2.0 / (util.product(xs=shape[:-1]) + shape[-1])))
                initializer = tf.random_normal(
                    shape=shape,
                    mean=0.0,
                    stddev=stddev,
                    dtype=util.tf_dtype(dtype=dtype))
            elif initializer == 'zeros':
                initializer = tf.zeros(shape=shape, dtype=tf_dtype)
            elif initializer == 'ones':
                initializer = tf.ones(shape=shape, dtype=tf_dtype)

            # Variable
            variable = tf.Variable(initial_value=initializer,
                                   trainable=is_trainable,
                                   validate_shape=True,
                                   name=name,
                                   dtype=tf_dtype,
                                   expected_shape=shape)  # collections=

            # Register shared variable with TensorFlow
            if shared is not None:
                tf.add_to_collection(name=shared, value=variable)

        # Register variable
        self.variables[name] = variable
        if is_trainable:
            self.trainable_variables[name] = variable

        # Add summary
        if (summarize is None and is_trainable) or summarize:
            variable = tf.identity(input=variable)
            variable = self.add_summary(label='variables',
                                        name=name,
                                        tensor=variable,
                                        mean_variance=True)

        return variable