Exemplo n.º 1
0
    def serialize_one(activation):
        if isinstance(activation, six.string_types):
            return activation

        if isinstance(activation, keras.engine.Layer):  # Advanced activation
            return serialize_keras_object(activation)

        # The order matters here, since Layers are also callable.
        if callable(activation):  # A function
            return func_dump(activation)

        # Keras serialized config
        if isinstance(activation, dict) \
                and "class_name" in activation \
                and "config" in activation:
            return activation

        # Could be a marshalled function
        if isinstance(activation, (list, tuple)) \
                and len(activation) == 3 \
                and isinstance(activation[0], six.string_types):
            try:
                # TODO: Better way to check if it is a marshalled function!
                func_load(activation)  # Try to unmarshal it

                return activation

            except ValueError:
                pass

        return None
def test_func_dump_and_load(test_function_type):

    if test_function_type == 'simple function':

        def test_func():
            return r'\u'

    elif test_function_type == 'closured function':

        def get_test_func():
            x = r'\u'

            def test_func():
                return x

            return test_func

        test_func = get_test_func()
    else:
        raise Exception('Unknown test case for test_func_dump_and_load')

    serialized = func_dump(test_func)
    deserialized = func_load(serialized)
    assert deserialized.__code__ == test_func.__code__
    assert deserialized.__defaults__ == test_func.__defaults__
    assert deserialized.__closure__ == test_func.__closure__
def test_func_dump_and_load_closure():
    y = 0
    test_func = lambda x: x + y
    serialized, _, closure = func_dump(test_func)
    deserialized = func_load(serialized, closure=closure)
    assert deserialized.__code__ == test_func.__code__
    assert deserialized.__defaults__ == test_func.__defaults__
    assert deserialized.__closure__ == test_func.__closure__
Exemplo n.º 4
0
def test_func_dump_and_load_closure():
    y = 0
    test_func = lambda x: x + y
    serialized, _, closure = func_dump(test_func)
    deserialized = func_load(serialized, closure=closure)
    assert deserialized.__code__ == test_func.__code__
    assert deserialized.__defaults__ == test_func.__defaults__
    assert deserialized.__closure__ == test_func.__closure__
Exemplo n.º 5
0
def test_func_dump_and_load():
    def test_func():
        return r'\u'
    serialized = func_dump(test_func)
    deserialized = func_load(serialized)
    assert deserialized.__code__ == test_func.__code__
    assert deserialized.__defaults__ == test_func.__defaults__
    assert deserialized.__closure__ == test_func.__closure__
Exemplo n.º 6
0
def test_func_dump_and_load():
    def test_func():
        return r'\u'

    serialized = func_dump(test_func)
    deserialized = func_load(serialized)
    assert deserialized.__code__ == test_func.__code__
    assert deserialized.__defaults__ == test_func.__defaults__
    assert deserialized.__closure__ == test_func.__closure__
Exemplo n.º 7
0
    def get_config(self):
        if isinstance(self.mode, python_types.LambdaType):
            mode = func_dump(self.mode)
            mode_type = 'lambda'
        elif callable(self.mode):
            mode = self.mode.__name__
            mode_type = 'function'
        else:
            mode = self.mode
            mode_type = 'raw'

        if isinstance(self._output_shape, python_types.LambdaType):
            output_shape = func_dump(self._output_shape)
            output_shape_type = 'lambda'
        elif callable(self._output_shape):
            output_shape = self._output_shape.__name__
            output_shape_type = 'function'
        else:
            output_shape = self._output_shape
            output_shape_type = 'raw'

        if isinstance(self._output_mask, python_types.LambdaType):
            output_mask = func_dump(self._output_mask)
            output_mask_type = 'lambda'
        elif callable(self._output_mask):
            output_mask = self._output_mask.__name__
            output_mask_type = 'function'
        else:
            output_mask = self._output_mask
            output_mask_type = 'raw'

        return {
            'name': self.name,
            'mode': mode,
            'mode_type': mode_type,
            'concat_axis': self.concat_axis,
            'dot_axes': self.dot_axes,
            'output_shape': output_shape,
            'output_shape_type': output_shape_type,
            'output_mask': output_mask,
            'output_mask_type': output_mask_type,
            'arguments': self.arguments
        }
Exemplo n.º 8
0
def _serialize_function_to_config(function):
  """Serialize the function for get_config()."""
  if isinstance(function, python_types.LambdaType):
    output = generic_utils.func_dump(function)
    output_type = "lambda"
    module = function.__module__
  elif callable(function):
    output = function.__name__
    output_type = "function"
    module = function.__module__
  else:
    raise ValueError("Unrecognized function type for input: {}".format(
        type(function)))

  return output, output_type, module
Exemplo n.º 9
0
    def _serialize_function_to_config(self, inputs, allow_raw=False):
        if isinstance(inputs, python_types.LambdaType):
            output = generic_utils.func_dump(inputs)
            output_type = 'lambda'
            module = inputs.__module__
        elif callable(inputs):
            output = inputs.__name__
            output_type = 'function'
            module = inputs.__module__
        elif allow_raw:
            output = inputs
            output_type = 'raw'
            module = None
        else:
            raise ValueError('Invalid input for serialization, type: %s ' %
                             type(inputs))

        return output, output_type, module
Exemplo n.º 10
0
def test_func_dump_and_load(test_function_type):

    if test_function_type == 'simple function':
        def test_func():
            return r'\u'

    elif test_function_type == 'closured function':
        def get_test_func():
            x = r'\u'

            def test_func():
                return x
            return test_func
        test_func = get_test_func()
    else:
        raise Exception('Unknown test case for test_func_dump_and_load')

    serialized = func_dump(test_func)
    deserialized = func_load(serialized)
    assert deserialized.__code__ == test_func.__code__
    assert deserialized.__defaults__ == test_func.__defaults__
    assert deserialized.__closure__ == test_func.__closure__