0

我正在尝试将两个 blob 的值相加。这些 blob 包含一个矩阵,即2,2

workspace.FeedBlob("X", np.random.randn(2, 2).astype(np.float32))
workspace.FeedBlob("Y", np.random.randn(2, 2).astype(np.float32))
net = core.Net('net')
sum_stuff = net.Add([X, Y])
4

1 回答 1

0

究竟是什么不起作用?以下示例将创建两个 2x2 矩阵并将它们相加:

from caffe2.python import workspace, model_helper, core
import numpy as np

# create 2x2 matrices with random integer from 0 to 9
# and feed them to the workspace
workspace.FeedBlob("X", np.random.randint(0,9,size=(2,2)).astype(np.int))
workspace.FeedBlob("Y", np.random.randint(0,9,size=(2,2)).astype(np.int))

# define a network which adds the two blobs together and
# stores the result as Sum
net = core.Net('net')
sum_stuff = net.Add(["X", "Y"], "Sum")

# run the network
workspace.CreateNet(net)
workspace.RunNet(net.Proto().name)

# get the values from the workspace
X = workspace.FetchBlob("X")
Y = workspace.FetchBlob("Y")
Sum = workspace.FetchBlob("Sum")

# print the result to check if correct
print("First matrix:\n{0}".format(X))
print("Second matrix:\n{0}".format(Y))
print("Sum of the two matrices:\n{0}".format(Sum))
于 2018-03-08T19:27:56.293 回答