当前位置: 首页>>代码示例>>Python>>正文


Python tensor.itensor3方法代码示例

本文整理汇总了Python中theano.tensor.itensor3方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.itensor3方法的具体用法?Python tensor.itensor3怎么用?Python tensor.itensor3使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在theano.tensor的用法示例。


在下文中一共展示了tensor.itensor3方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: make_node

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def make_node(self, x, x2, x3, x4, x5):
        # check that the theano version has support for __props__.
        # This next line looks like it has a typo,
        # but it's actually a way to detect the theano version
        # is sufficiently recent to support the use of __props__.
        assert hasattr(self, '_props'), "Your version of theano is too old to support __props__."
        x = tensor.as_tensor_variable(x)
        x2 = tensor.as_tensor_variable(x2)
        x3 = tensor.as_tensor_variable(x3)
        x4 = tensor.as_tensor_variable(x4)
        x5 = tensor.as_tensor_variable(x5)
        
        if prm.att_doc:
            if prm.compute_emb:
                td = tensor.itensor4().type()
            else:
                td = tensor.ftensor4().type()
            tm = tensor.ftensor3().type()
        else:
            if prm.compute_emb:
                td = tensor.itensor3().type()
            else:
                td = tensor.ftensor3().type()
            tm = tensor.fmatrix().type()
        return theano.Apply(self, [x,x2,x3,x4,x5], [td, tm, \
                                           tensor.fmatrix().type(), tensor.ivector().type()]) 
开发者ID:nyu-dl,项目名称:dl4ir-webnav,代码行数:28,代码来源:op_link.py

示例2: TestEmbeddingLayer

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def TestEmbeddingLayer():

    n_in = 60
    a=np.random.uniform(0, 1, (20, 300, n_in)).round().astype(np.int32)
    n_out = 5

    x = T.itensor3('x')
    layer = MetaEmbeddingLayer(x, n_in, n_out)
    f = theano.function([x], [layer.output, layer.pcenters])

    b, pcenter = f(a)
   
    print b[0, 1, 2]
    print b[0, 1, 20]
    print a.shape
    batch=np.random.randint(0, 20)
    row1 = np.random.randint(0,100)
    row2 = np.random.randint(0,100)

    
    v1= a[batch][row1]
    v2= a[batch][row2]
    print b.shape
    print b[batch][row1][row2]
    c = np.outer( v1, v2)
    d = c[:, :, np.newaxis ]
    e = np.sum( (d * layer.W.get_value() ), axis=(0,1))
    print v1
    print v2
    print e
    print 'diff: ', abs(e - b[batch][row1][row2] ).sum()

    print pcenter
    center = [ np.sum( l.W.get_value(), axis=(0,1) ) for l in layer ] 
    print center
    print np.sum(center**2) 
开发者ID:j3xugit,项目名称:RaptorX-Contact,代码行数:38,代码来源:EmbeddingLayer.py

示例3: ndim_itensor

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def ndim_itensor(ndim, name=None):
    if ndim == 2:
        return T.imatrix(name)
    elif ndim == 3:
        return T.itensor3(name)
    elif ndim == 4:
        return T.itensor4(name)
    return T.imatrix(name)


# dot-product 
开发者ID:memray,项目名称:seq2seq-keyphrase,代码行数:13,代码来源:theano_utils.py

示例4: main

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def main():
    xs = itensor3('xs')
    ins = ((None, None, 93), xs)
    gru = GRU(
        inputs=ins,
        hiddens=128,
        direction='bidirectional'
    )
    print("GRU output (hiddens) shape: ", gru.output_size)
    print("GRU params: ", gru.get_params())

    lstm = LSTM(
        inputs=ins,
        hiddens=128,
        direction='bidirectional'
    )
    print("LSTM output (hiddens) shape: ", lstm.output_size)
    print("LSTM params: ", lstm.get_params())

    rnn = RNN(
        inputs=ins,
        hiddens=128,
        direction='bidirectional'
    )
    print("RNN output (hiddens) shape: ", rnn.output_size)
    print("RNN params: ", rnn.get_params()) 
开发者ID:vitruvianscience,项目名称:OpenDeep,代码行数:28,代码来源:recurrent.py

