コード例 #1
0
ファイル: config.py プロジェクト: ruihan0495/IR2-FA2020
    def change_param(self, playby, eval, update_count, update_reg, purpose, mod, mask):
        self.play_by = playby
        self.eval = eval
        self.update_count = update_count
        self.update_reg = update_reg
        self.purpose = purpose
        self.mod = mod
        self.mask = mask

        if self.mod == 'crm' or self.play_by == 'RO':
            category_max = 0
            feature_max = 0
            for k, v in self.item_dict.items():
                if max(v['categories']) > category_max:
                    category_max = max(v['categories'])
                if max(v['feature_index']) > feature_max:
                    feature_max = max(v['feature_index'])
            fp = '../../data/FM-model-merge/FM-model-command-6-pretrain-0.pt'

            model = FactorizationMachine(emb_size=64, user_length=len(self.user_list), item_length=len(self.item_dict),
                                         feature_length=feature_max + 1, qonly=1, command=6, hs=64, ip=0.01,
                                         dr=0.5, old_new='new')
            start = time.time()
            model.load_state_dict(torch.load(fp))
            print('load FM model {} takes: {} secs'.format(fp,time.time() - start))
            self.FM_model = cuda_(model)
コード例 #2
0
    def init_FM_related(self):
        # category_max = 0
        feature_max = 0
        for k, v in self.item_dict.items():
            # if max(v['categories']) > category_max:
            #     category_max = max(v['categories'])
            if max(v['feature_index']) > feature_max:
                feature_max = max(v['feature_index'])

        self.feature_count = feature_max + 1

        self.PN_model = None
        self.FM_model = None

        # fp = '../../data/FM-model-merge/FM-model-command-8-pretrain-2.pt'
        fp = '../../data/FM-model-merge/v4-FM-lr-0.01-flr-0.001-reg-0.002-decay-0-qonly-1-bs-64-command-6-hs-64-ip-0.01-dr-0.5-optim-Ada-oldnew-new-pretrain-0-uf-0-rd-0-freeze-0-seed-330-useremb-1epoch-442.pt'

        model = FactorizationMachine(emb_size=64, user_length=len(self.user_list), item_length=len(self.item_dict),
                                     feature_length=feature_max + 1, qonly=1, command=8, hs=64, ip=0.01,
                                     dr=0.5, old_new='new')


        start = time.time()
        model.load_state_dict(torch.load(fp))
        print('load FM model {} takes: {} secs'.format(fp, time.time() - start))
        self.emb_matrix = model.feature_emb.weight[..., :-1].detach().numpy()
        self.user_emb = model.ui_emb.weight[..., :-1].detach().numpy()
        self.FM_model = cuda_(model)
コード例 #3
0
    def change_param(self, playby, eval, update_count, update_reg, purpose, mod):
        self.play_by = playby
        self.eval = eval
        self.update_count = update_count
        self.update_reg = update_reg
        self.purpose = purpose
        self.mod = mod

        if self.mod == 'crm':
           category_max = 0
           feature_max = 0
           for k, v in self.item_dict.items():
               if max(v['categories']) > category_max:
                   category_max = max(v['categories'])
               if max(v['feature_index']) > feature_max:
                   feature_max = max(v['feature_index'])
           fp = '../../data/FM-model-merge/v32-test-FM-lr-0.01-flr-0.0001-reg-0.002-decay-0.0-qonly-1-bs-64-command-6-hs-64-ip-0.01-dr-0.5-optim-Ada-oldnew-new-pretrain-0-uf-0-rd-0-freeze-0-seed-3702-useremb-1epoch-45.pt'
           fp = '../../data/FM-model-merge/v32-test-FM-lr-0.01-flr-0.0001-reg-0.002-decay-0.0-qonly-1-bs-64-command-6-hs-64-ip-0.01-dr-0.5-optim-Ada-oldnew-new-pretrain-0-uf-0-rd-0-freeze-0-seed-3704-useremb-1epoch-49.pt'

           fp = '../../data/FM-model-merge/v32-test-FM-lr-0.01-flr-0.001-reg-0.005-decay-0.0-qonly-1-bs-64-command-8-hs-64-ip-0.01-dr-0.5-optim-Ada-oldnew-new-pretrain-2-uf-1-rd-0-freeze-0-seed-3702-useremb-1epoch-5.pt'

           model = FactorizationMachine(emb_size=64, user_length=len(self.user_list), item_length=len(self.item_dict),
                                        feature_length=feature_max + 1, qonly=1, command=6, hs=64, ip=0.01,
                                        dr=0.5, old_new='new')
           start = time.time()
           model.load_state_dict(torch.load(fp))
           print('load FM model {} takes: {} secs, but hypyer parameters can be mistaken'.format(fp,
                                                                                                 time.time() - start))
           #self.emb_matrix = model.feature_emb.weight[..., :-1].detach().numpy()
           #self.user_emb = model.ui_emb.weight[..., :-1].detach().numpy()
           self.FM_model = cuda_(model)
