Exemplo n.º 1
0
    def __init__(self, config, dataset):
        super(DSSM, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']

        self.user_feature_num = self.user_token_field_num + self.user_float_field_num + self.user_token_seq_field_num
        self.item_feature_num = self.item_token_field_num + self.item_float_field_num + self.item_token_seq_field_num
        user_size_list = [self.embedding_size * self.user_feature_num
                          ] + self.mlp_hidden_size
        item_size_list = [self.embedding_size * self.item_feature_num
                          ] + self.mlp_hidden_size

        # define layers and loss
        self.user_mlp_layers = MLPLayers(user_size_list,
                                         self.dropout_prob,
                                         activation='tanh',
                                         bn=True)
        self.item_mlp_layers = MLPLayers(item_size_list,
                                         self.dropout_prob,
                                         activation='tanh',
                                         bn=True)

        self.loss = nn.BCELoss()
        self.sigmoid = nn.Sigmoid()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 2
0
    def __init__(self, config, dataset):
        super(NAIS, self).__init__(config, dataset)

        # load dataset info
        self.LABEL = config['LABEL_FIELD']
        self.logger = getLogger()

        # get all users's history interaction information.the history item
        # matrix is padding by the maximum number of a user's interactions
        self.history_item_matrix, self.history_lens, self.mask_mat = self.get_history_info(
            dataset)

        # load parameters info
        self.embedding_size = config['embedding_size']
        self.weight_size = config['weight_size']
        self.algorithm = config['algorithm']
        self.reg_weights = config['reg_weights']
        self.alpha = config['alpha']
        self.beta = config['beta']
        self.split_to = config['split_to']
        self.pretrain_path = config['pretrain_path']

        # split the too large dataset into the specified pieces
        if self.split_to > 0:
            self.logger.info('split the n_items to {} pieces'.format(
                self.split_to))
            self.group = torch.chunk(
                torch.arange(self.n_items).to(self.device), self.split_to)

        # define layers and loss
        # construct source and destination item embedding matrix
        self.item_src_embedding = nn.Embedding(self.n_items,
                                               self.embedding_size,
                                               padding_idx=0)
        self.item_dst_embedding = nn.Embedding(self.n_items,
                                               self.embedding_size,
                                               padding_idx=0)
        self.bias = nn.Parameter(torch.zeros(self.n_items))
        if self.algorithm == 'concat':
            self.mlp_layers = MLPLayers(
                [self.embedding_size * 2, self.weight_size])
        elif self.algorithm == 'prod':
            self.mlp_layers = MLPLayers(
                [self.embedding_size, self.weight_size])
        else:
            raise ValueError(
                "NAIS just support attention type in ['concat', 'prod'] but get {}"
                .format(self.algorithm))
        self.weight_layer = nn.Parameter(torch.ones(self.weight_size, 1))
        self.bceloss = nn.BCELoss()

        # parameters initialization
        if self.pretrain_path is not None:
            self.logger.info('use pretrain from [{}]...'.format(
                self.pretrain_path))
            self._load_pretrain()
        else:
            self.logger.info('unuse pretrain...')
            self.apply(self._init_weights)
Exemplo n.º 3
0
    def __init__(self, config, dataset):
        super(DIN, self).__init__(config, dataset)

        # get field names and parameter value from config
        self.LABEL_FIELD = config['LABEL_FIELD']
        self.embedding_size = config['embedding_size']
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.device = config['device']
        self.pooling_mode = config['pooling_mode']
        self.dropout_prob = config['dropout_prob']

        self.types = ['user', 'item']
        self.user_feat = dataset.get_user_feature()
        self.item_feat = dataset.get_item_feature()

        # init MLP layers
        # self.dnn_list = [(3 * self.num_feature_field['item'] + self.num_feature_field['user'])
        #                  * self.embedding_size] + self.mlp_hidden_size
        num_item_feature = len(self.item_feat.interaction.keys())
        self.dnn_list = [3 * num_item_feature * self.embedding_size] + self.mlp_hidden_size
        self.att_list = [4 * num_item_feature * self.embedding_size] + self.mlp_hidden_size

        mask_mat = torch.arange(self.max_seq_length).to(self.device).view(1, -1)  # init mask
        self.attention = SequenceAttLayer(
            mask_mat, self.att_list, activation='Sigmoid', softmax_stag=False, return_seq_weight=False
        )
        self.dnn_mlp_layers = MLPLayers(self.dnn_list, activation='Dice', dropout=self.dropout_prob, bn=True)

        self.embedding_layer = ContextSeqEmbLayer(dataset, self.embedding_size, self.pooling_mode, self.device)
        self.dnn_predict_layers = nn.Linear(self.mlp_hidden_size[-1], 1)
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        self.apply(self._init_weights)
Exemplo n.º 4
0
    def __init__(self, config, dataset):
        super(ConvNCF, self).__init__(config, dataset)

        # load dataset info
        self.LABEL = config['LABEL_FIELD']

        # load parameters info
        self.embedding_size = config['embedding_size']
        self.cnn_channels = config['cnn_channels']
        self.cnn_kernels = config['cnn_kernels']
        self.cnn_strides = config['cnn_strides']
        self.dropout_prob = config['dropout_prob']
        self.regs = config['reg_weights']

        # define layers and loss
        self.user_embedding = nn.Embedding(self.n_users, self.embedding_size)
        self.item_embedding = nn.Embedding(self.n_items, self.embedding_size)
        self.cnn_layers = CNNLayers(self.cnn_channels,
                                    self.cnn_kernels,
                                    self.cnn_strides,
                                    activation='relu')
        self.predict_layers = MLPLayers([self.cnn_channels[-1], 1],
                                        self.dropout_prob,
                                        activation='none')
        self.loss = ConvNCFBPRLoss()
Exemplo n.º 5
0
    def __init__(self, config, dataset):
        super(PNN, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']
        self.use_inner = config['use_inner']
        self.use_outer = config['use_outer']
        self.reg_weight = config['reg_weight']

        self.num_pair = int(self.num_feature_field *
                            (self.num_feature_field - 1) / 2)

        # define layers and loss
        product_out_dim = self.num_feature_field * self.embedding_size
        if self.use_inner:
            product_out_dim += self.num_pair
            self.inner_product = InnerProductLayer(self.num_feature_field,
                                                   device=self.device)

        if self.use_outer:
            product_out_dim += self.num_pair
            self.outer_product = OuterProductLayer(self.num_feature_field,
                                                   self.embedding_size,
                                                   device=self.device)
        size_list = [product_out_dim] + self.mlp_hidden_size
        self.mlp_layers = MLPLayers(size_list, self.dropout_prob, bn=False)
        self.predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1)
        self.relu = nn.ReLU()
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 6
0
    def __init__(self, config, dataset):
        super(DMF, self).__init__(config, dataset)

        # load dataset info
        self.LABEL = config['LABEL_FIELD']
        self.RATING = config['RATING_FIELD']

        # load parameters info
        self.user_embedding_size = config['user_embedding_size']
        self.item_embedding_size = config['item_embedding_size']
        self.user_hidden_size_list = config['user_hidden_size_list']
        self.item_hidden_size_list = config['item_hidden_size_list']
        # The dimensions of the last layer of users and items must be the same
        assert self.user_hidden_size_list[-1] == self.item_hidden_size_list[-1]
        self.inter_matrix_type = config['inter_matrix_type']

        # generate intermediate data
        if self.inter_matrix_type == '01':
            self.history_user_id, self.history_user_value, _ = dataset.history_user_matrix()
            self.history_item_id, self.history_item_value, _ = dataset.history_item_matrix()
            self.interaction_matrix = dataset.inter_matrix(form='csr').astype(np.float32)
        elif self.inter_matrix_type == 'rating':
            self.history_user_id, self.history_user_value, _ = dataset.history_user_matrix(value_field=self.RATING)
            self.history_item_id, self.history_item_value, _ = dataset.history_item_matrix(value_field=self.RATING)
            self.interaction_matrix = dataset.inter_matrix(form='csr', value_field=self.RATING).astype(np.float32)
        else:
            raise ValueError("The inter_matrix_type must in ['01', 'rating'] but get {}".format(self.inter_matrix_type))
        self.max_rating = self.history_user_value.max()
        # tensor of shape [n_items, H] where H is max length of history interaction.
        self.history_user_id = self.history_user_id.to(self.device)
        self.history_user_value = self.history_user_value.to(self.device)
        self.history_item_id = self.history_item_id.to(self.device)
        self.history_item_value = self.history_item_value.to(self.device)

        # define layers
        self.user_linear = nn.Linear(in_features=self.n_items, out_features=self.user_embedding_size, bias=False)
        self.item_linear = nn.Linear(in_features=self.n_users, out_features=self.item_embedding_size, bias=False)
        self.user_fc_layers = MLPLayers([self.user_embedding_size] + self.user_hidden_size_list)
        self.item_fc_layers = MLPLayers([self.item_embedding_size] + self.item_hidden_size_list)
        self.sigmoid = nn.Sigmoid()
        self.bce_loss = nn.BCELoss()

        # Save the item embedding before dot product layer to speed up evaluation
        self.i_embedding = None

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 7
0
    def __init__(self, config, dataset):
        super(DIEN, self).__init__(config, dataset)

        # get field names and parameter value from config
        self.device = config['device']
        self.alpha = config['alpha']
        self.gru = config['gru_type']
        self.pooling_mode = config['pooling_mode']
        self.dropout_prob = config['dropout_prob']
        self.LABEL_FIELD = config['LABEL_FIELD']
        self.embedding_size = config['embedding_size']
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.NEG_ITEM_SEQ = config['NEG_PREFIX'] + self.ITEM_SEQ

        self.types = ['user', 'item']
        self.user_feat = dataset.get_user_feature()
        self.item_feat = dataset.get_item_feature()

        num_item_feature = sum(
            1 if dataset.field2type[field] != FeatureType.FLOAT_SEQ else
            dataset.num(field) for field in self.item_feat.interaction.keys())
        num_user_feature = sum(
            1 if dataset.field2type[field] != FeatureType.FLOAT_SEQ else
            dataset.num(field) for field in self.user_feat.interaction.keys())
        item_feat_dim = num_item_feature * self.embedding_size
        mask_mat = torch.arange(self.max_seq_length).to(self.device).view(
            1, -1)  # init mask

        # init sizes of used layers
        self.att_list = [4 * num_item_feature * self.embedding_size
                         ] + self.mlp_hidden_size
        self.interest_mlp_list = [2 * item_feat_dim
                                  ] + self.mlp_hidden_size + [1]
        self.dnn_mlp_list = [
            2 * item_feat_dim + num_user_feature * self.embedding_size
        ] + self.mlp_hidden_size

        # init interest extractor layer, interest evolving layer embedding layer, MLP layer and linear layer
        self.interset_extractor = InterestExtractorNetwork(
            item_feat_dim, item_feat_dim, self.interest_mlp_list)
        self.interest_evolution = InterestEvolvingLayer(mask_mat,
                                                        item_feat_dim,
                                                        item_feat_dim,
                                                        self.att_list,
                                                        gru=self.gru)
        self.embedding_layer = ContextSeqEmbLayer(dataset, self.embedding_size,
                                                  self.pooling_mode,
                                                  self.device)
        self.dnn_mlp_layers = MLPLayers(self.dnn_mlp_list,
                                        activation='Dice',
                                        dropout=self.dropout_prob,
                                        bn=True)
        self.dnn_predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1)
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        self.apply(self._init_weights)
        self.other_parameter_name = ['embedding_layer']