示例5: add_datasets_to_graph

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def add_datasets_to_graph(list_of_datasets, list_of_names, graph, strict=True,
                          list_of_test_values=None):
    assert len(list_of_datasets) == len(list_of_names)
    datasets_added = []
    for n, (dataset, name) in enumerate(zip(list_of_datasets, list_of_names)):
        if dataset.dtype != "int32":
            if len(dataset.shape) == 1:
                sym = tensor.vector()
            elif len(dataset.shape) == 2:
                sym = tensor.matrix()
            elif len(dataset.shape) == 3:
                sym = tensor.tensor3()
            else:
                raise ValueError("dataset %s has unsupported shape" % name)
        elif dataset.dtype == "int32":
            if len(dataset.shape) == 1:
                sym = tensor.ivector()
            elif len(dataset.shape) == 2:
                sym = tensor.imatrix()
            elif len(dataset.shape) == 3:
                sym = tensor.itensor3()
            else:
                raise ValueError("dataset %s has unsupported shape" % name)
        else:
            raise ValueError("dataset %s has unsupported dtype %s" % (
                name, dataset.dtype))
        if list_of_test_values is not None:
            sym.tag.test_value = list_of_test_values[n]
        tag_expression(sym, name, dataset.shape)
        datasets_added.append(sym)
    graph["__datasets_added__"] = datasets_added
    return datasets_added 
开发者ID:kastnerkyle,项目名称:SciPy2015,代码行数:34,代码来源:kdl_template.py

示例6: T_one_hot

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def T_one_hot(inp_tensor, n_classes):
    """
    :todo:
        - Implement other methods from here: 
        - Compare them speed-wise for different sizes
        - Implement N_one_hot for Numpy version, with speed tests.

    Theano one-hot (1-of-k) from an input tensor of indecies.
    If the indecies are of the shape (a0, a1, ..., an) the output
    shape would be (a0, a1, ..., a2, n_classes).

    :params:
        - inp_tensor: any theano tensor with dtype int* as indecies and all of
                      them between [0, n_classes-1].
        - n_classes: number of classes which determines the output size.

    :usage:
        >>> idx = T.itensor3()
        >>> idx_val = numpy.array([[[0,1,2,3],[4,5,6,7]]], dtype='int32')
        >>> one_hot = T_one_hot(t, 8)
        >>> one_hot.eval({idx:idx_val})
        >>> print out
        array([[[[ 1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
         [ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.],
         [ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.]],
        [[ 0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.],
         [ 0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.],
         [ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.],
         [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]]]])
        >>> print idx_val.shape, out.shape
        (1, 2, 4) (1, 2, 4, 8)
    """
    flattened = inp_tensor.flatten()
    z = T.zeros((flattened.shape[0], n_classes), dtype=theano.config.floatX)
    one_hot = T.set_subtensor(z[T.arange(flattened.shape[0]), flattened], 1)
    out_shape = [inp_tensor.shape[i] for i in xrange(inp_tensor.ndim)] + [n_classes]
    one_hot = one_hot.reshape(out_shape)
    return one_hot 
开发者ID:soroushmehr,项目名称:sampleRNN_ICLR2017,代码行数:41,代码来源:ops.py

示例7: make_node

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def make_node(self, x1, x2, x3, x4):
        assert hasattr(self, '_props'), "Your version of theano is too old to support __props__."
        x1 = tensor.as_tensor_variable(x1)
        x2 = tensor.as_tensor_variable(x2)
        x3 = tensor.as_tensor_variable(x3)
        x4 = tensor.as_tensor_variable(x4)
        out = [tensor.fmatrix().type(), tensor.itensor3().type(), tensor.imatrix().type(), tensor.fmatrix().type()]

        return theano.Apply(self, [x1, x2, x3, x4], out) 
开发者ID:nyu-dl,项目名称:dl4ir-query-reformulator,代码行数:11,代码来源:op_search.py

