def relaxed_value_for_var(value, var, ignore_mismatch=False):
        """
        Returns a relaxed (possibly reshaped/upcast-ed) version of value,
        to be loaded to the given variable.

        Args:
            value (ndarray): an numpy array to be loaded to var
            var (tf.Variable):
            ignore_mismatch (bool): ignore failures when the value and the
                variable does not match.

        Returns:
            ndarray: a possibly reshaped or casted version of value.
            Returns None if `ignore_mismatch==True` and the value and the variable
            mismatch.
        """
        assert isinstance(var, tf.Variable)
        name = var.op.name

        # check incompatible shape
        varshape = tuple(var.get_shape().as_list())
        if varshape != value.shape:
            if np.prod(varshape) != np.prod(value.shape):
                if ignore_mismatch:
                    logger.warn(
                        "Cannot load an array of shape {} into variable '{}' whose shape is {}.".format(
                            value.shape, name, varshape))
                    return None
                else:
                    raise ValueError(
                        "Trying to load an array of shape {} into variable '{}' whose shape is {}.".format(
                            value.shape, name, varshape))
            # TODO only allow reshape when shape different by empty axis
            logger.warn("The tensor is reshaped from {} to {} when assigned to '{}'".format(
                value.shape, varshape, name))
            value = value.reshape(varshape)

        # Be permissive, and allow some common type incompatibility problems
        def allow_cast(to_type, from_type):
            # to_type: a tf dtype
            # from_type: a numpy dtype
            from_type = tf.as_dtype(from_type)

            # allow up/down casting between floating points
            if from_type.is_floating and to_type.is_floating:
                return True

            if from_type.is_integer and to_type.is_integer:
                # only allow up-casting between integers
                if to_type.min <= from_type.min and to_type.max >= from_type.max:
                    return True
            return False

        if hasattr(value, 'dtype'):
            vartype = var.dtype.as_numpy_dtype
            if vartype != value.dtype:
                msg = "Variable {} has dtype {} but was given a value of dtype {}.".format(name, var.dtype, value.dtype)

                if allow_cast(var.dtype.base_dtype, value.dtype):
                    value = vartype(value)
                    logger.warn(msg + " The value will be loaded after casting!")
                else:
                    assert vartype == value.dtype, msg
        return value

        Returns a relaxed (possibly reshaped/upcast-ed) version of value,
        to be loaded to the given variable.

        Args:
            value (ndarray): an numpy array to be loaded to var
            var (tf.Variable):
            ignore_mismatch (bool): ignore failures when the value and the
                variable does not match.

        Returns:
            ndarray: a possibly reshaped or casted version of value.
            Returns None if `ignore_mismatch==True` and the value and the variable
            mismatch.
        
tensorpack/tfutils/varmanip.py