create_function

dragon.create_function(
  inputs=None,
  outputs=None,
  givens=None,
  optimizer=None
)[source]

Create a callable graph from specified outputs.

Tensors that catch any operators can be used to create a graph:

x = dragon.Tensor(dtype='float32').constant()
y = x * 2
f = dragon.create_function(outputs=y)

The created graph will be executed once the function is called:

x.set_value(numpy.ones((2, 3)))
print(f())

Specify inputs to feed values implicitly before graph executing:

f = dragon.create_function(inputs=x, outputs=y)
print(f(numpy.ones((2, 3)))

Specify givens to substitute tensors before creating:

x = dragon.Tensor(dtype='float32').constant()
y = x * 2
foo = dragon.create_function(outputs=y)

# "bar" takes "x2" as input, and also writes to "y"
x2 = dragon.Tensor(dtype='float32').constant()
bar = dragon.create_function(outputs=y, givens={x: x2})

Specify optimizer to make a graph applying parameter updates:

x = dragon.Tensor(dtype='float32').set_value(1)
x_grad = dragon.Tensor(dtype='float32').set_value(1)

optimizer = dragon.optimizers.SGD(base_lr=0.01)
optimizer.apply_gradients(values_and_grads=[(x, x_grad)])

# Compute x -= 0.01 * x_grad
train_step = dragon.create_function(optimizer=optimizer)
train_step()
print(x.get_value())  # 0.99
Parameters:
Returns:

callable – The callable function.