示例8: build_image_only_network

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def build_image_only_network(d_word, d_hidden, lr, eps=1e-6):

    # input theano vars
    in_context_fc7 = T.tensor3(name='context_images')
    in_context_bb = T.tensor4(name='context_bb')
    in_bbmask = T.tensor3(name='bounding_box_mask')
    in_context = T.itensor4(name='context')
    in_cmask = T.tensor4(name='context_mask')
    in_answer_fc7 = T.tensor3(name='answer_images')
    in_answer_bb = T.matrix(name='answer_bb')
    in_answers = T.itensor3(name='answers')
    in_amask = T.tensor3(name='answer_mask')
    in_labels = T.imatrix(name='labels')

    # define network
    l_context_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_context_fc7)
    l_answer_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_answer_fc7)

    l_bbmask = lasagne.layers.InputLayer(shape=(None, 3, max_boxes), input_var=in_bbmask)

    l_context_proj = lasagne.layers.DenseLayer(l_context_fc7, num_units=d_hidden, nonlinearity=lasagne.nonlinearities.rectify, num_leading_axes=2)
    l_context_final_reps = lasagne.layers.LSTMLayer(l_context_proj, num_units=d_hidden, only_return_final=True)

    l_ans_proj = lasagne.layers.DenseLayer(l_answer_fc7, num_units=d_word, nonlinearity=lasagne.nonlinearities.rectify, 
        num_leading_axes=2)
    l_scores = InnerProductLayer([l_context_final_reps, l_ans_proj])

    preds = lasagne.layers.get_output(l_scores)
    loss = T.mean(lasagne.objectives.categorical_crossentropy(preds, in_labels))

    all_params = lasagne.layers.get_all_params(l_scores, trainable=True)
    updates = lasagne.updates.adam(loss, all_params, learning_rate=lr)
    train_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, 
        in_context, in_cmask, in_answer_fc7, in_labels], 
        loss, updates=updates, on_unused_input='warn')
    pred_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, 
        in_context, in_cmask, in_answer_fc7], 
        preds, on_unused_input='warn')

    return train_fn, pred_fn, l_scores 
开发者ID:miyyer,项目名称:comics,代码行数:42,代码来源:visual_cloze.py

示例9: build_image_only_network

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def build_image_only_network(d_word, d_hidden, lr, eps=1e-6):

    # input theano vars
    in_context_fc7 = T.tensor3(name='context_images')
    in_context_bb = T.tensor4(name='context_bb')
    in_bbmask = T.tensor3(name='bounding_box_mask')
    in_context = T.itensor4(name='context')
    in_cmask = T.tensor4(name='context_mask')
    in_answer_fc7 = T.matrix(name='answer_images')
    in_answer_bb = T.matrix(name='answer_bb')
    in_answers = T.itensor3(name='answers')
    in_amask = T.tensor3(name='answer_mask')
    in_labels = T.imatrix(name='labels')

    # define network
    l_context_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_context_fc7)
    l_answers = lasagne.layers.InputLayer(shape=(None, 3, max_words), input_var=in_answers)
    l_amask = lasagne.layers.InputLayer(shape=l_answers.shape, input_var=in_amask)

    # contexts and answers should share embeddings
    l_answer_emb = lasagne.layers.EmbeddingLayer(l_answers, len_voc, d_word)

    l_context_proj = lasagne.layers.DenseLayer(l_context_fc7, num_units=d_hidden, nonlinearity=lasagne.nonlinearities.rectify, num_leading_axes=2)
    l_context_final_reps = lasagne.layers.LSTMLayer(l_context_proj, num_units=d_hidden, only_return_final=True)
    l_ans_reps = SumAverageLayer([l_answer_emb, l_amask], compute_sum=True, num_dims=3)
    l_scores = InnerProductLayer([l_context_final_reps, l_ans_reps])

    preds = lasagne.layers.get_output(l_scores)
    loss = T.mean(lasagne.objectives.categorical_crossentropy(preds, in_labels))

    all_params = lasagne.layers.get_all_params(l_scores, trainable=True)
    updates = lasagne.updates.adam(loss, all_params, learning_rate=lr)
    train_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, in_context, in_cmask, in_answer_fc7, in_answer_bb, in_answers, 
        in_amask, in_labels], 
        loss, updates=updates, on_unused_input='warn')
    pred_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, in_context, in_cmask, in_answer_fc7, in_answer_bb, in_answers, 
        in_amask], 
        preds, on_unused_input='warn')
    return train_fn, pred_fn, l_scores 