コード例 #4
0
    def init_FM_related(self):
        city_max = 0
        category_max = 0
        feature_max = 0
        for k, v in self.item_dict.items():

            if max(v['categories']) > category_max:
                category_max = max(v['categories'])
            if max(v['feature_index']) > feature_max:
                feature_max = max(v['feature_index'])

        # print('feature max = {}'.format(feature_max))

        stars_list = [1, 2, 3, 4, 5]
        price_list = [1, 2, 3, 4]
        self.star_count, self.price_count = len(stars_list), len(price_list)
        self.city_count, self.category_count, self.feature_count = city_max + 1, category_max + 1, feature_max + 1

        self.city_span = (0, self.city_count)
        self.star_span = (self.city_count, self.city_count + self.star_count)
        self.price_span = (self.city_count + self.star_count, self.city_count +
                           self.star_count + self.price_count)

        self.spans = [self.city_span, self.star_span, self.price_span]

        print('city max: {}, category max: {}, feature max: {}'.format(
            self.city_count, self.category_count, self.feature_count))
        fp = '../../data/FM-model-merge/lastfm-FM.pt'
        model = FactorizationMachine(emb_size=64,
                                     user_length=len(self.user_list),
                                     item_length=len(self.busi_list),
                                     feature_length=feature_max + 1,
                                     qonly=1,
                                     command=8,
                                     hs=64,
                                     ip=0.01,
                                     dr=0.5,
                                     old_new='new')
        model.load_state_dict(torch.load(fp, map_location='cpu'))
        print('load FM model {}, but hypyer parameters can be mistaken'.format(
            fp))
        self.emb_matrix = model.feature_emb.weight[..., :-1].detach().numpy()
        self.user_emb = model.ui_emb.weight[..., :-1].detach().numpy()

        avr_user_emb = np.zeros((1, emb_size))
        for i in range(self.old_users):
            avr_user_emb += self.user_emb[[i]].reshape(1, emb_size)
        avr_user_emb = avr_user_emb / self.old_users
        for j in range(self.old_users, len(self.user_list)):
            self.user_emb[[j]] = avr_user_emb

        self.FM_model = cuda_(model)
コード例 #5
0
    def init_FM_related(self):
        city_max = 0
        category_max = 0
        feature_max = 0
        for k, v in self.item_dict.items():
            if v['city'] > city_max:
                city_max = v['city']
            if max(v['categories']) > category_max:
                category_max = max(v['categories'])
            if max(v['feature_index']) > feature_max:
                feature_max = max(v['feature_index'])

        stars_list = [1, 2, 3, 4, 5]
        price_list = [1, 2, 3, 4]
        self.star_count, self.price_count = len(stars_list), len(price_list)
        self.city_count, self.category_count, self.feature_count = city_max + 1, category_max + 1, feature_max + 1

        self.city_span = (0, self.city_count)
        self.star_span = (self.city_count, self.city_count + self.star_count)
        self.price_span = (self.city_count + self.star_count, self.city_count +
                           self.star_count + self.price_count)

        self.spans = [self.city_span, self.star_span, self.price_span]

        print('city max: {}, category max: {}, feature max: {}'.format(
            self.city_count, self.category_count, self.feature_count))
        fp = '../../data/FM-model-merge/FM-model-command-8-pretrain-2.pt'
        model = FactorizationMachine(emb_size=64,
                                     user_length=len(self.user_list),
                                     item_length=len(self.item_dict),
                                     feature_length=feature_max + 1,
                                     qonly=1,
                                     command=8,
                                     hs=64,
                                     ip=0.01,
                                     dr=0.5,
                                     old_new='new')
        model.load_state_dict(torch.load(fp, map_location=torch.device('cpu')))
        print('load FM model {}'.format(fp))
        self.emb_matrix = model.feature_emb.weight[..., :-1].detach().numpy()
        self.user_emb = model.ui_emb.weight[..., :-1].detach().numpy()
        self.FM_model = cuda_(model)
