Ejemplo n.º 1
0
def variable_scope(name_or_scope,
                   default_name=None,
                   values=None,
                   initializer=None,
                   regularizer=None,
                   reuse=None,
                   dtype=None):

    if default_name is None and name_or_scope is None:
        raise TypeError(
            "if default_name is None then name_or_scope is required")

    if values is None:
        values = []

    if name_or_scope is not None:
        if not isinstance(name_or_scope, (var_scope, ) + six.string_types):
            raise TypeError("variable_scope: name_or_scope must be a string or"
                            " var_scope.")
        if isinstance(name_or_scope, six.string_types):
            name_scope = name_or_scope
        else:
            name_scope = name_or_scope.name.split("/")[-1]

        if name_scope:
            with name_scope_op(name_scope) as cur_name_scope:
                if isinstance(name_or_scope, six.string_types):
                    old_name_scope = cur_name_scope
                else:
                    old_name_scope = name_or_scope.original_name_scope
            with _pure_variable_scope(name_or_scope,
                                      reuse=reuse,
                                      initializer=initializer,
                                      regularizer=regularizer,
                                      old_name_scope=old_name_scope,
                                      dtype=dtype) as vs:
                yield vs
        else:
            with _pure_variable_scope(name_or_scope,
                                      reuse=reuse,
                                      initializer=initializer,
                                      regularizer=regularizer,
                                      dtype=dtype) as vs:
                yield vs
    else:
        if reuse:
            raise ValueError("reuse=True cannot be used without a "
                             "name_or_scope")
        with name_scope_op(default_name) as scope:
            unique_default_name = _get_unique_variable_scope(default_name)
            with _pure_variable_scope(unique_default_name,
                                      initializer=initializer,
                                      regularizer=regularizer,
                                      old_name_scope=scope,
                                      dtype=dtype) as vs:
                yield vs
Ejemplo n.º 2
0
    def get_variable(self,
                     var_store,
                     name,
                     shape=None,
                     dtype=None,
                     initializer=None,
                     regularizer=None,
                     trainable=True):
        if regularizer is None:
            regularizer = self._regularizer

        full_name = self.name + "/" + name if self.name else name

        with name_scope_op(None):
            if (dtype is not None and initializer is not None
                    and not callable(initializer)):
                # initializer is a numpy object
                init_dtype = initializer.dtype
                if init_dtype != dtype:
                    raise ValueError("nitializer type '%s' and explicit"
                                     " dtype '%s'  don't match." %
                                     (init_dtype, dtype))

            if initializer is None:
                initializer = self._initializer
            if dtype is None:
                dtype = self._dtype

            return var_store.get_variable(full_name,
                                          shape=shape,
                                          dtype=dtype,
                                          reuse=self.reuse,
                                          trainable=trainable,
                                          initializer=initializer,
                                          regularizer=regularizer)
Ejemplo n.º 3
0
    def get_variable(self,
                     name,
                     shape=None,
                     dtype=None,
                     initializer=None,
                     regularizer=None,
                     reuse=None,
                     trainable=True):
        initializing_from_value = False

        if initializer is not None and not callable(initializer):
            initializing_from_value = True

        if shape is not None and initializing_from_value:
            raise ValueError("if initializer is a constant, "
                             "do not specify shape.")

        should_check = reuse is not None
        dtype = dtype or theano.config.floatX

        # name already defined
        if name in self._vars:
            if should_check and not reuse:
                raise ValueError("variable %s already exists, disallowed." %
                                 name)

            found_var = self._vars[name]
            found_shape = found_var.get_value().shape

            if not is_compatible_shape(shape, found_shape):
                raise ValueError("trying to share variable %s, "
                                 "but specified shape %s and found shape %s." %
                                 (name, shape, found_shape))

            if dtype and dtype != found_var.dtype:
                raise ValueError("trying to share variable %s, but specified "
                                 "dtype %s and found dtype %s." %
                                 (name, dtype, found_var.dtype))
            return found_var

        # creating a new variable
        if should_check and reuse:
            raise ValueError("variable %s does not exist, or was not created "
                             "with get_variable()." % name)

        # get default initializer
        if initializer is None:
            if is_floating_dtype(dtype):
                initializer = uniform_unit_scaling_initializer()
                initializing_from_value = False

            elif is_integer_dtype(dtype):
                initializer = zeros_initializer()
                initializer = initializer(shape=shape, dtype=dtype)
                initializing_from_value = True
            else:
                raise ValueError("a initializer for variable %s of %s "
                                 "is required" % (name, dtype))

        if initializing_from_value:
            init_val = initializer
        else:
            init_val = lambda: initializer(shape, dtype=dtype)

        # create variable
        v = variable(initial_value=init_val,
                     name=name,
                     trainable=trainable,
                     dtype=dtype)

        self._vars[name] = v

        if regularizer:
            with name_scope_op(name + "/regularizer/"):
                loss = regularizer(v)
                if loss is not None:
                    add_regularization_loss(loss)

        return v