def fig5(self, ext='pdf'): # the matric of positions dxs = [0, 0.5, ldots_span, 1] xs = np.cumsum(dxs) dys = [0, 0.7, 0.7] ys = -np.cumsum(dys) nodes = np.zeros([len(xs), len(ys)], dtype='O') with viznet.DynamicShow(figsize=(6,3), filename='_fig5.%s'%ext) as ds: for i, x in enumerate(xs): if x==0: brush = dot else: brush = mpo for j, y in enumerate(ys): if j==1 and brush is not dot: brush_ = invis_mini else: brush_ = brush nodes[i, j] = brush_ >> (x, y) # horizontal lines for j in [0, 2]: for i in range(len(xs)-1): if i == 1: connect_ldots(nodes[i, j], nodes[i+1, j]) else: edge >> (nodes[i, j], nodes[i+1, j]) # vertical lines for i in range(len(xs)): for j in range(len(ys)-1): edge >> (nodes[i,j], nodes[i, j+1])
def fig4(self, ext='pdf'): node_list = [] node_list2 = [] dxs = [0, ldots_span, 1, 1, 1, ldots_span] xs = np.cumsum(dxs) with viznet.DynamicShow(figsize=(6,3), filename='_fig4.%s'%ext) as ds: for i, x in enumerate(xs): if i == 2 or i==3: brush = invis else: brush = mpo node_list.append(brush >> (x, 0)) if i == 2: mpo2 = hbar >> ((xs[2]+xs[3])/2., -dy) node_list2.append(mpo2) elif i == 3: node_list2.append(mpo2) else: node_list2.append(mpo >> (x, -dy)) # horizontal lines for nlist in [node_list, node_list2]: connect_ldots(nlist[0], nlist[1]) connect_ldots(nlist[4], nlist[5]) edge >> (nlist[1], nlist[2]) edge >> (nlist[3], nlist[4]) # vertical lines for node, node2 in zip(node_list, node_list2): edge >> (node, node2.pin('top', align=node))
def cgraph_layer(output='png'): '''computation graph for a typical single layer.''' with viznet.DynamicShow(figsize=(5, 4), filename='_cg_layer.%s' % output) as ds: output = viznet.NodeBrush('nn.output', ds.ax) input = viznet.NodeBrush('nn.input', ds.ax) op = viznet.NodeBrush('basic', ds.ax) edge = viznet.EdgeBrush('->', ds.ax) x = input >> (0, 0) x.text('$x$') dot = op >> (1, 0) dot.text('dot') dot.text('$f(W,x)=Wx$', 'top') W = input >> (1, -1) W.text('$W$') edge >> (x, dot) edge >> (W, dot) b = input >> (2, -1) b.text('$b$') plus = op >> (2, 0) plus.text('$+$') edge >> (dot, plus) edge >> (b, plus) sigmoid = output >> (3, 0) sigmoid.text(r'$\sigma$') edge >> (plus, sigmoid)
def cgraph_mnist_full(output='png'): '''computation graph for simple mnist network.''' with viznet.DynamicShow(figsize=(3, 6), filename='_cg_mnist_full.%s' % output) as ds: output = viznet.NodeBrush('nn.output', ds.ax) input = viznet.NodeBrush('nn.input', ds.ax) op = viznet.NodeBrush('basic', ds.ax) edge = viznet.EdgeBrush('->', ds.ax) x = input >> (0, 0) x.text('$x$') dot = op >> (0, -1) dot.text('dot') W = input >> (-1, -1) W.text('$W$') edge >> (x, dot) edge >> (W, dot) b = input >> (-1, -2) b.text('$b$') plus = op >> (0, -2) plus.text('$+$') edge >> (dot, plus) edge >> (b, plus) softmax = op >> (0, -3) softmax.text(r'Softmax', 'left') edge >> (plus, softmax) crossentropy = op >> (0, -4) crossentropy.text(r'CrossEntropy', 'left') y = input >> (1, -4) y.text(r'$y$') edge >> (softmax, crossentropy) edge >> (y, crossentropy) mean = output >> (0, -5) mean.text(r'Mean') edge >> (crossentropy, mean)
def framework(self): with viznet.DynamicShow(figsize=(6, 4), filename="framework.png") as dp: grid = viznet.Grid((3.0, 1.5), offset=(2, 2)) edge = EdgeBrush('->', lw=2., color='r') # define an mpo mpo = NodeBrush('box', color='cyan', roundness=0.2, size=(1.0, 0.4)) # generate two mpos Const = mpo >> grid[0:2, 0:1] Const.text('LuxurySparse') Intrinsics = mpo >> grid[3:5, 0:1] Intrinsics.text('Binary Op') Intrinsics = mpo >> grid[6:8, 0:1] Intrinsics.text('Cache Server') Register = mpo >> grid[3:5, 2:3] Register.text('Register') Block = mpo >> grid[0:8, 4:5] Block.text('Blocks (Operator Tree)') Extensions = mpo >> grid[4.5:8, 6:7] Extensions.text('Boost & Extensions') Interface = mpo >> grid[0:3.5, 6:7] Interface.text('Interface') Applications = mpo >> grid[0:8, 8:9] Applications.text('Applications')
def logo3(): viznet.setting.node_setting['inner_lw'] = 0 viznet.setting.node_setting['lw'] = 0 npoint = 60 nedge = 50 angle = random(npoint) * 2 * np.pi #r = np.exp(randn(npoint)*0.4) r = np.sqrt(randn(npoint)) xy = np.array([r * np.cos(angle), r * np.sin(angle)]).T #xy = randn(npoint, 2)*0.5 with viznet.DynamicShow(figsize=(4, 4), filename='_logo3.png') as ds: #body = viznet.NodeBrush('tn.mps', size='huge', color='#AACCFF') >> (0, 0) dot = viznet.NodeBrush('tn.mps', size='tiny') node_list = [] for i, p in enumerate(xy): dot.color = random(3) * 0.5 + 0.5 dot.zorder = 100 + i * 2 dot.size = 0.05 + 0.08 * random() node_list.append(dot >> p) dis_mat = np.linalg.norm(xy - xy[:, None, :], axis=-1) tree = minimum_spanning_tree(dis_mat).tocoo() for i, j in zip(tree.row, tree.col): n1, n2 = node_list[i], node_list[j] viznet.EdgeBrush( choice(['.>.', '.>.']), lw=1, color=random([3]) * 0.4, zorder=(n1.obj.zorder + n2.obj.zorder) / 2) >> (n1, n2)
def fig1(self, ext='pdf'): with viznet.DynamicShow(figsize=(3,2), filename='_fig1.%s'%ext) as ds: mpo2 = box >> (0.5, 0) leg(mpo2, 'left', (-leg_length, 0)) leg(mpo2, 'right', (leg_length, 0)) for i in range(2): node = invis >> (i, -dy) edge >> (mpo2.pin('bottom', align=node), node)
def logo1(): with viznet.DynamicShow(figsize=(4, 4), filename='_logo1.png') as ds: n1 = viznet.NodeBrush('tn.dia', color='#CC3333') >> (0, 0) n2 = viznet.NodeBrush('qc.cross') >> (1, 0) n3 = viznet.NodeBrush('tn.mpo', size=0.25) >> (1, 1) n4 = viznet.NodeBrush('nn.memory') >> (0, 1) viznet.EdgeBrush('<.>') >> (n1, n2) viznet.EdgeBrush('<.>') >> (n2, n3) viznet.EdgeBrush('-<=') >> (n3, n4) viznet.EdgeBrush('=>-') >> (n4, n1)
def fig2(self, ext='pdf'): nrow, ncol = 2, 3 nodes = np.zeros([nrow, ncol], dtype='O') with viznet.DynamicShow(figsize=(5,3), filename='_fig2.%s'%ext) as ds: for i in range(nrow): for j in range(ncol): nodes[i, j] = mpo >> (j*ldots_span, i) # horizontal lines for i in range(nrow): for j in range(ncol-1): connect_ldots(nodes[i, j], nodes[i, j+1]) # vertical lines for j in range(ncol): for i in range(nrow-1): edge >> (nodes[i,j], nodes[i+1, j])
def cgraph_illu(output='png'): '''computation graph for simple mnist network.''' dx = 1 dy = 1 with viznet.DynamicShow(figsize=(3, 2), filename='_cg_illu.%s' % output) as ds: op = viznet.NodeBrush('basic', ds.ax) edge = viznet.EdgeBrush('->', ds.ax) func1 = op >> (dx, 0) func1.text('$f$') inv1 = viznet.edgenode.Pin([0, 0]) e1 = edge >> (inv1, func1) e1.text('input', 'top') func2 = op >> (0, -dy) func2.text('$f$') inv2 = viznet.edgenode.Pin([dx, -dy]) e2 = edge >> (func2, inv2) e2.text('output', 'top')
def logo4(partly): viznet.setting.node_setting['inner_lw'] = 0 viznet.setting.node_setting['lw'] = 2 with viznet.DynamicShow(figsize=(4, 4), filename='_logo4.svg') as ds: dot = viznet.NodeBrush('box', size=0.5, roundness=0.2) tall = 5 xs = np.zeros(tall) ys = np.arange(tall) dot.color = '#333333' for x, y in zip(xs, ys): dot >> (x, y) xs = [-1, 0, 1] ys = (tall - 2) * np.ones(3) dot.color = '#FF9933' for x, y in zip(xs, ys): dot >> (x, y) xs = [1, 2, 2, 2] ys = [2, 2, 1, 0] dot.color = '#333333' for x, y in zip(xs, ys): dot >> (x, y) if not partly: x0 = 5 xs = x0 + np.arange(3) ys = [3, 3, 3] dot.color = '#333333' for x, y in zip(xs, ys): dot >> (x, y) ys = [1, 1, 1] dot.color = '#333333' for x, y in zip(xs, ys): dot >> (x, y) x0 += 5 xs = np.ones(tall) * x0 ys = np.arange(tall) dot.color = '#333333' for x, y in zip(xs, ys): dot >> (x, y)
def fig0(self, ext='pdf'): # the matric of positions dxs = [0, 1, ldots_span] xs = np.cumsum(dxs) node_list = [] with viznet.DynamicShow(figsize=(6,3), filename='_fig0.%s'%ext) as ds: for i, x in enumerate(xs): node_list.append(mpo>>(x, 0)) # horizontal lines for i in range(len(xs)-1): if i == 1: connect_ldots(node_list[i], node_list[i+1]) else: edge >> (node_list[i], node_list[i+1]) # vertical lines for node in node_list: leg(node, 'bottom', offset=(0, -leg_length))
def logo2(): viznet.setting.node_setting['inner_lw'] = 0 viznet.setting.node_setting['lw'] = 0 with viznet.DynamicShow(figsize=(4, 4), filename='_logo2.png') as ds: body = viznet.NodeBrush('tn.dia', size=1, color='#44CCFF') >> (0, 0) b1 = viznet.NodeBrush('tn.mpo', size='large', color='#5588CC') >> (0, 0) b2 = viznet.NodeBrush('tn.dia', size='small', color='#331133') >> (0, 0) viznet.setting.node_setting['inner_lw'] = 2 l = 1.0 brush = viznet.NodeBrush('qc.cross', rotate=np.pi / 4.) n1 = brush >> (l, l) n2 = brush >> (-l, l) n3 = brush >> (-l, -l) n4 = brush >> (l, -l) viznet.EdgeBrush('.>-', lw=2) >> (body, n1) viznet.EdgeBrush('.>-', lw=2) >> (body, n2) viznet.EdgeBrush('.>-', lw=2) >> (body, n3) viznet.EdgeBrush('.>-', lw=2) >> (body, n4)
def fig3(self, ext='pdf'): node_list = [] dxs = [0, ldots_span, 1, 1, 1, ldots_span] xs = np.cumsum(dxs) with viznet.DynamicShow(figsize=(6,3), filename='_fig3.%s'%ext) as ds: for i, x in enumerate(xs): if i == 2 or i==3: brush = invis else: brush = mpo node_list.append(brush >> (x, 0)) # horizontal lines connect_ldots(node_list[0], node_list[1]) connect_ldots(node_list[4], node_list[5]) edge >> (node_list[1], node_list[2]) edge >> (node_list[3], node_list[4]) # vertical lines for node in node_list: leg(node, 'bottom', offset=(0, -leg_length))
def simple(): '''(x1+x2)*x3''' num_node_visible = 6 node = viznet.NodeBrush('nn.input', size='normal') op = viznet.NodeBrush('basic', size='small') edge = viznet.EdgeBrush('->-') with viznet.DynamicShow((4, 3), '_simple_cg.png') as d: # define brushes x1 = node >> (0, 0) x2 = node >> (0, 1) add = op >> (1, 0.5) edge >> (x1, add) edge >> (x2, add) x3 = node >> (1, 1.5) prod = op >> (2, 1.0) edge >> (x3, prod) edge >> (add, prod) prod.text(r'$\times$') add.text(r'$+$') x1.text('$x_1$') x2.text('$x_2$') x3.text('$x_3$')