Пример #1
0
    def __init__(self, x_size, h_size, opt={}, prefix='answer', dropout=None):
        super(SANDecoder, self).__init__()
        self.prefix = prefix
        self.attn_b = FlatSimilarityWrapper(x_size, h_size, prefix, opt,
                                            dropout)
        self.attn_e = FlatSimilarityWrapper(x_size, h_size, prefix, opt,
                                            dropout)
        self.rnn_type = opt.get('{}_rnn_type'.format(prefix), 'gru')
        self.rnn = RNN_MAP.get(self.rnn_type, nn.GRUCell)(x_size, h_size)
        self.num_turn = opt.get('{}_num_turn'.format(prefix), 5)
        self.opt = opt
        self.mem_random_drop = opt.get('{}_mem_drop_p'.format(prefix), 0)
        self.answer_opt = opt.get('{}_opt'.format(prefix), 0)
        # 0: std mem; 1: random select step; 2 random selection; voting in pred; 3:sort merge
        self.mem_type = opt.get('{}_mem_type'.format(prefix), 0)
        self.gamma = opt.get('{}_mem_gamma'.format(prefix), 0.5)
        self.alpha = Parameter(torch.zeros(1, 1, 1))

        self.proj = nn.Linear(h_size, x_size) if h_size != x_size else None
        if dropout is None:
            self.dropout = DropoutWrapper(
                opt.get('{}_dropout_p'.format(self.prefix), 0))
        else:
            self.dropout = dropout
        self.h2h = nn.Linear(h_size, h_size)
        self.a2h = nn.Linear(x_size, h_size, bias=False)
        self.luong_output_layer = nn.Linear(h_size + x_size, h_size)
Пример #2
0
    def __init__(self, x_size, h_size, opt={}, prefix='answer', dropout=None):
        super(SAN, self).__init__()
        self.prefix = prefix
        # self.attn_b  = FlatSimilarityWrapper(x_size, h_size, prefix, opt, dropout)
        # self.attn_e  = FlatSimilarityWrapper(x_size, h_size, prefix, opt, dropout)
        self.attn = FlatSimilarityWrapper(x_size, h_size, prefix, opt, dropout)
        self.rnn_type = '{}{}'.format(
            opt.get('{}_rnn_type'.format(prefix), 'gru').upper(), 'Cell')
        self.rnn = getattr(nn, self.rnn_type)(x_size, h_size)
        self.num_turn = opt.get('{}_num_turn'.format(prefix), 5)
        # self.attn_startIdx_over_reasoning_steps = Parameter(torch.ones(1,1,self.num_turn).softmax(2))   #initialize with average
        # self.attn_endIdx_over_reasoning_steps = Parameter(torch.ones(1,1,self.num_turn).softmax(2))   #initialize with average
        self.opt = opt
        self.mem_random_drop = opt.get('{}_mem_drop_p'.format(prefix), 0)
        self.answer_opt = opt.get('{}_opt'.format(prefix), 0)
        self.mem_type = opt.get('{}_mem_type'.format(prefix), 0)
        self.gamma = opt.get('{}_mem_gamma'.format(prefix), 0.5)
        self.alpha = Parameter(torch.zeros(1, 1, 1))

        self.proj = nn.Linear(h_size, x_size) if h_size != x_size else None
        if dropout is None:
            self.dropout = DropoutWrapper(
                opt.get('{}_dropout_p'.format(self.prefix), 0))
        else:
            self.dropout = dropout
Пример #3
0
    def __init__(self, x_size, h_size, opt={}, prefix='answer', dropout=None):
        super(SAN, self).__init__()
        self.prefix = prefix
        self.attn_b = FlatSimilarityWrapper(x_size, h_size, prefix, opt,
                                            dropout)
        self.attn_e = FlatSimilarityWrapper(x_size, h_size, prefix, opt,
                                            dropout)
        self.rnn_type = opt.get('{}_rnn_type'.format(prefix), 'gru')
        self.rnn = RNN_MAP.get(self.rnn_type, nn.GRUCell)(x_size, h_size)
        self.num_turn = opt.get('{}_num_turn'.format(prefix), 5)
        self.opt = opt
        self.mem_random_drop = opt.get('{}_mem_drop_p'.format(prefix), 0)
        self.answer_opt = opt.get('{}_opt'.format(prefix), 0)
        self.mem_type = opt.get('{}_mem_type'.format(prefix), 0)
        self.gamma = opt.get('{}_mem_gamma'.format(prefix), 0.5)
        self.alpha = Parameter(torch.zeros(1, 1, 1))

        self.proj = nn.Linear(h_size, x_size) if h_size != x_size else None
        if dropout is None:
            self.dropout = DropoutWrapper(
                opt.get('{}_dropout_p'.format(self.prefix), 0))
        else:
            self.dropout = dropout