function

dragon.function(
  func=None,
  input_signature=None
)[source]

Compile a function and return an executable.

Only the tensor operations could be compiled:

def foo(x, y):
    return dragon.math.add([x + y, x])

bar = dragon.function(foo)
print(bar(1, 2))
print(bar(dragon.constant([1, 2]), dragon.constant([2, 3])))

Above usages which can simplified as follows:

@dragon.function
def foo(x, y):
    return dragon.math.add([x + y, x])

print(foo(1, 2))
print(foo(dragon.constant([1, 2]), dragon.constant([2, 3])))

Tensor shape and dtype will be required sometimes:

@dragon.function(input_signature=[
    dragon.TensorSpec(shape=[], dtype='float32'),
    dragon.TensorSpec(shape=[], dtype='float32')
])
def foo(x, y):
    return dragon.math.add([x + y, x])

print(foo(1, 2))
Parameters:
  • func (callable, optional) – The function to be compiled.
  • input_signature (Sequence[dragon.TensorSpec], optional) – The specs to hint the input info.
Returns:

callable – A callable to execute the compiled function.