コード例 #6
0
ファイル: config.py プロジェクト: ruihan0495/IR2-FA2020
    def init_FM_related(self):
        category_max = 0
        feature_max = 0
        for k, v in self.item_dict.items():
            if max(v['categories']) > category_max:
                category_max = max(v['categories'])
            if max(v['feature_index']) > feature_max:
                feature_max = max(v['feature_index'])

        self.feature_count = feature_max + 1

        self.PN_model = None
        self.FM_model = None

        fp = '../../data/FM-model-merge/FM-model-command-8-pretrain-2.pt'


        model = FactorizationMachine(emb_size=64, user_length=len(self.user_list), item_length=len(self.item_dict),
                                     feature_length=feature_max + 1, qonly=1, command=8, hs=64, ip=0.01,
                                     dr=0.5, old_new='new')


        start = time.time()
        if torch.cuda.is_available():
            model.load_state_dict(torch.load(fp))
        else:
            model.load_state_dict(torch.load(fp, map_location=torch.device('cpu')))

        print('load FM model {} takes: {} secs'.format(fp, time.time() - start))
        self.emb_matrix = model.feature_emb.weight[..., :-1].detach().numpy()
        self.user_emb = model.ui_emb.weight[..., :-1].detach().numpy()
        self.FM_model = cuda_(model)
