本文整理汇总了Python中caffe2.python.workspace.RunOperatorOnce方法的典型用法代码示例。如果您正苦于以下问题:Python workspace.RunOperatorOnce方法的具体用法?Python workspace.RunOperatorOnce怎么用?Python workspace.RunOperatorOnce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类caffe2.python.workspace
的用法示例。
在下文中一共展示了workspace.RunOperatorOnce方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_add5_and_add5gradient_op
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunOperatorOnce [as 别名]
def run_add5_and_add5gradient_op(device):
# clear the workspace before running the operator
workspace.ResetWorkspace()
add5 = core.CreateOperator("Add5",
["X"],
["Y"],
device_option=device)
print("==> Running Add5 op:")
workspace.FeedBlob("X", (np.random.rand(5, 5)), device_option=device)
print("Input of Add5: ", workspace.FetchBlob("X"))
workspace.RunOperatorOnce(add5)
print("Output of Add5: ", workspace.FetchBlob("Y"))
print("\n\n==> Running Add5Gradient op:")
print("Input of Add5Gradient: ", workspace.FetchBlob("Y"))
add5gradient = core.CreateOperator("Add5Gradient",
["Y"],
["Z"],
device_option=device)
workspace.RunOperatorOnce(add5gradient)
print("Output of Add5Gradient: ", workspace.FetchBlob("Z"))
示例2: enqueue_blobs
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunOperatorOnce [as 别名]
def enqueue_blobs(self, gpu_id, blob_names, blobs):
"""Put a mini-batch on a BlobsQueue."""
assert len(blob_names) == len(blobs)
t = time.time()
dev = c2_utils.CudaDevice(gpu_id)
queue_name = 'gpu_{}/{}'.format(gpu_id, self._blobs_queue_name)
blob_names = ['gpu_{}/{}'.format(gpu_id, b) for b in blob_names]
for (blob_name, blob) in zip(blob_names, blobs):
workspace.FeedBlob(blob_name, blob, device_option=dev)
logger.debug(
'enqueue_blobs {}: workspace.FeedBlob: {}'.
format(gpu_id, time.time() - t)
)
t = time.time()
op = core.CreateOperator(
'SafeEnqueueBlobs', [queue_name] + blob_names,
blob_names + [queue_name + '_enqueue_status'],
device_option=dev
)
workspace.RunOperatorOnce(op)
logger.debug(
'enqueue_blobs {}: workspace.RunOperatorOnce: {}'.
format(gpu_id, time.time() - t)
)
示例3: _CorrectMomentum
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunOperatorOnce [as 别名]
def _CorrectMomentum(self, correction):
"""The MomentumSGDUpdate op implements the update V as
V := mu * V + lr * grad,
where mu is the momentum factor, lr is the learning rate, and grad is
the stochastic gradient. Since V is not defined independently of the
learning rate (as it should ideally be), when the learning rate is
changed we should scale the update history V in order to make it
compatible in scale with lr * grad.
"""
logger.info(
'Scaling update history by {:.6f} (new lr / old lr)'.
format(correction))
for i in range(cfg.NUM_GPUS):
with c2_utils.CudaScope(i):
for param in self.TrainableParams(gpu_id=i):
op = core.CreateOperator(
'Scale', [param + '_momentum'], [param + '_momentum'],
scale=correction)
workspace.RunOperatorOnce(op)
示例4: _run_test
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunOperatorOnce [as 别名]
def _run_test(self, A, B, check_grad=False):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('SpatialNarrowAs', ['A', 'B'], ['C'])
workspace.FeedBlob('A', A)
workspace.FeedBlob('B', B)
workspace.RunOperatorOnce(op)
C = workspace.FetchBlob('C')
if check_grad:
gc = gradient_checker.GradientChecker(
stepsize=0.005,
threshold=0.005,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(op, [A, B], 0, [0])
self.assertTrue(res, 'Grad check failed')
dims = C.shape
C_ref = A[:dims[0], :dims[1], :dims[2], :dims[3]]
np.testing.assert_allclose(C, C_ref, rtol=1e-5, atol=1e-08)
示例5: enqueue_blobs
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunOperatorOnce [as 别名]
def enqueue_blobs(
self,
gpu_id,
enqueue_blobs_names,
blob_values,
):
enqueue_blobs_names = [
'gpu_{}/{}'.format(
gpu_id, enqueue_blob_name
) for enqueue_blob_name in enqueue_blobs_names
]
deviceOption = core.DeviceOption(caffe2_pb2.CUDA, gpu_id)
for (blob_name, blob) in zip(enqueue_blobs_names, blob_values):
workspace.FeedBlob(blob_name, blob, device_option=deviceOption)
queue_name = 'gpu_{}/{}'.format(gpu_id, self._blobs_queue_name)
workspace.RunOperatorOnce(
core.CreateOperator(
'SafeEnqueueBlobs',
[queue_name] + enqueue_blobs_names,
enqueue_blobs_names + [queue_name + '_enqueue_status'],
device_option=deviceOption,
)
)
示例6: _run_op_test
# 需要导入模块: from caffe2.python import workspace [as 别名]
# 或者: from caffe2.python.workspace import RunOperatorOnce [as 别名]
def _run_op_test(self, X, I, check_grad=False):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('BatchPermutation', ['X', 'I'], ['Y'])
workspace.FeedBlob('X', X)
workspace.FeedBlob('I', I)
workspace.RunOperatorOnce(op)
Y = workspace.FetchBlob('Y')
if check_grad:
gc = gradient_checker.GradientChecker(
stepsize=0.1,
threshold=0.001,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(op, [X, I], 0, [0])
self.assertTrue(res, 'Grad check failed')
Y_ref = X[I]
np.testing.assert_allclose(Y, Y_ref, rtol=1e-5, atol=1e-08)