Esempio n. 1
0
  def get_config(self):
    module = self.function.__module__
    if isinstance(self.function, python_types.LambdaType):
      function = generic_utils.func_dump(self.function)
      function_type = 'lambda'
    else:
      function = self.function.__name__
      function_type = 'function'

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

    config = {
        'function': function,
        'module': module,
        'function_type': function_type,
        'output_shape': output_shape,
        'output_shape_type': output_shape_type,
        'output_shape_module': output_shape_module,
        'arguments': self.arguments
    }
    base_config = super(Lambda, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))
Esempio n. 2
0
  def get_config(self):
    module = self.function.__module__
    if isinstance(self.function, python_types.LambdaType):
      function = generic_utils.func_dump(self.function)
      function_type = 'lambda'
    else:
      function = self.function.__name__
      function_type = 'function'

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

    config = {
        'function': function,
        'module': module,
        'function_type': function_type,
        'output_shape': output_shape,
        'output_shape_type': output_shape_type,
        'output_shape_module': output_shape_module,
        'arguments': self.arguments
    }
    base_config = super(Lambda, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))
Esempio n. 3
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
Esempio n. 4
0
    def get_config(self):
        configs = super(Network, self).get_config()

        fn = self.fn_layer_creator
        if isinstance(fn, python_types.LambdaType):
            fn = (generic_utils.func_dump(fn), 'lambda', fn.__module__)
        elif callable(fn):
            fn = (fn.__name__, 'function', fn.__module__)

        configs.update({
            'fn_layer_creator': fn,
            'delay_context': self.delay_context,
            'pooling': self.pooling,
        })
        return configs
Esempio n. 5
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
Esempio n. 6
0
def serialize_function(func):
    """Serializes function for Keras.

  (De)serializing Python functions from/to bytecode is unsafe. Therefore we
  return the function's type as an anonymous function ('lambda') or named
  function in the Python environment ('function'). In the latter case, this lets
  us use the Python scope to obtain the function rather than reload it from
  bytecode. (Note that both cases are brittle!)

  This serialization mimicks the implementation in `tf.keras.layers.Lambda`.

  Args:
    func: Python function to serialize.

  Returns:
    (serial, function_type): Serialized object, which is a tuple of its
    bytecode (if function is anonymous) or name (if function is named), and its
    function type.
  """
    if isinstance(func, types.LambdaType):
        return generic_utils.func_dump(func), 'lambda'
    return func.__name__, 'function'
Esempio n. 7
0
def serialize_function(func):
  """Serializes function for Keras.

  (De)serializing Python functions from/to bytecode is unsafe. Therefore we
  return the function's type as an anonymous function ('lambda') or named
  function in the Python environment ('function'). In the latter case, this lets
  us use the Python scope to obtain the function rather than reload it from
  bytecode. (Note that both cases are brittle!)

  This serialization mimicks the implementation in `tf.keras.layers.Lambda`.

  Args:
    func: Python function to serialize.

  Returns:
    (serial, function_type): Serialized object, which is a tuple of its
    bytecode (if function is anonymous) or name (if function is named), and its
    function type.
  """
  if isinstance(func, types.LambdaType):
    return generic_utils.func_dump(func), 'lambda'
  return func.__name__, 'function'