使用张量流定义自定义密集层

时间:2019-12-11 15:48:09

标签: python tensorflow keras customization

我想在tensorflow中定义一个新的CustomLayer,以便记下自定义的前进和后退操作。 实际上,我已经根据Dense tensorflow层定义了一个CustomLayer。但是它不起作用,我坚持下去。 谁能帮我这个? 这是我代码的当前版本:

class CustomLayer(Layer):

def __init__(self,
             units,
             activation=None,
             use_bias=True,
             kernel_initializer='glorot_uniform',
             bias_initializer='zeros',
             kernel_regularizer=None,
             bias_regularizer=None,
             activity_regularizer=None,
             kernel_constraint=None,
             bias_constraint=None,
             dynamic=True,
             **kwargs):
    if 'input_shape' not in kwargs and 'input_dim' in kwargs:
        kwargs['input_shape'] = (kwargs.pop('input_dim'),)

    super(CustomLayer, self).__init__(activity_regularizer=regularizers.get(activity_regularizer), **kwargs)
    self.units = int(units)
    self.activation = activations.get(activation)
    self.use_bias = use_bias
    self.kernel_initializer = initializers.get(kernel_initializer)
    self.bias_initializer = initializers.get(bias_initializer)
    self.kernel_regularizer = regularizers.get(kernel_regularizer)
    self.bias_regularizer = regularizers.get(bias_regularizer)
    self.kernel_constraint = constraints.get(kernel_constraint)
    self.bias_constraint = constraints.get(bias_constraint)

    self.supports_masking = True
    self.input_spec = InputSpec(min_ndim=2)

def build(self, input_shape):
    dtype = dtypes.as_dtype(self.dtype or K.floatx())
    if not (dtype.is_floating or dtype.is_complex):
        raise TypeError('Unable to build Dense layer with non-floating point '
                        'dtype %s' % (dtype,))
    input_shape = tensor_shape.TensorShape(input_shape)
    if tensor_shape.dimension_value(input_shape[-1]) is None:
        raise ValueError('The last dimension of the inputs to Dense '
                         'should be defined. Found None.')
    last_dim = tensor_shape.dimension_value(input_shape[-1])
    self.input_spec = InputSpec(min_ndim=2,
                                axes={-1: last_dim})
    self.kernel = self.add_weight(
        'kernel',
        shape=[last_dim, self.units],
        initializer=self.kernel_initializer,
        regularizer=self.kernel_regularizer,
        constraint=self.kernel_constraint,
        dtype=self.dtype,
        trainable=True)
    if self.use_bias:
        self.bias = self.add_weight(
            'bias',
            shape=[self.units, ],
            initializer=self.bias_initializer,
            regularizer=self.bias_regularizer,
            constraint=self.bias_constraint,
            dtype=self.dtype,
            trainable=True)
    else:
        self.bias = None
    self.built = True

def call(self, inputs):
    global global_self
    global_self = self
    return custom_op(inputs)


global_self = None


@tf.custom_gradient
def custom_op(inputs):
   self = global_self
   rank = len(inputs.shape)
   if rank > 2:
    # Broadcasting is required for the inputs.
      outputs = standard_ops.tensordot(inputs, self.kernel, [[rank - 1], [0]])
    # Reshape the output back to the original ndim of the input.
      if not context.executing_eagerly():
         shape = inputs.shape.as_list()
         output_shape = shape[:-1] + [self.units]
         outputs.set_shape(output_shape)
  else:
    inputs = math_ops.cast(inputs, self._compute_dtype)
    if K.is_sparse(inputs):
        outputs = sparse_ops.sparse_tensor_dense_matmul(inputs, self.kernel)
    else:
        outputs = gen_math_ops.mat_mul(inputs, self.kernel)
  if self.use_bias:
    outputs = nn.bias_add(outputs, self.bias)
  if self.activation is not None:
    return self.activation(outputs)  # pylint: disable=not-callable

   def custom_grad(dy):
       print(dy, [dy])
       grad = dy  # compute gradient
       return grad
return outputs, custom_grad

当我尝试执行时,获取此回溯

Traceback (most recent call last):
File "/home/labt41/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 802, in call
outputs = call_fn(cast_inputs, *args, **kwargs)
File "/home/labt41/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py", line 237, in wrapper
raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: in converted code:
relative to /home/labt41:

PycharmProjects/TF_Custom/CustomLayer_stackoverflow.py:125 call  *
    return custom_op(inputs)
PycharmProjects/TF_Custom/CustomLayer_stackoverflow.py:136 decorated  *
    return _graph_mode_decorator(f, *args, **kwargs)
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/custom_gradient.py:229 _graph_mode_decorator
    result, grad_fn = f(*args)
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:547 iter
    self._disallow_iteration()
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:543 _disallow_iteration
    self._disallow_in_graph_mode("iterating over `tf.Tensor`")
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:523 _disallow_in_graph_mode
    " this function with @tf.function.".format(task))

OperatorNotAllowedInGraphError: iterating over tf.Tensor is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/labt41/PycharmProjects/TF_Custom/CustomLayer_stackoverflow.py", line 179, in <module>
model = create_model()
File "/home/labt41/PycharmProjects/TF_Custom/CustomLayer_stackoverflow.py", line 50, in create_model
layer = CustomLayer(units=128)(visible)
File "/home/labt41/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 814, in call
str(e) + '\n"""')
TypeError: You are attempting to use Python control flow in a layer that was not declared to be dynamic. Pass dynamic=True to the class constructor.
Encountered error:

in converted code:
    relative to /home/labt41:

PycharmProjects/TF_Custom/CustomLayer_stackoverflow.py:125 call  *
    return custom_op(inputs)
PycharmProjects/TF_Custom/CustomLayer_stackoverflow.py:136 decorated  *
    return _graph_mode_decorator(f, *args, **kwargs)
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/ops/custom_gradient.py:229 _graph_mode_decorator
    result, grad_fn = f(*args)
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:547 iter
    self._disallow_iteration()
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:543 _disallow_iteration
    self._disallow_in_graph_mode("iterating over `tf.Tensor`")
anaconda3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:523 _disallow_in_graph_mode
    " this function with @tf.function.".format(task))

OperatorNotAllowedInGraphError: iterating over tf.Tensor is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

0 个答案:

没有答案