Python theano.tensor.itensor4() Examples

The following are 9 code examples of theano.tensor.itensor4(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module theano.tensor , or try the search function .
Example #1
Source File: op_link.py    From dl4ir-webnav with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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()]) 
Example #2
Source File: theano_utils.py    From seq2seq-keyphrase with MIT License 5 votes vote down vote up
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 
Example #3
Source File: training_strategy.py    From dcase_task2 with MIT License 5 votes vote down vote up
def get_categorical_segmentation_TrainingStrategy(**kwargs):
    """
    Defines training strategy for neural network
    """    
    return TrainingStrategy(y_tensor_type=T.itensor4, objective=mean_pixel_categorical_crossentropy, **kwargs) 
Example #4
Source File: theano_utils.py    From CopyNet with MIT License 5 votes vote down vote up
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 
Example #5
Source File: visual_cloze.py    From comics with MIT License 5 votes vote down vote up
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 
Example #6
Source File: text_cloze.py    From comics with MIT License 5 votes vote down vote up
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 
Example #7
Source File: visual_cloze.py    From comics with MIT License 4 votes vote down vote up
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 
Example #8
Source File: visual_cloze.py    From comics with MIT License 4 votes vote down vote up
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 
Example #9
Source File: text_cloze.py    From comics with MIT License 4 votes vote down vote up
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