开发者ID:miyyer,项目名称:comics,代码行数:41,代码来源:text_cloze.py

示例10: _init_model

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def _init_model(self, in_size, out_size, n_hid=10, learning_rate_sl=0.005, \
            learning_rate_rl=0.005, batch_size=32, ment=0.1):
        # 2-layer MLP
        self.in_size = in_size # x and y coordinate
        self.out_size = out_size # up, down, right, left
        self.batch_size = batch_size
        self.learning_rate = learning_rate_rl
        self.n_hid = n_hid

        input_var, turn_mask, act_mask, reward_var = T.ftensor3('in'), T.imatrix('tm'), \
                T.itensor3('am'), T.fvector('r')

        in_var = T.reshape(input_var, (input_var.shape[0]*input_var.shape[1],self.in_size))

        l_mask_in = L.InputLayer(shape=(None,None), input_var=turn_mask)

        pol_in = T.fmatrix('pol-h')
        l_in = L.InputLayer(shape=(None,None,self.in_size), input_var=input_var)
        l_pol_rnn = L.GRULayer(l_in, n_hid, hid_init=pol_in, mask_input=l_mask_in) # B x H x D
        pol_out = L.get_output(l_pol_rnn)[:,-1,:]
        l_den_in = L.ReshapeLayer(l_pol_rnn, (turn_mask.shape[0]*turn_mask.shape[1], n_hid)) # BH x D
        l_out = L.DenseLayer(l_den_in, self.out_size, nonlinearity=lasagne.nonlinearities.softmax)

        self.network = l_out
        self.params = L.get_all_params(self.network)

        # rl
        probs = L.get_output(self.network) # BH x A
        out_probs = T.reshape(probs, (input_var.shape[0],input_var.shape[1],self.out_size)) # B x H x A
        log_probs = T.log(out_probs)
        act_probs = (log_probs*act_mask).sum(axis=2) # B x H
        ep_probs = (act_probs*turn_mask).sum(axis=1) # B
        H_probs = -T.sum(T.sum(out_probs*log_probs,axis=2),axis=1) # B
        self.loss = 0.-T.mean(ep_probs*reward_var + ment*H_probs)

        updates = lasagne.updates.rmsprop(self.loss, self.params, learning_rate=learning_rate_rl, \
                epsilon=1e-4)

        self.inps = [input_var, turn_mask, act_mask, reward_var, pol_in]
        self.train_fn = theano.function(self.inps, self.loss, updates=updates)
        self.obj_fn = theano.function(self.inps, self.loss)
        self.act_fn = theano.function([input_var, turn_mask, pol_in], [out_probs, pol_out])

        # sl
        sl_loss = 0.-T.mean(ep_probs)
        sl_updates = lasagne.updates.rmsprop(sl_loss, self.params, learning_rate=learning_rate_sl, \
                epsilon=1e-4)

        self.sl_train_fn = theano.function([input_var, turn_mask, act_mask, pol_in], sl_loss, \
                updates=sl_updates)
        self.sl_obj_fn = theano.function([input_var, turn_mask, act_mask, pol_in], sl_loss) 
开发者ID:MiuLab,项目名称:KB-InfoBot,代码行数:53,代码来源:agent_rl.py

