コード例 #1
0
    def trans_core(self, seq_data, is_train=True):
        interest_state_lst = []
        for i,sequence in enumerate(seq_data):
            stag = 'sequence_' + str(i)
            seq_mask, seq_lens, seq_emb, tar_sku_emb, seq_ts_emb = sequence
            with tf.variable_scope('trans_' + stag, reuse=tf.AUTO_REUSE):
                m = TransformerModel(self.wnd_conf)

                if(self.wnd_conf.is_trans_input_by_mlp):
                    seq_emb = tf.layers.dense(seq_emb, self.wnd_conf.d_model, name='dense_trans_seq_' + stag)
                    tar_sku_emb = tf.layers.dense(tar_sku_emb, self.wnd_conf.d_model, name='dense_trans_sku_' + stag)

                seq_key = seq_emb
                seq_key_lens=seq_lens
                seq_key_ts_emb = seq_ts_emb
                seq_q = tf.expand_dims(tar_sku_emb, axis=1)   #[N, 1, d]
                seq_q_lens = tf.ones_like(seq_q[:, 0, 0], dtype=tf.int32) #[N]
                input = (seq_q, seq_q_lens, seq_key, seq_key_lens, seq_key_ts_emb)
                user_stat = m.encode_decode(input, name="encode_decode_" + stag, training=is_train)

                print("is_trans_out_concat_item:", self.wnd_conf.is_trans_out_concat_item)
                print("is_trans_out_by_mlp:", self.wnd_conf.is_trans_out_by_mlp)

                #method 1: [user_stat, tar_sku_emb]
                if(self.wnd_conf.is_trans_out_concat_item):
                    final_state = tf.concat([user_stat, tar_sku_emb], axis=-1)
                    if(self.wnd_conf.is_trans_out_by_mlp):
                        final_state = tf.layers.dense(final_state, self.wnd_conf.d_model, name='dense_trans_concat_' + stag)
                #method 2: user_stat
                else:
                    final_state = user_stat
            interest_state_lst.append(final_state)
        interest_state = tf.concat(interest_state_lst, -1)
        print("interest_state:", interest_state.get_shape().as_list())
        return interest_state
コード例 #2
0
ファイル: train.py プロジェクト: ajhoop/MIDS_Capstone
    def __init__(self, learning_rate=1e-3, config=None):
        super().__init__()
        self.save_hyperparameters()

        self.linear1 = torch.nn.Linear(in_features=2048, out_features=2048)
        self.bn1 = torch.nn.BatchNorm1d(num_features=2048)
        self.linear2 = torch.nn.Linear(in_features=2048, out_features=5386)
        self.bn2 = torch.nn.BatchNorm1d(num_features=5386)
        self.dropout = torch.nn.Dropout(p=0.001)

        self.valid_acc_1 = pl.metrics.Accuracy(compute_on_step=False)
        self.valid_acc_2 = pl.metrics.Accuracy(compute_on_step=False)
        self.valid_acc_3 = pl.metrics.Accuracy(compute_on_step=False)
        self.valid_acc_4 = pl.metrics.Accuracy(compute_on_step=False)
        self.valid_acc_5 = pl.metrics.Accuracy(compute_on_step=False)


        print("Using configuration file : config.json")
        self.config = json.loads(re.sub(r'#.*?\n', '', open('config.json', 'r').read()))

        self.tx           = TransformerModel.TransformerNet(
                            num_src_vocab = int(self.config['num_src_vocab']), 
                            embedding_dim = int(self.config['embedding_dim']), 
                            hidden_size   = int(self.config['hidden_size']), 
                            nheads        = int(self.config['nheads']), 
                            n_layers      = int(self.config['n_layers']), 
                            max_src_len   = int(self.config['padding_length']), 
                            dropout       = float(self.config['dropout']))
コード例 #3
0
 def __init__(self):
     self.config = json.loads(
         re.sub(r'#.*?\n', '',
                open('config.json', 'r').read()))
     self.predict = TransformerModel.Predict()
     self.hts_map = pd.read_csv(self.config['hts_map'],
                                dtype={
                                    'hs': str,
                                    'desc': str
                                })
コード例 #4
0
    def __init__(self, learning_rate=0.00001, config=None):
        super().__init__()
        self.save_hyperparameters()
        self.loss = torch.nn.MSELoss()

        self.model = TransformerModel.TransformerNet(
            num_src_vocab=int(config['num_src_vocab']),
            embedding_dim=int(config['embedding_dim']),
            hidden_size=int(config['hidden_size']),
            nheads=int(config['nheads']),
            n_layers=int(config['n_layers']),
            max_src_len=int(config['padding_length']),
            dropout=float(config['dropout']))

        #IPython.embed(); exit(1)
        self.group_size = int(config['group_size'])
        self.padding_length = int(config['padding_length'])
        self.embedding_dim = int(config['embedding_dim'])
        self.batch_size = int(config['rf_batch_size'])
        self.load_state_dict(torch.load(config['lm_save_file']))
        self.model.train()
コード例 #5
0
ファイル: do_inference.py プロジェクト: ajhoop/MIDS_Capstone
import re
from sklearn.metrics import accuracy_score
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score

print("Using configuration file : config.json")
config = json.loads(re.sub(r'#.*?\n', '', open('config.json', 'r').read()))

df_val = pd.read_csv(config['test_csv'],
                     dtype={
                         'label': str,
                         'text': str,
                         'Index': int
                     })

predict = TransformerModel.Predict()
predict.setup_dataloader(df_val)

#df_val['pred_label'], df_val['cos_sim'] = predict.do_inference()
prob = predict.do_inference(method='prob')

df_val['first'], df_val['second'], df_val['third'] = list(prob[0][:, 0]), list(
    prob[0][:, 1]), list(prob[0][:, 2])
print('Accuracy : first (probablity)',
      accuracy_score(df_val['label'], df_val['first']))
print(
    'Accuracy : first + second ',
    accuracy_score(df_val['label'], df_val['first']) +
    accuracy_score(df_val['label'], df_val['second']))
#print('Accuracy : first + second + third ', (accuracy_score(df_val['label'], df_val['first']) +
#                                             accuracy_score(df_val['label'], df_val['second']) +
コード例 #6
0
#!/usr/bin/env python3

import IPython
import sys
import pandas as pd
import transformers

sys.path.insert(0, '.')
import TransformerModel
import pickle
import json
import re

print("Using configuration file : config.json")
config = json.loads(re.sub(r'#.*?\n', '', open('config.json', 'r').read()))

retrofit_trained = TransformerModel.Misc()
df_train = pd.read_csv(config['train_csv'], dtype={'label': str, 'Index': int})
retrofit_trained.get_embedding(df_train)