<jupyter_start><jupyter_text>Caffe2 Basic ElementsIn this tutorial, we will go through a set of basic elements for Caffe2.<jupyter_code>from matplotlib import pyplot
import numpy as np
import time

from caffe2.python import core, workspace
from caffe2.proto import caffe2_pb2

%matplotlib inline<jupyter_output><empty_output><jupyter_text>WorkspaceFirst we will look into the workspace, where all the data reside. Workspace consists of all the blobs and data you created and stored in memory.One can feed blobs in the workspace using `FeedBlob()`<jupyter_code>X = np.random.randn(2,2).astype(np.float32)
print("Numpy random array X:\n{}".format(X))
workspace.FeedBlob("X", X)<jupyter_output>Numpy random array X:
[[ 0.3728585  -0.38201433]
 [ 1.7013371  -0.3140982 ]]<jupyter_text>`Blobs()` can be used to print out all existing blobs in the workspace.  Operators Operators in Caffe2 are to define the functions like operations. "Create an operator" in Caffe2 python would only define the computational graph, At later time it would be executed in the C++ backend.One example is to define a computational graph Relu.<jupyter_code>op = core.CreateOperator(
    "Relu", #The type of operator that we want to run
    ["X"],  #A list of input blobs
    ["Y"]   #A list of output blobs
)<jupyter_output><empty_output><jupyter_text>We can feed the input X to the workspace through `workspace.RunOperatorOnce(operator)`<jupyter_code>workspace.FeedBlob("X", np.random.randn(2, 2).astype(np.float32))
workspace.RunOperatorOnce(op)<jupyter_output><empty_output><jupyter_text>Here we check if the operator is excuted and what would be the results on X, Y, and the expected value of Y.<jupyter_code>print("Current blobs in the workspace: {}\n".format(workspace.Blobs()))
print("X:\n{}\n".format(workspace.FetchBlob("X")))
print("Y:\n{}\n".format(workspace.FetchBlob("Y")))
print("Expected:\n{}\n".format(np.maximum(workspace.FetchBlob("X"), 0)))<jupyter_output>Current blobs in the workspace: [u'X', u'Y']

X:
[[-1.9222305  -0.26815236]
 [ 0.3026588   1.6451247 ]]

Y:
[[0.        0.       ]
 [0.3026588 1.6451247]]

Expected:
[[0.        0.       ]
 [0.3026588 1.6451247]]<jupyter_text>Operators can also take optional arguments, which are specified as key-value pairs. One example is to feed a tensor and fills it with Gaussian random variables with zero mean and 1 standard deviation.<jupyter_code>op=core.CreateOperator(
    'GaussianFill',
    [],
    ["Z"],
    shape=[100,100],
    mean=0.0,
    std=1.0,
)
print ("Content of op:\n")
print (str(op))<jupyter_output>Content of op:

output: "Z"
name: ""
type: "GaussianFill"
arg {
  name: "std"
  f: 1.0
}
arg {
  name: "shape"
  ints: 100
  ints: 100
}
arg {
  name: "mean"
  f: 0.0
}<jupyter_text>We can then feed data to the op through `workspace.RunOperatorOnce(op)`.<jupyter_code>workspace.RunOperatorOnce(op)
temp = workspace.FetchBlob("Z")
pyplot.hist(temp.flatten(),bins=50)
pyplot.title("Distribution of Z")<jupyter_output><empty_output><jupyter_text>Nets Nets are computational graphs. A Net is composed of multiple operators.We can create a computational graph like we presented in the beginning of the section.```X = np.random.randn(2,3)W = np.random.randn(5,3)b = np.ones(5)Y = X * W^T + b```<jupyter_code>net = core.Net("first computational graph")
print("Current network proto:\n\n{}".format(net.Proto()))
X = net.GaussianFill([], ["X"], mean=0.0, std=1.0, shape=[2, 3], run_once=0)
print("New network proto:\n\n{}".format(net.Proto()))
print("Type of X is: {}".format(type(X)))
print("The blob name is: {}".format(str(X)))
W = net.GaussianFill([], ["W"], mean=0.0, std=1.0, shape=[5, 3], run_once=0)
b = net.ConstantFill([], ["b"], shape=[5,], value=1.0, run_once=0)
Y = X.FC([W, b], ["Y"])
print("Current network proto:\n\n{}".format(net.Proto()))
from caffe2.python import net_drawer
from IPython import display
graph = net_drawer.GetPydotGraph(net, rankdir="LR")
display.Image(graph.create_png(), width=800)<jupyter_output><empty_output><jupyter_text>There are two ways to run a Net in Caffe2 Python. 1. Using `workspace.RunNetOnce()` to run and immediately destructs the network. This is slow but simple.2. a) Call `workspace.CreateNet()` to create a C++ net object owned by the workspace; and then b) use `workspace.RunNet()` by passing the name of the network to it. This method is fast but a bit more complicated.<jupyter_code># First way to use workspace.RunNetOnce() to run a Net
workspace.ResetWorkspace()
print("Current blobs in the workspace: {}".format(workspace.Blobs()))
workspace.RunNetOnce(net)
print("Blobs in the workspace after execution: {}".format(workspace.Blobs()))
# Let's dump the contents of the blobs
for name in workspace.Blobs():
    print("{}:\n{}".format(name, workspace.FetchBlob(name)))
#a) Call `workspace.CreateNet()` to create a C++ net object owned by the workspace; 
#and then b) use `workspace.RunNet()` by passing the name of the network to it.
workspace.ResetWorkspace()
print("Current blobs in the workspace: {}".format(workspace.Blobs()))
workspace.CreateNet(net)
workspace.RunNet(net.Proto().name)
print("Blobs in the workspace after execution: {}".format(workspace.Blobs()))
for name in workspace.Blobs():
    print("{}:\n{}".format(name, workspace.FetchBlob(name)))
start = time.time()
for i in range(1000):
    workspace.RunNetOnce(net)
end = time.time()
print('Run time per RunNetOnce: {}'.format((end - start) / 1000))

start = time.time()
for i in range(1000):
    workspace.RunNet(net.Proto().name)
end = time.time()
print('Run time per RunNet: {}'.format((end - start) / 1000))<jupyter_output>Run time per RunNetOnce: 9.42590236664e-05
Run time per RunNet: 7.55906105042e-06
{'fraction_non_alphanumeric': 0.14421385860007035, 'fraction_numerical': 0.03798804080196975, 'mean_word_length': 5.256325632563256, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '274741', 'license': 'MIT', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1921, 'n_words': 590, 'path': 'Section_2_Caffe2 Basic Elements.ipynb', 'repo_name': 'PacktPublishing/Hands-On-Deep-Learning-with-Caffe2', 'size': '5686'}