Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
def visualize_tree(pairs, geometry, engine='viznet', offsets=None):
    if len(geometry)==2:
        xs, ys = np.meshgrid(np.arange(geometry[0]), np.arange(geometry[1]), indexing='ij')
        num_bit = np.prod(geometry)
    else:
        num_bit = geometry[0]
        t = np.linspace(0,2*np.pi*(num_bit-1)/num_bit,num_bit)
        xs, ys = np.sqrt(num_bit)/2.5*np.cos(t), np.sqrt(num_bit)/2.5*np.sin(t)
    locs = np.concatenate([xs[...,None], ys[...,None]], axis=-1).reshape([-1,2])
    wl = np.log(np.array([p[2] for p in pairs]))
    if offsets is None:
        offsets = [(0,0)]*num_bit

    if engine == 'networkx':
        import networkx as nx
        G = nx.Graph()
        for i, loc in enumerate(locs):
            plt.text(loc[0], loc[1]-0.2, '%d'%i, fontsize=18, va='center', ha='center')
            G.add_node(i, loc=loc)
        G.add_edges_from([p[:2] for p in pairs])
        vmin = wl.min()-0.3
        vmax = wl.max()+0.3
        print(vmin, vmax)
        nx.draw(G, pos=locs, node_color='#A0CBE2', edge_color=np.log(wl), edge_vmin=vmin, edge_vmax=vmax,
                width=4, edge_cmap=plt.cm.Blues, with_labels=False, alpha=1)
    elif engine == 'matplotlib':
        for i, loc in enumerate(locs):
            plt.text(loc[0], loc[1]-0.2, '%d'%i, fontsize=18, va='center', ha='center')
        plt.scatter(xs, ys, s=200, zorder=101)
        w_interval = wl.max()-wl.min()
        wl/=w_interval*1.2
        wl-=wl.min()-0.01
        for (i, j, _), w in zip(pairs, wl):
            start, end = locs[i], locs[j]
            cmap = plt.get_cmap('hot')
            plt.plot([start[0], end[0]], [start[1], end[1]],color=cmap(w*10))
    else:
        import viznet
        cmap = plt.get_cmap('binary')
        w_interval = wl.max()-wl.min()
        wl=(wl-wl.min())/(w_interval+0.1)
        print(wl)
        wl+=0.1
        viznet.setting.node_setting['lw']=0

        node = viznet.NodeBrush('basic', size='small', color='#6699CC')
        nl = []
        for i, pos in enumerate(locs):
            ni = node >> np.array(pos)+offsets[i]
            ni.text(i)
            nl.append(ni)

        for (i,j,_), w in zip(pairs, wl):
            edge = viznet.EdgeBrush('-', color=cmap(w), lw=2)
            edge >> (nl[i], nl[j])
Пример #6
0
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)
Пример #7
0
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')
Пример #8
0
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$')
Пример #9
0
import numpy as np
import viznet

facecolor = '#BB99FF'
leg_length = 0.2
mpo_offset = 0.4
ldots_span = 1.7
dy = 0.9
edge = viznet.EdgeBrush('-')
dot = viznet.NodeBrush('pin')
hbar = viznet.NodeBrush('tn.mpo', color=facecolor); hbar.size = (0.7, 0.3)
box = viznet.NodeBrush('box', color=facecolor, size=(0.8,0.4))
mpo = viznet.NodeBrush('tn.mpo', color=facecolor)
invis = viznet.NodeBrush('invisible')
invis_mini = viznet.NodeBrush('invisible', size='small')

def leg(obj, loc, offset=(0,0)):
    target = obj.pin(loc) + offset
    pin = obj.pin(loc, align=target)
    edge >> (pin, target)

def connect_ldots(node1, node2):
    center = 0.5*(node1.center + node2.center)
    inter = invis >> center.position
    inter.text('$\ldots$', fontsize=18)
    edge >> (node1, inter)
    edge >> (inter, node2)

class Gradient():
    def fig0(self, ext='pdf'):
        # the matric of positions