示例11: __init__

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def __init__(self, K, vocab_size, num_chars, W_init, 
            nhidden, embed_dim, dropout, train_emb, char_dim, use_feat, gating_fn, 
            save_attn=False):
        self.nhidden = nhidden
        self.embed_dim = embed_dim
        self.dropout = dropout
        self.train_emb = train_emb
        self.char_dim = char_dim
        self.learning_rate = LEARNING_RATE
        self.num_chars = num_chars
        self.use_feat = use_feat
        self.save_attn = save_attn
        self.gating_fn = gating_fn

        self.use_chars = self.char_dim!=0
        if W_init is None: W_init = lasagne.init.GlorotNormal().sample((vocab_size, self.embed_dim))

        doc_var, query_var, cand_var = T.itensor3('doc'), T.itensor3('quer'), \
                T.wtensor3('cand')
        docmask_var, qmask_var, candmask_var = T.bmatrix('doc_mask'), T.bmatrix('q_mask'), \
                T.bmatrix('c_mask')
        target_var = T.ivector('ans')
        feat_var = T.imatrix('feat')
        doc_toks, qry_toks= T.imatrix('dchars'), T.imatrix('qchars')
        tok_var, tok_mask = T.imatrix('tok'), T.bmatrix('tok_mask')
        cloze_var = T.ivector('cloze')
        self.inps = [doc_var, doc_toks, query_var, qry_toks, cand_var, target_var, docmask_var,
                qmask_var, tok_var, tok_mask, candmask_var, feat_var, cloze_var]

        self.predicted_probs, predicted_probs_val, self.network, W_emb, attentions = (
                self.build_network(K, vocab_size, W_init))

        self.loss_fn = T.nnet.categorical_crossentropy(self.predicted_probs, target_var).mean()
        self.eval_fn = lasagne.objectives.categorical_accuracy(self.predicted_probs, 
                target_var).mean()

        loss_fn_val = T.nnet.categorical_crossentropy(predicted_probs_val, target_var).mean()
        eval_fn_val = lasagne.objectives.categorical_accuracy(predicted_probs_val, 
                target_var).mean()

        self.params = L.get_all_params(self.network, trainable=True)
        
        updates = lasagne.updates.adam(self.loss_fn, self.params, learning_rate=self.learning_rate)

        self.train_fn = theano.function(self.inps,
                [self.loss_fn, self.eval_fn, self.predicted_probs], 
                updates=updates,
                on_unused_input='warn')
        self.validate_fn = theano.function(self.inps, 
                [loss_fn_val, eval_fn_val, predicted_probs_val]+attentions,
                on_unused_input='warn') 
开发者ID:bdhingra,项目名称:ga-reader,代码行数:53,代码来源:GAReader.py

示例12: __theano_train__

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def __theano_train__(self, n_size):
        """
        Pr(l|u, C(l)) = Pr(l|u) * Pr(l|C(l))
        Pr(u, l, t) = Pr(l|u, C(l))     if C(l) exists,
                      Pr(l|u)           otherwise.
        $Theta$ = argmax Pr(u, l, t)
        """
        tra_mask = T.ivector()
        seq_length = T.sum(tra_mask)  # 有效长度
        wl = T.concatenate((self.wl, self.wl_m))
        tidx, cidx, bidx, userid = T.ivector(), T.imatrix(), T.itensor3(), T.iscalar()
        pb = self.pb[bidx]  # (seq_length x 4 x depth x n_size)
        lrs = self.lrs[tidx]  # (seq_length x 4 x depth)
        # user preference
        xu = self.xu[userid]
        plu = softmax(T.dot(xu, self.wl.T))
        # geographical influence
        cl = T.sum(wl[cidx], axis=1)  # (seq_length x n_size)
        cl = cl.reshape((cl.shape[0], 1, 1, cl.shape[1]))
        br = sigmoid(T.sum(pb[:seq_length] * cl, axis=3) * lrs[:seq_length]) * T.ceil(abs(T.mean(cl, axis=3)))
        path = T.prod(br, axis=2) * self.probs[tidx][:seq_length]
        # paths = T.prod((T.floor(1-path) + path), axis=1)
        paths = T.sum(path, axis=1)
        paths = T.floor(1 - paths) + paths
        # ----------------------------------------------------------------------------
        # cost, gradients, learning rate, l2 regularization
        lr, l2 = self.alpha_lambda[0], self.alpha_lambda[1]
        seq_l2_sq = T.sum([T.sum(par ** 2) for par in [xu, self.wl]])
        upq = - 1 * T.sum(T.log(plu[tidx[:seq_length]] * paths)) / seq_length
        seq_costs = (
            upq +
            0.5 * l2 * seq_l2_sq)
        seq_grads = T.grad(seq_costs, self.params)
        seq_updates = [(par, par - lr * gra) for par, gra in zip(self.params, seq_grads)]
        pars_subs = [(self.xu, xu), (self.pb, pb)]
        seq_updates.extend([(par, T.set_subtensor(sub, sub - lr * T.grad(seq_costs, sub)))
                            for par, sub in pars_subs])
        # ----------------------------------------------------------------------------
        uidx = T.iscalar()  # T.iscalar()类型是 TensorType(int32, )
        self.seq_train = theano.function(
            inputs=[uidx],
            outputs=upq,
            updates=seq_updates,
            givens={
                userid: uidx,
                tidx: self.tra_target_masks[uidx],
                cidx: self.tra_context_masks[T.arange(self.tra_accum_lens[uidx][0], self.tra_accum_lens[uidx][1])],
                bidx: self.routes[self.tra_target_masks[uidx]],
                tra_mask: self.tra_masks[uidx]
                # tra_mask_cot: self.tra_masks_cot[T.arange(self.tra_accum_lens[uidx][0], self.tra_accum_lens[uidx][1])]
            }) 