Exemplo n.º 8
0
    def __init__(self, config, dataset):
        super(xDeepFM, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.reg_weight = config['reg_weight']
        self.dropout_prob = config['dropout_prob']
        self.direct = config['direct']
        self.cin_layer_size = temp_cin_size = list(config['cin_layer_size'])

        # Check whether the size of the CIN layer is legal.
        if not self.direct:
            self.cin_layer_size = list(
                map(lambda x: int(x // 2 * 2), temp_cin_size))
            if self.cin_layer_size[:-1] != temp_cin_size[:-1]:
                logger = getLogger()
                logger.warning(
                    'Layer size of CIN should be even except for the last layer when direct is True.'
                    'It is changed to {}'.format(self.cin_layer_size))

        # Create a convolutional layer for each CIN layer
        self.conv1d_list = []
        self.field_nums = [self.num_feature_field]
        for i, layer_size in enumerate(self.cin_layer_size):
            conv1d = nn.Conv1d(self.field_nums[-1] * self.field_nums[0],
                               layer_size, 1).to(self.device)
            self.conv1d_list.append(conv1d)
            if self.direct:
                self.field_nums.append(layer_size)
            else:
                self.field_nums.append(layer_size // 2)

        # Create MLP layer
        size_list = [self.embedding_size * self.num_feature_field
                     ] + self.mlp_hidden_size + [1]
        self.mlp_layers = MLPLayers(size_list, dropout=self.dropout_prob)

        # Get the output size of CIN
        if self.direct:
            self.final_len = sum(self.cin_layer_size)
        else:
            self.final_len = sum(
                self.cin_layer_size[:-1]) // 2 + self.cin_layer_size[-1]

        self.cin_linear = nn.Linear(self.final_len, 1, bias=False)
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()
        self.apply(self._init_weights)
Exemplo n.º 9
0
    def __init__(self, config, dataset):
        super(MKR, self).__init__(config, dataset)

        # load parameters info
        self.LABEL = config['LABEL_FIELD']
        self.embedding_size = config['embedding_size']
        self.kg_embedding_size = config['kg_embedding_size']
        self.L = config['low_layers_num']  # the number of low layers
        self.H = config['high_layers_num']  # the number of high layers
        self.reg_weight = config['reg_weight']
        self.use_inner_product = config['use_inner_product']
        self.dropout_prob = config['dropout_prob']

        # init embeddings
        self.user_embeddings_lookup = nn.Embedding(self.n_users,
                                                   self.embedding_size)
        self.item_embeddings_lookup = nn.Embedding(self.n_entities,
                                                   self.embedding_size)
        self.entity_embeddings_lookup = nn.Embedding(self.n_entities,
                                                     self.embedding_size)
        self.relation_embeddings_lookup = nn.Embedding(self.n_relations,
                                                       self.embedding_size)

        # define layers
        lower_mlp_layers = []
        high_mlp_layers = []
        for i in range(self.L + 1):
            lower_mlp_layers.append(self.embedding_size)
        for i in range(self.H):
            high_mlp_layers.append(self.embedding_size * 2)

        self.user_mlp = MLPLayers(lower_mlp_layers, self.dropout_prob,
                                  'sigmoid')
        self.tail_mlp = MLPLayers(lower_mlp_layers, self.dropout_prob,
                                  'sigmoid')
        self.cc_unit = nn.Sequential()
        for i_cnt in range(self.L):
            self.cc_unit.add_module('cc_unit{}'.format(i_cnt),
                                    CrossCompressUnit(self.embedding_size))
        self.kge_mlp = MLPLayers(high_mlp_layers, self.dropout_prob, 'sigmoid')
        self.kge_pred_mlp = MLPLayers(
            [self.embedding_size * 2, self.embedding_size], self.dropout_prob,
            'sigmoid')
        if self.use_inner_product == False:
            self.rs_pred_mlp = MLPLayers([self.embedding_size * 2, 1],
                                         self.dropout_prob, 'sigmoid')
            self.rs_mlp = MLPLayers(high_mlp_layers, self.dropout_prob,
                                    'sigmoid')

        # loss
        self.sigmoid_BCE = nn.BCEWithLogitsLoss()

        # parameters initialization
        self.apply(xavier_normal_initialization)
Exemplo n.º 10
0
    def __init__(self, config, dataset):
        super(WideDeep, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']

        # define layers and loss
        size_list = [self.embedding_size * self.num_feature_field
                     ] + self.mlp_hidden_size
        self.mlp_layers = MLPLayers(size_list, self.dropout_prob)
        self.deep_predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1)
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 11
0
    def __init__(self, config, dataset):
        super(NeuMF, self).__init__(config, dataset)

        # load dataset info
        self.LABEL = config['LABEL_FIELD']

        # load parameters info
        self.mf_embedding_size = config['mf_embedding_size']
        self.mlp_embedding_size = config['mlp_embedding_size']
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']
        self.mf_train = config['mf_train']
        self.mlp_train = config['mlp_train']
        self.use_pretrain = config['use_pretrain']
        self.mf_pretrain_path = config['mf_pretrain_path']
        self.mlp_pretrain_path = config['mlp_pretrain_path']

        # define layers and loss
        self.user_mf_embedding = nn.Embedding(self.n_users,
                                              self.mf_embedding_size)
        self.item_mf_embedding = nn.Embedding(self.n_items,
                                              self.mf_embedding_size)
        self.user_mlp_embedding = nn.Embedding(self.n_users,
                                               self.mlp_embedding_size)
        self.item_mlp_embedding = nn.Embedding(self.n_items,
                                               self.mlp_embedding_size)
        self.mlp_layers = MLPLayers([2 * self.mlp_embedding_size] +
                                    self.mlp_hidden_size)
        self.mlp_layers.logger = None  # remove logger to use torch.save()
        if self.mf_train and self.mlp_train:
            self.predict_layer = nn.Linear(
                self.mf_embedding_size + self.mlp_hidden_size[-1], 1,
                self.dropout_prob)
        elif self.mf_train:
            self.predict_layer = nn.Linear(self.mf_embedding_size, 1)
        elif self.mlp_train:
            self.predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1)
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        if self.use_pretrain:
            self.load_pretrain()
        else:
            self.apply(self._init_weights)
Exemplo n.º 12
0
    def __init__(self, config, dataset):
        super(NFM, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']

        # define layers and loss
        size_list = [self.embedding_size] + self.mlp_hidden_size
        self.fm = BaseFactorizationMachine(reduce_sum=False)
        self.bn = nn.BatchNorm1d(num_features=self.embedding_size)
        self.mlp_layers = MLPLayers(size_list, self.dropout_prob, activation='sigmoid', bn=True)
        self.predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1, bias=False)
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 13
0
    def __init__(self, config, dataset):
        super(DeepFM, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']

        # define layers and loss
        self.fm = BaseFactorizationMachine(reduce_sum=True)
        size_list = [self.embedding_size * self.num_feature_field
                     ] + self.mlp_hidden_size
        self.mlp_layers = MLPLayers(size_list, self.dropout_prob)
        self.deep_predict_layer = nn.Linear(
            self.mlp_hidden_size[-1], 1)  # Linear product to the final score
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 14
0
    def __init__(self, config, dataset):
        super(MultiDAE, self).__init__(config, dataset)

        self.layers = config["mlp_hidden_size"]
        self.lat_dim = config['latent_dimension']
        self.drop_out = config['dropout_prob']

        self.history_item_id, self.history_item_value, _ = dataset.history_item_matrix()
        self.history_item_id = self.history_item_id.to(self.device)
        self.history_item_value = self.history_item_value.to(self.device)

        self.encode_layer_dims = [self.n_items] + self.layers + [self.lat_dim]
        self.decode_layer_dims = [self.lat_dim] + self.encode_layer_dims[::-1][1:]

        self.encoder = MLPLayers(self.encode_layer_dims, activation='tanh')
        self.decoder = self.mlp_layers(self.decode_layer_dims)

        # parameters initialization
        self.apply(xavier_normal_initialization)
Exemplo n.º 15
0
    def __init__(self, config, dataset):
        super(DCN, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.cross_layer_num = config['cross_layer_num']
        self.reg_weight = config['reg_weight']
        self.dropout_prob = config['dropout_prob']

        # define layers and loss
        # init weight and bias of each cross layer
        self.cross_layer_w = nn.ParameterList(
            nn.Parameter(
                torch.randn(self.num_feature_field *
                            self.embedding_size).to(self.device))
            for _ in range(self.cross_layer_num))
        self.cross_layer_b = nn.ParameterList(
            nn.Parameter(
                torch.zeros(self.num_feature_field *
                            self.embedding_size).to(self.device))
            for _ in range(self.cross_layer_num))

        # size of mlp hidden layer
        size_list = [self.embedding_size * self.num_feature_field
                     ] + self.mlp_hidden_size
        # size of cross network output
        in_feature_num = self.embedding_size * self.num_feature_field + self.mlp_hidden_size[
            -1]

        self.mlp_layers = MLPLayers(size_list,
                                    dropout=self.dropout_prob,
                                    bn=True)
        self.predict_layer = nn.Linear(in_feature_num, 1)
        self.reg_loss = RegLoss()
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 16
0
    def __init__(self, config, dataset):
        super(FNN, self).__init__(config, dataset)

        # load parameters info
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.dropout_prob = config['dropout_prob']

        size_list = [self.embedding_size * self.num_feature_field
                     ] + self.mlp_hidden_size

        # define layers and loss
        self.mlp_layers = MLPLayers(size_list,
                                    self.dropout_prob,
                                    activation='tanh',
                                    bn=False)  # use tanh as activation
        self.predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1, bias=True)

        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 17
0
    def __init__(self, config, dataset):
        super(AutoInt, self).__init__(config, dataset)

        # load parameters info
        self.attention_size = config['attention_size']
        self.dropout_probs = config['dropout_probs']
        self.n_layers = config['n_layers']
        self.num_heads = config['num_heads']
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.has_residual = config['has_residual']

        # define layers and loss
        self.att_embedding = nn.Linear(self.embedding_size,
                                       self.attention_size)
        self.embed_output_dim = self.num_feature_field * self.embedding_size
        self.atten_output_dim = self.num_feature_field * self.attention_size
        size_list = [self.embed_output_dim] + self.mlp_hidden_size
        self.mlp_layers = MLPLayers(size_list, dropout=self.dropout_probs[1])
        # multi-head self-attention network
        self.self_attns = nn.ModuleList([
            nn.MultiheadAttention(self.attention_size,
                                  self.num_heads,
                                  dropout=self.dropout_probs[0])
            for _ in range(self.n_layers)
        ])
        self.attn_fc = torch.nn.Linear(self.atten_output_dim, 1)
        self.deep_predict_layer = nn.Linear(self.mlp_hidden_size[-1], 1)
        if self.has_residual:
            self.v_res_res_embedding = torch.nn.Linear(self.embedding_size,
                                                       self.attention_size)

        self.dropout_layer = nn.Dropout(p=self.dropout_probs[2])
        self.sigmoid = nn.Sigmoid()
        self.loss = nn.BCELoss()

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 18
0
    def __init__(self, config, dataset):
        super(NNCF, self).__init__(config, dataset)

        # load dataset info
        self.LABEL = config['LABEL_FIELD']
        self.interaction_matrix = dataset.inter_matrix(form='coo').astype(
            np.float32)

        # load parameters info
        self.ui_embedding_size = config['ui_embedding_size']
        self.neigh_embedding_size = config['neigh_embedding_size']
        self.num_conv_kernel = config['num_conv_kernel']
        self.conv_kernel_size = config['conv_kernel_size']
        self.pool_kernel_size = config['pool_kernel_size']
        self.mlp_hidden_size = config['mlp_hidden_size']
        self.neigh_num = config['neigh_num']
        self.neigh_info_method = config['neigh_info_method']
        self.resolution = config['resolution']

        # define layers and loss
        self.user_embedding = nn.Embedding(self.n_users,
                                           self.ui_embedding_size)
        self.item_embedding = nn.Embedding(self.n_items,
                                           self.ui_embedding_size)
        self.user_neigh_embedding = nn.Embedding(self.n_items,
                                                 self.neigh_embedding_size)
        self.item_neigh_embedding = nn.Embedding(self.n_users,
                                                 self.neigh_embedding_size)
        self.user_conv = nn.Sequential(
            nn.Conv1d(self.neigh_embedding_size, self.num_conv_kernel,
                      self.conv_kernel_size),
            nn.MaxPool1d(self.pool_kernel_size), nn.ReLU())
        self.item_conv = nn.Sequential(
            nn.Conv1d(self.neigh_embedding_size, self.num_conv_kernel,
                      self.conv_kernel_size),
            nn.MaxPool1d(self.pool_kernel_size), nn.ReLU())
        conved_size = self.neigh_num - (self.conv_kernel_size - 1)
        pooled_size = (conved_size - (self.pool_kernel_size - 1) -
                       1) // self.pool_kernel_size + 1
        self.mlp_layers = MLPLayers(
            [2 * pooled_size * self.num_conv_kernel + self.ui_embedding_size] +
            self.mlp_hidden_size, config['dropout'])
        self.out_layer = nn.Sequential(nn.Linear(self.mlp_hidden_size[-1], 1),
                                       nn.Sigmoid())
        self.dropout_layer = torch.nn.Dropout(p=config['dropout'])
        self.loss = nn.BCELoss()

        # choose the method to use neighborhood information
        if self.neigh_info_method == "random":
            self.u_neigh, self.i_neigh = self.get_neigh_random()
        elif self.neigh_info_method == "knn":
            self.u_neigh, self.i_neigh = self.get_neigh_knn()
        elif self.neigh_info_method == "louvain":
            self.u_neigh, self.i_neigh = self.get_neigh_louvain()
        else:
            raise RuntimeError(
                'You need to choose the right algorithm of processing neighborhood information. \
                The parameter neigh_info_method can be set to random, knn or louvain.'
            )

        # parameters initialization
        self.apply(self._init_weights)
Exemplo n.º 19
0
 def __init__(self, input_size, hidden_size, mlp_size):
     super(InterestExtractorNetwork, self).__init__()
     self.gru = nn.GRU(input_size=input_size,
                       hidden_size=hidden_size,
                       batch_first=True)
     self.auxiliary_net = MLPLayers(layers=mlp_size, activation='Sigmoid')