コード例 #7
0
ファイル: FM_old_train.py プロジェクト: SuBoYu/CRS_Chatbot
def main():
    parser = argparse.ArgumentParser(description="Run FM")
    parser.add_argument('-lr',
                        type=float,
                        metavar='<lr>',
                        dest='lr',
                        help='lr',
                        default=0.01)
    parser.add_argument('-flr',
                        type=float,
                        metavar='<flr>',
                        dest='flr',
                        help='flr',
                        default=0.001)
    # means the learning rate of feature similarity learning
    parser.add_argument('-reg',
                        type=float,
                        metavar='<reg>',
                        dest='reg',
                        help='reg',
                        default=0.002)
    # regularization
    parser.add_argument('-decay',
                        type=float,
                        metavar='<decay>',
                        dest='decay',
                        help='decay',
                        default=0)
    # weight decay
    parser.add_argument('-qonly',
                        type=int,
                        metavar='<qonly>',
                        dest='qonly',
                        help='qonly',
                        default=1)
    # means quadratic form only (letting go other terms in FM equation...)
    parser.add_argument('-bs',
                        type=int,
                        metavar='<bs>',
                        dest='bs',
                        help='bs',
                        default=64)
    # batch size
    parser.add_argument('-hs',
                        type=int,
                        metavar='<hs>',
                        dest='hs',
                        help='hs',
                        default=64)
    # hidden size
    parser.add_argument('-ip',
                        type=float,
                        metavar='<ip>',
                        dest='ip',
                        help='ip',
                        default=0.01)
    # init parameter for hidden
    parser.add_argument('-dr',
                        type=float,
                        metavar='<dr>',
                        dest='dr',
                        help='dr',
                        default=0.5)
    # dropout ratio
    parser.add_argument('-optim',
                        type=str,
                        metavar='<optim>',
                        dest='optim',
                        help='optim',
                        default='Ada')
    # optimizer
    parser.add_argument('-observe',
                        type=int,
                        metavar='<observe>',
                        dest='observe',
                        help='observe',
                        default=1)
    # the frequency of doing evaluation
    parser.add_argument('-oldnew',
                        type=str,
                        metavar='<oldnew>',
                        dest='oldnew',
                        help='oldnew',
                        default='new')
    # we don't use this parameter now
    parser.add_argument('-pretrain',
                        type=int,
                        metavar='<pretrain>',
                        dest='pretrain',
                        help='pretrain',
                        default=0)
    # does it need to load pretrain model?
    parser.add_argument('-uf',
                        type=int,
                        metavar='<uf>',
                        dest='uf',
                        help='uf',
                        default=0)
    # update feature
    parser.add_argument('-rd',
                        type=int,
                        metavar='<rd>',
                        dest='rd',
                        help='rd',
                        default=0)
    # remove duplicate, we don;t use this parameter now
    parser.add_argument('-useremb',
                        type=int,
                        metavar='<useremb>',
                        dest='useremb',
                        help='user embedding',
                        default=1)
    # update user embedding during feature similarity
    parser.add_argument('-freeze',
                        type=int,
                        metavar='<freeze>',
                        dest='freeze',
                        help='freeze',
                        default=0)
    # we don't use this param now
    parser.add_argument('-command',
                        type=int,
                        metavar='<command>',
                        dest='command',
                        help='command',
                        default=6)
    # command = 6: normal FM
    # command = 8: with our second type of negative sample
    parser.add_argument('-seed',
                        type=int,
                        metavar='<seed>',
                        dest='seed',
                        help='seed',
                        default=330)
    # random seed
    A = parser.parse_args()

    user_length = len(user_list)
    item_length = len(busi_list)

    random.seed(A.seed)
    np.random.seed(A.seed)
    torch.manual_seed(A.seed)
    torch.cuda.manual_seed(A.seed)

    if A.pretrain == 0:
        # means no pretrain
        model = FactorizationMachine(emb_size=A.hs,
                                     user_length=user_length,
                                     item_length=item_length,
                                     feature_length=FEATURE_COUNT,
                                     qonly=A.qonly,
                                     command=A.command,
                                     hs=A.hs,
                                     ip=A.ip,
                                     dr=A.dr,
                                     old_new=A.oldnew)

    if A.pretrain == 2:
        model = FactorizationMachine(emb_size=A.hs,
                                     user_length=user_length,
                                     item_length=item_length,
                                     feature_length=FEATURE_COUNT,
                                     qonly=A.qonly,
                                     command=A.command,
                                     hs=A.hs,
                                     ip=A.ip,
                                     dr=A.dr,
                                     old_new=A.oldnew)
        fp = '../../data/FM-model-merge/v4-FM-lr-0.01-flr-0.001-reg-0.002-decay-0.0-qonly-1-bs-64-command-8-hs-64-ip-0.01-dr-0.5-optim-SGD-oldnew-new-pretrain-0-uf-0-rd-0-freeze-0-seed-3812-useremb-1epoch-49.pt'
        print(fp)
        model.load_state_dict(torch.load(fp))
    cuda_(model)

    param1, param2 = list(), list()
    param3 = list()

    i = 0
    for name, param in model.named_parameters():
        print(name, param)
        if i == 0:
            param1.append(param)
        else:
            param2.append(param)
        if i == 2:
            param3.append(param)
        i += 1

    print(
        'param1 is: {}, shape:{}\nparam2 is: {}, shape: {}\nparam3 is: {}, shape: {}\n'
        .format(param1, [param.shape for param in param1], param2,
                [param.shape for param in param2], param3,
                [param.shape for param in param3]))
    bs = A.bs
    max_epoch = 250

    if A.optim == 'SGD':
        optimizer1 = torch.optim.SGD(param1, lr=A.lr, weight_decay=0.1)
        optimizer2 = torch.optim.SGD(param2, lr=A.lr)
        optimizer3 = torch.optim.SGD(param3, lr=A.flr)
    if A.optim == 'Ada':
        optimizer1 = torch.optim.Adagrad(param1, lr=A.lr, weight_decay=A.decay)
        optimizer2 = torch.optim.Adagrad(param2, lr=A.lr, weight_decay=A.decay)
        optimizer3 = torch.optim.Adagrad(param3,
                                         lr=A.flr,
                                         weight_decay=A.decay)

    reg_ = torch.Tensor([A.reg])
    reg_ = torch.autograd.Variable(reg_, requires_grad=False)
    reg_ = cuda_(reg_)

    file_name = 'v4-FM-lr-{}-flr-{}-reg-{}-decay-{}-qonly-{}-bs-{}-command-{}-hs-{}-ip-{}-dr-{}-optim-{}-oldnew-{}-pretrain-{}-uf-{}-rd-{}-freeze-{}-seed-{}-useremb-{}'.format(
        A.lr, A.flr, A.reg, A.decay, A.qonly, A.bs, A.command, A.hs, A.ip,
        A.dr, A.optim, A.oldnew, A.pretrain, A.uf, A.rd, A.freeze, A.seed,
        A.useremb)

    model = train(model, bs, max_epoch, optimizer1, optimizer2, optimizer3,
                  reg_, A.qonly, A.observe, A.command, file_name, A.uf,
                  A.useremb)