def get_config(self):
        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'

        if isinstance(self._output_shape, python_types.LambdaType):
            output_shape = generic_utils.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'

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

    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'

    config = {
        'function': function,
        'function_type': function_type,
        'output_shape': output_shape,
        'output_shape_type': output_shape_type,
        'arguments': self.arguments
    }
    base_config = super(Lambda, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))
示例#3
0
  def get_config(self):
    if isinstance(self.function, python_types.LambdaType):
      function = func_dump(self.function)
      function_type = 'lambda'
    else:
      function = self.function.__name__
      function_type = 'function'

    config = {
        'function': function,
        'function_type': function_type,
        'arguments': self.arguments
    }
    base_config = super(Lambda, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))
示例#4
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'