开发者ID:tangrizzly,项目名称:Point-of-Interest-Recommendation,代码行数:53,代码来源:POI2Vec.py

示例13: build_text_only_network

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def build_text_only_network(d_word, d_hidden, lr, eps=1e-6):

    # input theano vars
    in_context_fc7 = T.tensor3(name='context_images')
    in_context_bb = T.tensor4(name='context_bb')
    in_bbmask = T.tensor3(name='bounding_box_mask')
    in_context = T.itensor4(name='context')
    in_cmask = T.tensor4(name='context_mask')
    in_answer_fc7 = T.tensor3(name='answer_images')
    in_answer_bb = T.matrix(name='answer_bb')
    in_answers = T.itensor3(name='answers')
    in_amask = T.tensor3(name='answer_mask')
    in_labels = T.imatrix(name='labels')

    # define network
    l_answer_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_answer_fc7)

    l_context = lasagne.layers.InputLayer(shape=(None, max_panels, max_boxes, max_words), 
        input_var=in_context)

    l_cmask = lasagne.layers.InputLayer(shape=l_context.shape, input_var=in_cmask)
    l_bbmask = lasagne.layers.InputLayer(shape=(None, 3, max_boxes), input_var=in_bbmask)

    # contexts and answers should share embeddings
    l_context_emb = lasagne.layers.EmbeddingLayer(l_context, len_voc, 
        d_word, name='word_emb')

    l_context_box_reps = SumAverageLayer([l_context_emb, l_cmask], compute_sum=True, num_dims=4)
    l_box_reshape = lasagne.layers.ReshapeLayer(l_context_box_reps, (-1, max_boxes, d_word))
    l_bbmask_reshape = lasagne.layers.ReshapeLayer(l_bbmask, (-1, max_boxes))
    l_box_lstm = lasagne.layers.LSTMLayer(l_box_reshape, num_units=d_word, mask_input=l_bbmask_reshape, only_return_final=True)
    l_context_panel_reps = lasagne.layers.ReshapeLayer(l_box_lstm, (-1, 3, d_word))
    l_context_proj = lasagne.layers.DenseLayer(l_context_panel_reps, num_units=d_hidden, nonlinearity=lasagne.nonlinearities.rectify, num_leading_axes=2)
    l_context_final_reps = lasagne.layers.LSTMLayer(l_context_proj, num_units=d_hidden, only_return_final=True)
    l_context_final_reps = lasagne.layers.DenseLayer(l_context_final_reps, num_units=d_word, nonlinearity=lasagne.nonlinearities.rectify)

    l_ans_proj = lasagne.layers.DenseLayer(l_answer_fc7, num_units=d_word, nonlinearity=lasagne.nonlinearities.rectify, num_leading_axes=2)
    l_scores = InnerProductLayer([l_context_final_reps, l_ans_proj])

    preds = lasagne.layers.get_output(l_scores)
    loss = T.mean(lasagne.objectives.categorical_crossentropy(preds, in_labels))

    all_params = lasagne.layers.get_all_params(l_scores, trainable=True)
    updates = lasagne.updates.adam(loss, all_params, learning_rate=lr)
    train_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, 
        in_context, in_cmask, in_answer_fc7, in_labels], 
        loss, updates=updates, on_unused_input='warn')
    pred_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, 
        in_context, in_cmask, in_answer_fc7], 
        preds, on_unused_input='warn')

    return train_fn, pred_fn, l_scores 
开发者ID:miyyer,项目名称:comics,代码行数:54,代码来源:visual_cloze.py

