guard

api_attr

imperative programming (dynamic graph)

paddle.fluid.dygraph.guard(place=None)[source]

This context will create a dygraph context for dygraph to run, using python with statement.

Parameters

place (fluid.CPUPlace or fluid.CUDAPlace, optional) – Place to execute dygraph. If None, the running place will be determined according to the way of paddle compilation. Default: None

Returns

None

Examples

import numpy as np
import paddle.fluid as fluid

with fluid.dygraph.guard():
    inp = np.ones([3, 1024], dtype='float32')
    t = fluid.dygraph.base.to_variable(inp)
    linear1 = fluid.Linear(1024, 4, bias_attr=False)
    linear2 = fluid.Linear(4, 4)
    ret = linear1(t)
    dy_ret = linear2(ret)