Пример #1
0
    tgt_embed_layer.initialize(1)

    second_input = tgt_embed_layer.belongs_to(decoder).compute(
        feedback_var, mask=tgt_mask_var)

    second_input = DimShuffle(1, 0, 2).compute(second_input)

    recurrent_unit = GRU(args.hidden_size,
                         input_type="sequence",
                         output_type="sequence",
                         additional_input_dims=[args.word_embed])

    expander_chain = Chain(Dense(600), Dense(args.tgt_vocab_size))
    expander_chain.initialize(args.hidden_size)

    attention_layer = SoftAttentionalLayer(recurrent_unit)
    attention_var = attention_layer.belongs_to(decoder).compute(
        hidden_layer,
        mask=src_mask_var,
        feedback=second_input,
        steps=tgt_var.shape(1))

    # expander
    output_var = expander_chain.belongs_to(expander).compute(attention_var)

    cost = TMCostLayer(tgt_var, tgt_mask_var,
                       args.tgt_vocab_size).compute(output_var)

    model = ComputationalGraph(input_dim=[src_var, src_mask_var])
    model.training_callbacks.append(training_monitor)
Пример #2
0
    seq_input_var = create_var(T.matrix('seq'),
                               dim=args.hidden_size * 2,
                               test_shape=[64, args.hidden_size * 2])
    state_var = create_var(T.matrix("s"),
                           dim=args.hidden_size,
                           test_shape=[64, args.hidden_size])

    input_embed = WordEmbedding(
        args.word_embed,
        args.tgt_vocab_size).belongs_to(decoder).compute(last_token_var)

    recurrent_unit = GRU(args.hidden_size,
                         input_type="sequence",
                         output_type="sequence",
                         additional_input_dims=[input_embed.dim()])
    attention_layer = SoftAttentionalLayer(recurrent_unit, test=True)
    attention_layer.belongs_to(decoder).initialize(args.hidden_size * 2)

    new_state = attention_layer.step({
        "UaH":
        T.dot(seq_input_var.tensor, attention_layer.Ua),
        "feedback":
        input_embed.tensor,
        "inputs":
        seq_input_var.tensor,
        "state":
        state_var.tensor
    })["state"]
    decoder_output_var = create_var(new_state, dim=args.hidden_size)

    # expander