示例#1
0
文件: net.py 项目: afcarl/nerv
def _gen(n, comp_c_f, struct_f=bintree):
    dims = 32
    lbls = 5
    unk = '<UNK>'

    sents = tuple(tuple(s) for s in islice(sentgen(min_len=2), n))
    vocab = set(chain.from_iterable(sents))

    dic = OrderedDict()
    for tok in vocab:
        dic[tok] = random_uniform(dims)
    dic[unk] = random_uniform(dims)

    Source = keyed_source_vertex(dims, dic, missing_=unk)
    Class = softmax_vertex(lbls, dims)
    Comp = comp_c_f(dims, 2)
    Model = net_model((
        Source,
        Comp,
        Class,
    ))

    def class_c():
        return Class(target=onehot(lbls))

    nets = []
    for sent in sents:
        net = struct_f(Comp, (Source(tok) for tok in sent))
        decorate(net, class_c, internal_prob=0.2)
        nets.append(net)

    return (tuple(nets), Model, Comp)
示例#2
0
        def softmax():
            dims = 2
            lbls = 2

            Source = static_source_vertex(dims)
            Class = softmax_vertex(lbls, 2 * dims)
            Model = net_model((Class, ))

            a = Source(random_uniform(dims))
            b = Source(random_uniform(dims))
            c = Class(target=array((
                1,
                0,
            )).reshape(-1, 1))

            net = Net()
            net.add_edge(a, c)
            net.add_edge(b, c)

            model = Model()

            return (
                model,
                net,
            )
示例#3
0
        def rnn():
            dims = 2
            lbls = 2

            dic = OrderedDict((
                (
                    'a',
                    random_uniform(dims),
                ),
                (
                    'b',
                    random_uniform(dims),
                ),
                (
                    '<UNK>',
                    random_uniform(dims),
                ),
            ))

            Source = keyed_source_vertex(dims, dic, missing_='<UNK>')
            Comp = rnn_vertex(dims, 2)
            Class = softmax_vertex(lbls, dims)
            Model = net_model((
                Source,
                Comp,
                Class,
            ))

            a = Source('a')
            b = Source('b')
            c = Comp()
            d = Class(target=array((
                1,
                0,
            )).reshape(-1, 1))

            net = Net()
            net.add_edge(a, c)
            net.add_edge(b, c)
            net.add_edge(c, d)

            model = Model()

            return (
                model,
                net,
            )
示例#4
0
    def pickle_check():
        dims = 32

        vertex_classes = (
            static_source_vertex(dims),
            # Note: This is not how the KeyedSourceVertex is used.
            keyed_source_vertex(dims, {'a': 'foo'}),
            softmax_vertex(dims, dims),
            rnn_vertex(dims, 2),
        )

        for vertex_class in vertex_classes:
            dumps(vertex_class)

        Model = net_model(vertex_classes)

        dumps(Model)