示例14: build_image_text_network

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def build_image_text_network(d_word, d_hidden, lr, eps=1e-6):

    # input theano vars
    in_context_fc7 = T.tensor3(name='context_images')
    in_context_bb = T.tensor4(name='context_bb')
    in_bbmask = T.tensor3(name='bounding_box_mask')
    in_context = T.itensor4(name='context')
    in_cmask = T.tensor4(name='context_mask')
    in_answer_fc7 = T.tensor3(name='answer_images')
    in_answer_bb = T.matrix(name='answer_bb')
    in_answers = T.itensor3(name='answers')
    in_amask = T.tensor3(name='answer_mask')
    in_labels = T.imatrix(name='labels')

    # define network
    l_context_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_context_fc7)
    l_answer_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_answer_fc7)

    l_context = lasagne.layers.InputLayer(shape=(None, max_panels, max_boxes, max_words), 
        input_var=in_context)

    l_cmask = lasagne.layers.InputLayer(shape=l_context.shape, input_var=in_cmask)
    l_bbmask = lasagne.layers.InputLayer(shape=(None, 3, max_boxes), input_var=in_bbmask)

    # contexts and answers should share embeddings
    l_context_emb = lasagne.layers.EmbeddingLayer(l_context, len_voc, 
        d_word, name='word_emb')

    l_context_box_reps = SumAverageLayer([l_context_emb, l_cmask], compute_sum=True, num_dims=4)
    l_box_reshape = lasagne.layers.ReshapeLayer(l_context_box_reps, (-1, max_boxes, d_word))
    l_bbmask_reshape = lasagne.layers.ReshapeLayer(l_bbmask, (-1, max_boxes))
    l_box_lstm = lasagne.layers.LSTMLayer(l_box_reshape, num_units=d_word, mask_input=l_bbmask_reshape, only_return_final=True)
    l_context_panel_reps = lasagne.layers.ReshapeLayer(l_box_lstm, (-1, 3, d_word))
    l_context_concat = MyConcatLayer([l_context_panel_reps, l_context_fc7], axis=-1)
    l_context_proj = lasagne.layers.DenseLayer(l_context_concat, num_units=d_hidden, nonlinearity=lasagne.nonlinearities.rectify, num_leading_axes=2)
    l_context_final_reps = lasagne.layers.LSTMLayer(l_context_proj, num_units=d_hidden, only_return_final=True)

    l_context_final_reps = lasagne.layers.DenseLayer(l_context_final_reps, num_units=d_word, nonlinearity=lasagne.nonlinearities.rectify)
    l_ans_proj = lasagne.layers.DenseLayer(l_answer_fc7, num_units=d_word, nonlinearity=lasagne.nonlinearities.rectify, num_leading_axes=2)
    l_scores = InnerProductLayer([l_context_final_reps, l_ans_proj])

    preds = lasagne.layers.get_output(l_scores)
    loss = T.mean(lasagne.objectives.categorical_crossentropy(preds, in_labels))

    all_params = lasagne.layers.get_all_params(l_scores, trainable=True)
    updates = lasagne.updates.adam(loss, all_params, learning_rate=lr)
    train_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, 
        in_context, in_cmask, in_answer_fc7, in_labels], 
        loss, updates=updates, on_unused_input='warn')
    pred_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, 
        in_context, in_cmask, in_answer_fc7], 
        preds, on_unused_input='warn')

    return train_fn, pred_fn, l_scores 
开发者ID:miyyer,项目名称:comics,代码行数:56,代码来源:visual_cloze.py

示例15: build_text_only_network

# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import itensor3 [as 别名]
def build_text_only_network(d_word, d_hidden, lr, eps=1e-6):

    # input theano vars
    in_context_fc7 = T.tensor3(name='context_images') # bsz x 3 x 4096 (because 3 context panels, fc7 features each of dim 4096)
    in_context_bb = T.tensor4(name='context_bb') # bsz x 3 x 3 x 4 (because 3 context panels, each contains a max of 3 speech boxes, each box described by 4 coordinates) 
    in_bbmask = T.tensor3(name='bounding_box_mask') # bsz x 3 x 3 (because 3 context panels, each contains a max of 3 speech boxes, the mask has an entry of 1 in the ith position if the panel contains the ith speech box)
    in_context = T.itensor4(name='context') # bsz x 3 x 3 x 30 (because 3 context panels, each contains a max of 3 speech boxes, each box contains speech with a max of 30 words)
    in_cmask = T.tensor4(name='context_mask') # bsz x 3 x 3 x 30 (because 3 context panels, each contains a max of 3 speech boxes, each box contains speech with a max of 30 words, where the mask has an entry of 1 in the ith position if the ith word exists in the speech)
    in_answer_fc7 = T.matrix(name='answer_images') # bsz x 4096 (fc7 feature for the panel for which we want to guess the speech)
    in_answer_bb = T.matrix(name='answer_bb') # bsz x 4 (the answer panel has one speech box described by 4 coordinates)
    in_answers = T.itensor3(name='answers') # bsz x 3 x 30 (3 candidate answers each of max 30 words )
    in_amask = T.tensor3(name='answer_mask') # bsz x 3 x 30 (mask for 3 candidates answers, ie, an entry of 1 in the ith position if the ith word exists in the candidate)
    in_labels = T.imatrix(name='labels') # bsz x 3 (out of 3 candidate answers, the correct answer will have a 1)

    # define network
    l_context_fc7 = lasagne.layers.InputLayer(shape=(None, 3, 4096), input_var=in_context_fc7)
    l_answer_fc7 = lasagne.layers.InputLayer(shape=(None, 4096), input_var=in_answer_fc7)

    l_context = lasagne.layers.InputLayer(shape=(None, max_panels, max_boxes, max_words), 
        input_var=in_context)
    l_answers = lasagne.layers.InputLayer(shape=(None, 3, max_words), input_var=in_answers)

    l_cmask = lasagne.layers.InputLayer(shape=l_context.shape, input_var=in_cmask)
    l_amask = lasagne.layers.InputLayer(shape=l_answers.shape, input_var=in_amask)
    l_bbmask = lasagne.layers.InputLayer(shape=(None, 3, max_boxes), input_var=in_bbmask)

    # contexts and answers should share embeddings
    l_context_emb = lasagne.layers.EmbeddingLayer(l_context, len_voc, 
        d_word, name='word_emb')
    l_answer_emb = lasagne.layers.EmbeddingLayer(l_answers, len_voc, 
        d_word, W=l_context_emb.W)

    l_context_box_reps = SumAverageLayer([l_context_emb, l_cmask], compute_sum=True, num_dims=4)
    l_box_reshape = lasagne.layers.ReshapeLayer(l_context_box_reps, (-1, max_boxes, d_word))
    l_bbmask_reshape = lasagne.layers.ReshapeLayer(l_bbmask, (-1, max_boxes))
    l_box_lstm = lasagne.layers.LSTMLayer(l_box_reshape, num_units=d_word, mask_input=l_bbmask_reshape, only_return_final=True)
    l_context_panel_reps = lasagne.layers.ReshapeLayer(l_box_lstm, (-1, 3, d_word))
    l_context_final_reps = lasagne.layers.LSTMLayer(l_context_panel_reps, num_units=d_word, only_return_final=True)

    l_ans_reps = SumAverageLayer([l_answer_emb, l_amask], compute_sum=True, num_dims=3)
    l_scores = InnerProductLayer([l_context_final_reps, l_ans_reps])

    preds = lasagne.layers.get_output(l_scores)
    loss = T.mean(lasagne.objectives.categorical_crossentropy(preds, in_labels))

    all_params = lasagne.layers.get_all_params(l_scores, trainable=True)
    updates = lasagne.updates.adam(loss, all_params, learning_rate=lr)
    train_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, in_context, in_cmask, in_answer_fc7, in_answer_bb, in_answers, 
        in_amask, in_labels], 
        loss, updates=updates, on_unused_input='warn')
    pred_fn = theano.function([in_context_fc7, in_context_bb, in_bbmask, in_context, in_cmask, in_answer_fc7, in_answer_bb, in_answers, 
        in_amask], 
        preds, on_unused_input='warn')
    return train_fn, pred_fn, l_scores 
开发者ID:miyyer,项目名称:comics,代码行数:56,代码来源:text_cloze.py


注:本文中的theano.tensor.itensor3方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。