Exemplo n.º 1
0
def train(config):
    # set seed
    set_seed(config.seed, cuda=config.use_cuda)

    # load dataset
    train = Interactions(config.train_root)
    # transform triplets to sequence representation
    train.to_sequence(config.L, config.T)

    test = Interactions(config.test_root,
                        user_map=train.user_map,
                        item_map=train.item_map)

    # print(config)
    # print(model_config)
    # fit model
    model = Recommender(n_iter=config.n_iter,
                        batch_size=config.batch_size,
                        learning_rate=config.learning_rate,
                        l2=config.l2,
                        neg_samples=config.neg_samples,
                        model_args=model_config,
                        use_cuda=config.use_cuda)

    return model.fit(train, test, verbose=True)
Exemplo n.º 2
0
def user_based_train_test_split(interactions,
                                test_percentage=0.2,
                                random_state=None):
    """
    Split interactions between a train and a test set based on
    user ids, so that a given user's entire interaction history
    is either in the train, or the test set.
    Parameters
    ----------
    interactions: :class:`spotlight.interactions.Interactions`
        The interactions to shuffle.
    test_percentage: float, optional
        The fraction of users to place in the test set.
    random_state: np.random.RandomState, optional
        The random state used for the shuffle.
    Returns
    -------
    (train, test): (:class:`spotlight.interactions.Interactions`,
                    :class:`spotlight.interactions.Interactions`)
         A tuple of (train data, test data)
    """

    if random_state is None:
        random_state = np.random.RandomState()

    minint = np.iinfo(np.uint32).min
    maxint = np.iinfo(np.uint32).max

    seed = random_state.randint(minint, maxint)

    in_test = ((murmurhash3_32(
        interactions.user_ids.astype(np.int32), seed=seed, positive=True) %
                100 / 100.0) < test_percentage)
    in_train = np.logical_not(in_test)

    train = Interactions(interactions.user_ids[in_train],
                         interactions.item_ids[in_train],
                         ratings=_slice_or_none(interactions.ratings,
                                                in_train),
                         timestamps=_slice_or_none(interactions.timestamps,
                                                   in_train),
                         weights=_slice_or_none(interactions.weights,
                                                in_train),
                         num_users=interactions.num_users,
                         num_items=interactions.num_items)
    test = Interactions(interactions.user_ids[in_test],
                        interactions.item_ids[in_test],
                        ratings=_slice_or_none(interactions.ratings, in_test),
                        timestamps=_slice_or_none(interactions.timestamps,
                                                  in_test),
                        weights=_slice_or_none(interactions.weights, in_test),
                        num_users=interactions.num_users,
                        num_items=interactions.num_items)

    return train, test
Exemplo n.º 3
0
def random_train_test_split(interactions,
                            test_percentage=0.2,
                            random_state=None):
    """
    Randomly split interactions between training and testing.
    Parameters
    ----------
    interactions: :class:`spotlight.interactions.Interactions`
        The interactions to shuffle.
    test_percentage: float, optional
        The fraction of interactions to place in the test set.
    random_state: np.random.RandomState, optional
        The random state used for the shuffle.
    Returns
    -------
    (train, test): (:class:`spotlight.interactions.Interactions`,
                    :class:`spotlight.interactions.Interactions`)
         A tuple of (train data, test data)
    """

    interactions.shuffle(random_state=random_state)

    cutoff = int((1.0 - test_percentage) * len(interactions))

    train_idx = slice(None, cutoff)
    test_idx = slice(cutoff, None)

    train = Interactions(
        interactions.user_ids[train_idx],
        interactions.item_ids[train_idx],
        ratings=_slice_or_none(interactions.ratings, train_idx),
        timestamps=_slice_or_none(interactions.timestamps, train_idx),
        weights=_slice_or_none(interactions.weights, train_idx),
        user_features=interactions.user_features,
        context_features=_slice_or_none(interactions.context_features,
                                        train_idx),
        item_features=interactions.item_features,
        num_users=interactions.num_users,
        num_items=interactions.num_items)
    test = Interactions(interactions.user_ids[test_idx],
                        interactions.item_ids[test_idx],
                        ratings=_slice_or_none(interactions.ratings, test_idx),
                        timestamps=_slice_or_none(interactions.timestamps,
                                                  test_idx),
                        weights=_slice_or_none(interactions.weights, test_idx),
                        user_features=interactions.user_features,
                        context_features=_slice_or_none(
                            interactions.context_features, test_idx),
                        item_features=interactions.item_features,
                        num_users=interactions.num_users,
                        num_items=interactions.num_items)

    return train, test
Exemplo n.º 4
0
def test(args):
    data = Interactions(args.test_root)
    data.to_sequence(args.L, args.T)
    sequences_np = data.sequences.sequences
    targets_np = data.sequences.targets
    users_np = data.sequences.user_ids.reshape(-1, 1)
    n_test = sequences_np.shape[0]
    print('total test instances: %d' % n_test)
    num_users = data.num_users
    num_items = data.num_items
    NDCG, HR, MRR = 0.0, 0.0, 0.0
    item_ids = np.zeros((args.batch_size,num_items))
    for i in range(args.batch_size):
        item_ids[i] = np.arange(num_items)
    test_batches = n_test // args.batch_size

    model=Caser(num_users,num_items,args)
    gpu_config = tf.ConfigProto()
    gpu_config.gpu_options.allow_growth = True
    with tf.Session(config=gpu_config) as sess:
        saver = tf.train.Saver(tf.global_variables())
        ckpt = tf.train.get_checkpoint_state(args.check_dir)
        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)
            print('Restore model from {} successfully!'.format(args.check_dir))
        else:
            print('Restore model from {} failed!'.format(args.check_dir))
            return
        for i in range(test_batches):
            sequences = sequences_np[i * args.batch_size: (i + 1) * args.batch_size]
            targets = targets_np[i * args.batch_size: (i + 1) * args.batch_size]
            users = users_np[i * args.batch_size: (i + 1) * args.batch_size]
            _, top_k_index = model.predict(sess, sequences, users, item_ids)
            hr, ndcg, mrr = 0.0, 0.0, 0.0
            for i in range(args.batch_size):
                cur_user = top_k_index[i]
                for j in range(args.top_k):
                    if targets[i][0] == cur_user[j]:
                        hr += 1
                        mrr += 1 / (1 + j)
                        dcg = 1 / np.log2(1 + 1 + j)
                        idcg = 1 / np.log2(1 + 1)
                        ndcg += dcg / idcg
                        break
            HR += hr / args.batch_size
            NDCG += ndcg / args.batch_size
            MRR += mrr / args.batch_size
    return HR / test_batches, NDCG / test_batches, MRR / test_batches
Exemplo n.º 5
0
 def _initialize(self, atoms):
     """ Initialization of hotbit. """
     if not self.init:
         self.set_text(self.txt)
         self.timer = Timer('Hotbit', txt=self.get_output())
         self.start_timing('initialization')
         self.el = Elements(self, atoms)
         self.ia = Interactions(self)
         self.st = States(self)
         self.rep = Repulsion(self)
         self.pp = PairPotential(self)
         if self.get('vdw'):
             if self.get('vdw_parameters') is not None:
                 self.el.update_vdw(self.get('vdw_parameters'))
             setup_vdw(self)
         self.env = Environment(self)
         pbc = atoms.get_pbc()
         # FIXME: gamma_cut -stuff
         #if self.get('SCC') and np.any(pbc) and self.get('gamma_cut')==None:
         #    raise NotImplementedError('SCC not implemented for periodic systems yet (see parameter gamma_cut).')
         if np.any(pbc) and abs(
                 self.get('charge')) > 0.0 and self.get('SCC'):
             raise AssertionError('Charged system cannot be periodic.')
         self.flush()
         self.flags = {}
         self.flags['Mulliken'] = False
         self.flags['DOS'] = False
         self.flags['bonds'] = False
         self.flags['grid'] = False
         self.stop_timing('initialization')
     self.el.set_atoms(atoms)
     if not self.init:
         self.init = True
         self.greetings()
def main():
    parser = argparse.ArgumentParser()
    # data arguments
    parser.add_argument('--train_root',
                        type=str,
                        default='datasets/ml1m/train.csv')
    parser.add_argument('--val_root',
                        type=str,
                        default='datasets/ml1m/val.csv')
    parser.add_argument('--test_root',
                        type=str,
                        default='datasets/ml1m/test.csv')
    parser.add_argument('--L', type=int, default=20)
    parser.add_argument('--T', type=int, default=1)  #next_T
    parser.add_argument('--top_k', type=int, default=20)
    # train arguments
    parser.add_argument('--n_iter', type=int, default=64)
    parser.add_argument('--seed', type=int, default=1234)
    parser.add_argument('--batch_size', type=int, default=16)
    parser.add_argument('--learning_rate', type=float, default=0.001)
    parser.add_argument('--l2', type=float, default=1e-6)
    parser.add_argument('--neg_samples', type=int, default=1)  #same as T
    parser.add_argument('--decay_rate', default=0.99, type=float)
    # model arguments
    parser.add_argument('--d', type=int, default=50)  #embedding dims
    parser.add_argument(
        '--nv', type=int,
        default=4)  #the number of filters for vertical convolution
    parser.add_argument(
        '--nh', type=int,
        default=16)  #the number of filters for horizontal convolution
    parser.add_argument('--drop', type=float, default=0.5)
    parser.add_argument('--check_dir', type=str, default='save/')
    config = parser.parse_args()

    # set seed
    set_seed(config.seed)

    # load dataset
    train = Interactions(config.train_root)
    # transform triplets to sequence representation
    train.to_sequence(config.L, config.T)

    val = Interactions(config.val_root)
    val.to_sequence(config.L, config.T)

    print(config)
    # fit model
    model = Recommender(args=config)
    model.fit(train, val, verbose=True)
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser()
    # data arguments
    parser.add_argument('--train_root', type=str, default='datasets/ml1m/test/train.txt')
    parser.add_argument('--test_root', type=str, default='datasets/ml1m/test/test.txt')
    parser.add_argument('--L', type=int, default=5)
    parser.add_argument('--T', type=int, default=3)
    # train arguments
    parser.add_argument('--n_iter', type=int, default=50)
    parser.add_argument('--seed', type=int, default=1234)
    parser.add_argument('--batch_size', type=int, default=512)
    parser.add_argument('--learning_rate', type=float, default=1e-3)
    parser.add_argument('--l2', type=float, default=1e-6)
    parser.add_argument('--neg_samples', type=int, default=3)
    # model arguments
    parser.add_argument('--d', type=int, default=50)
    parser.add_argument('--nv', type=int, default=4)
    parser.add_argument('--nh', type=int, default=16)
    parser.add_argument('--drop', type=float, default=0.5)
    
    config = parser.parse_args()

    # set seed
    set_seed(config.seed)

    # load dataset
    train = Interactions(config.train_root)
    # transform triplets to sequence representation
    train.to_sequence(config.L, config.T)

    test = Interactions(config.test_root,
                        user_map=train.user_map,
                        item_map=train.item_map)

    print(config)
    # fit model
    model = Recommender(args=config)
    model.fit(train, test, verbose=True)
Exemplo n.º 8
0
def main():
    WRITE_COLLECTION = args['write_collection']
    USER_FILE = args['user_file']
    READ_COLLECTION = args['read_collections']

    # get the list of users
    users = get_unique_users_fromfile(USER_FILE)
    logging.basicConfig(filename=LOG_FILE,
                        level=logging.DEBUG,
                        format='%(asctime)s %(message)s')

    # get mongodb collections
    db = get_mongo_connection()
    read_collection = db[READ_COLLECTION]
    write_collection = db[WRITE_COLLECTION]

    # create instance if Interactions (which contains actual processing utility)
    inter = Interactions(read_collection, QUERY)

    # process the users and write interactions to db
    process(inter, users, write_collection)
Exemplo n.º 9
0
    itemdict = {itemset[i]: i + 1 for i in range(len(itemset))}
    df['user_id'] = df.apply(lambda row: userdict[row.userid], axis=1)
    df['item_id'] = df.apply(lambda row: itemdict[row.itemid], axis=1)
    df = df.drop(columns=['userid', 'itemid'])

    randidx = np.random.rand(len(df)) < 0.8
    train_df = df[randidx]
    test_df = df[~randidx]

    train_df = train_df.sort_values(['user_id', 'timestamp'],
                                    ascending=[True, True])
    test_df = test_df.sort_values(['user_id', 'timestamp'],
                                  ascending=[True, True])

    # load dataset
    train = Interactions(train_df, userdict, itemdict)
    train.num_items += 1
    # transform triplets to sequence representation
    train.to_sequence(config.L, 1)

    test = Interactions(test_df,
                        user_map=train.user_map,
                        item_map=train.item_map)
    test.num_items += 1

    print(config)
    print(model_config)
    # fit model
    model = Recommender(n_iter=config.n_iter,
                        batch_size=config.batch_size,
                        learning_rate=config.learning_rate,
Exemplo n.º 10
0
def createAnalysis(param):

    from abaqus import *
    backwardCompatibility.setValues(reportDeprecated=False)
    from abaqusConstants import *
    from caeModules import *
    import mesh
    Im2MeshToolbox = r"D:\myWork\procedures\2DImage2Mesh_VC"
    import sys
    sys.path.append(Im2MeshToolbox)
    from sipShell2Abq import shellTo2DGeo

    ## IMPORT FILE FROM SCANIP MODEL
    myModel = mdb.ModelFromInputFile(inputFileName=param['sipInpFile'],
                                     name=param['modelName'])
    shellTo2DGeo(myModel)
    deleteDefaultModel()

    ## SHORTCUTS
    myAssembly = myModel.rootAssembly
    allMats = myModel.materials

    ## STEP CREATION
    myModel.StaticStep(initialInc=0.01,
                       timePeriod=param['timePeriod'],
                       maxInc=.1,
                       minInc=1e-9,
                       name='displ',
                       nlgeom=ON,
                       previous='Initial')

    directions = list()
    matNames = list()
    slMaCouples = list()
    for i, myPart in enumerate(myModel.parts.values()):
        myPSet = myPart.sets
        part = myPart.name.split('_')[0]
        if part == 'INPLANE6':
            del myModel.parts['INPLANE6_Geo']
            del myAssembly.features['INPLANE6_instance']
            continue

        ## MATERIALS
        cSys = myPart.DatumCsysByThreePoints(coordSysType=CARTESIAN,
                                             origin=(0., 0., 0.),
                                             point1=(1., 0., 0.),
                                             point2=(0., 1., 0.))
        mat = 'SM_' + part
        if allMats.has_key(mat):
            myMat = myModel.materials[mat]
        else:
            print 'material %s unknown!!!' % mat
            continue

        E = 0.06  #equivalent GM only as we are perp to fibers!!
        nu = 0.499
        matParam = (E / (4 * (1. + nu)), 6 * (1 - 2. * nu) / E)
        if matParam[1] == 0.:  # incompressible
            myMat.Hyperelastic(testData=OFF,
                               table=(matParam, ),
                               materialType=ISOTROPIC,
                               type=NEO_HOOKE,
                               behaviorType=INCOMPRESSIBLE)
        else:
            myMat.Hyperelastic(testData=OFF,
                               table=(matParam, ),
                               materialType=ISOTROPIC,
                               type=NEO_HOOKE,
                               behaviorType=COMPRESSIBLE)

        ## SECTION
        myModel.HomogeneousSolidSection(name='Section_%s' % part,
                                        material=mat,
                                        thickness=None)
        myPart.SectionAssignment(region=(myPart.faces[0], ),
                                 sectionName='Section_%s' % part)
        ## BC'S

        myISet = myAssembly.instances['%s_instance' % part].sets
        dMin = 'NS_%s_WITH_XMIN' % part
        dMax = 'NS_%s_WITH_XMAX' % part

        if myISet.has_key(dMin):
            myModel.PinnedBC(createStepName='displ',
                             localCsys=None,
                             name='fix_%d' % (i),
                             region=myISet[dMin])
        if part == 'INPLANE5':
            x1 = (1.10898, 1.29364)
            x2 = (1.29274, 0.74189)
            d1 = (0.134, -0.12)
            d2 = (0.18, -0.10)
            for node in myISet['SF_INPLANE5_WITH_SECTIONNED6'].nodes:
                i += 1
                applyInterpolatedDisplacement(myModel, node, x1, x2, d1, d2,
                                              'mov_%d' % (i))

        ## Get Master/Slave couples
        oParts = list(myModel.parts.keys())
        oParts.remove(myPart.name)
        for setName in myPSet.keys():
            if setName.startswith('SF_%s_WITH_' % part):
                for oPart in oParts:
                    oPartName = oPart.split('_')[0]
                    if oPartName == 'INPLANE6':
                        continue
                    elif oPartName in setName:
                        if [oPartName, part] not in slMaCouples:
                            slMaCouples.append([part, oPartName])

    ## CONSTRAINTS - same for all interfaces!!
    for nbInteraction, slMaCouple in enumerate(slMaCouples):
        masterName = 'SF_%s_WITH_%s' % (slMaCouple[1], slMaCouple[0])
        slaveName = 'SF_%s_WITH_%s' % (slMaCouple[0], slMaCouple[1])
        myMaInstance = myAssembly.instances['%s_instance' % slMaCouple[1]]
        mySlInstance = myAssembly.instances['%s_instance' % slMaCouple[0]]
        from interactions import Interactions
        inter = Interactions(myModel)
        inter.setName('interaction_%i' % nbInteraction)
        if param['interfaceType'] == 'Tie':
            inter.setMasterSlave(myMaInstance.sets[masterName],
                                 mySlInstance.sets[slaveName])
            inter.setInteractionToTie()
        else:
            inter.setMasterSlave(myMaInstance.surfaces[masterName],
                                 mySlInstance.surfaces[slaveName])
            inter.setNormalStiffness(param['normalStiffness'])
            if param['interfaceType'] == 'Friction':
                inter.setFrictionBehaviour('Friction')
            elif param['interfaceType'] == 'Rough':
                inter.setFrictionBehaviour('Rough')
            elif param['interfaceType'] == 'Cohesive':
                inter.setCohesiveBehaviour(
                    useDefaultBehaviour=False,
                    penalties=param['cohesivePenalties'])
            elif param['interfaceType'] == 'CohesiveRough':
                inter.setCohesiveBehaviour(
                    useDefaultBehaviour=False,
                    penalties=param['cohesivePenalties'])
                inter.setFrictionBehaviour('Rough')
            elif param['interfaceType'] == 'CohesiveFriction':
                inter.setCohesiveBehaviour()
                inter.setFrictionBehaviour('Friction')
        inter.createInteraction()

    ## JOB
    myJob = mdb.Job(model=param['modelName'], name=param['modelName'])
    myJob.writeInput(consistencyChecking=OFF)
    if param['numCpus'] > 1:
        myJob.setValues(numCpus=param['numCpus'],
                        numDomains=param['numCpus'],
                        multiprocessingMode=DEFAULT)
    return myJob, mdb
Exemplo n.º 11
0
    parser.add_argument('--block_dim', type=list, default=[128, 256])
    parser.add_argument('--drop',
                        type=float,
                        default=0.5,
                        help='drop out ratio.')
    parser.add_argument('--fc_dim', type=int, default=150)
    parser.add_argument('--ac_fc',
                        type=str,
                        default='tanh',
                        choices=['relu', 'tanh', 'sigm'])

    args = parser.parse_args()

    # set seed
    set_seed(args.seed, cuda=args.use_cuda)
    # load dataset
    train = Interactions(args.data_root + args.dataset + args.train_dir)
    # transform triplets to sequence representation
    train.to_sequence(args.L, args.T)

    test = Interactions(args.data_root + args.dataset + args.test_dir,
                        user_map=train.user_map,
                        item_map=train.item_map)

    print(args)
    print('Using dataset: {}'.format(args.dataset))
    # fit model
    model = Recommender(args)

    model.fit(train, test, verbose=True)
Exemplo n.º 12
0
from students import Work
from interactions import Interactions

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

intents = discord.Intents.default()
intents.members = True
prefix = '!'

bot = commands.Bot(command_prefix='!', intents=intents)

bot.add_cog(Breakout(bot))
bot.add_cog(Work(bot))
bot.add_cog(Schedule(bot))
bot.add_cog(Interactions(bot))


@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')


@bot.command()
async def ping(ctx):
    await ctx.send('pong!')


@bot.command()
async def setuproles(ctx):
    await ctx.send('Setting up roles...')
Exemplo n.º 13
0
    # train arguments
    parser.add_argument('--n_iter', type=int, default=100)
    parser.add_argument('--seed', type=int, default=1234)
    parser.add_argument('--batch_size', type=int, default=2048)
    parser.add_argument('--learning_rate', type=float, default=1e-3)
    parser.add_argument('--l2', type=float, default=0)

    # model dependent arguments
    parser.add_argument('--d', type=int, default=50)

    config = parser.parse_args()

    from data import Amazon
    data_set = Amazon.Beauty()  # Books, CDs, LastFM
    train_set, test_set, num_users, num_items, kg_map = data_set.generate_dataset(
        index_shift=1)

    maxlen = 0
    for inter in train_set:
        if len(inter) > maxlen:
            maxlen = len(inter)

    train = Interactions(train_set, num_users, num_items)
    train.to_newsequence(config.L, config.T)

    logger.info(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    logger.info(config)

    train_kerl(train, test_set, config, kg_map)
Exemplo n.º 14
0
from overview import Overview
from interactions import Interactions
from reviews import Reviews
import shutil

name = "ortho micronor"
drugOverview = Overview("https://www.drugs.com/mtm/ortho-micronor.html", name)
drugOverview.buildOverview()

drugInteraction = Interactions(
    "https://www.drugs.com/drug-interactions/norethindrone,ortho-micronor.html",
    name)
drugInteraction.buildInteractions()

drugReviews = Reviews(
    "https://www.drugs.com/comments/norethindrone/ortho-micronor.html", name)
drugReviews.buildReviews()

shutil.move(name + '.txt', 'minipills')
Exemplo n.º 15
0
def build_data(args):
    # load dataset
    train = Interactions(args.train_root)
    # transform triplets to sequence representation
    train.to_sequence(args.L, args.T)

    test = Interactions(args.test_root,
                        user_map=train.user_map,
                        item_map=train.item_map)

    if not os.path.exists('./topic_data'):
        os.mkdir('./topic_data')
    topic_path = f'./topic_data/data_{args.train_data}_{args.L}_{args.topic_num}.pkl'
    if args.train_lda or not os.path.exists(topic_path):
        matrix = dok_matrix((train.num_users, train.num_items - 1))
        user_items = defaultdict(list)
        with open(args.train_root) as f:
            for line in f:
                line = line[:-1]
                if line == '':
                    break
                user, item, _ = line.split()
                matrix[train.user_map[user], train.item_map[item] - 1] += 1
                user_items[train.user_map[user]].append(
                    f'i_{train.item_map[item]}')

        lda = LatentDirichletAllocation(n_components=args.topic_num,
                                        perp_tol=0.01,
                                        max_iter=200,
                                        max_doc_update_iter=500,
                                        n_jobs=args.n_jobs,
                                        random_state=args.seed,
                                        verbose=1,
                                        evaluate_every=10)
        lda.fit(matrix)
        item_probs = lda.components_ / lda.components_.sum(axis=1)[:,
                                                                   np.newaxis]
        print("LDA training process finished!")

        ### calculate train_probs
        ### This process may takes a lot of memories. Therefore, we apply batch processing
        n_sequences = train.sequences.sequences.shape[0]
        train_probs = np.zeros((n_sequences, args.topic_num))

        n_batches = ceil(n_sequences / args.lda_batch_size)
        for n in range(n_batches):
            sub_sequences = train.sequences.sequences[n *
                                                      args.lda_batch_size:(n +
                                                                           1) *
                                                      args.lda_batch_size]
            matrix = np.zeros((*sub_sequences.shape, train.num_items - 1),
                              dtype=np.float32)

            i = np.arange(sub_sequences.shape[0]).reshape(-1, 1)
            i = i.repeat(sub_sequences.shape[1], axis=1).reshape(-1)
            j = np.arange(sub_sequences.shape[1]).repeat(
                sub_sequences.shape[0])
            k = sub_sequences.reshape(-1) - 1

            matrix[i, j, k] += 1
            matrix = matrix.sum(axis=1)

            probs = lda.transform(matrix)
            train_probs[n * args.lda_batch_size:(n + 1) *
                        args.lda_batch_size, :] = probs

        # calculate test_probs
        n_sequences = train.test_sequences.sequences.shape[0]
        test_probs = np.zeros((n_sequences, args.topic_num))
        n_batches = ceil(n_sequences / args.lda_batch_size)
        for n in range(n_batches):
            sub_sequences = train.test_sequences.sequences[n *
                                                           args.lda_batch_size:
                                                           (n + 1) *
                                                           args.lda_batch_size]
            matrix = np.zeros((*sub_sequences.shape, train.num_items - 1),
                              dtype=np.float32)

            i = np.arange(sub_sequences.shape[0]).reshape(-1, 1)
            i = i.repeat(sub_sequences.shape[1], axis=1).reshape(-1)
            j = np.arange(sub_sequences.shape[1]).repeat(
                sub_sequences.shape[0])
            k = sub_sequences.reshape(-1) - 1

            matrix[i, j, k] += 1
            matrix = matrix.sum(axis=1)

            probs = lda.transform(matrix)
            test_probs[n * args.lda_batch_size:(n + 1) *
                       args.lda_batch_size, :] = probs

        train.sequences.probs = train_probs.astype(np.float32)
        train.test_sequences.probs = test_probs.astype(np.float32)

        with open(topic_path, 'wb') as f:
            pickle.dump((train, test, user_items, item_probs),
                        f,
                        protocol=pickle.HIGHEST_PROTOCOL)
    else:
        with open(topic_path, 'rb') as f:
            train, test, user_items, item_probs = pickle.load(f)

    return train, test, user_items, item_probs
Exemplo n.º 16
0
    model_parser = argparse.ArgumentParser()
    model_parser.add_argument('--d', type=int, default=100)
    model_parser.add_argument('--nv', type=int, default=2)
    model_parser.add_argument('--nh', type=int, default=16)
    model_parser.add_argument('--drop', type=float, default=0.5)
    model_parser.add_argument('--ac_conv', type=str, default='iden')
    model_parser.add_argument('--ac_fc', type=str, default='sigm')

    model_config = model_parser.parse_args()
    model_config.L = config.L

    # set seed
    set_seed(config.seed, cuda=config.use_cuda)

    # load dataset
    train = Interactions(config.train_root)
    # transform triplets to sequence representation
    train.to_sequence(config.L)

    test = Interactions(config.test_root,
                        user_map=train.user_map,
                        item_map=train.item_map)

    print(config)
    print(model_config)
    # fit model
    model = Recommender(n_iter=config.n_iter,
                        batch_size=config.batch_size,
                        learning_rate=config.learning_rate,
                        l2=config.l2,
                        neg_samples=config.neg_samples,
Exemplo n.º 17
0

# Command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-x",
                    "--xcolors",
                    action="store_true",
                    default=False,
                    help="use X colors (Linux only)")
args = parser.parse_args()

queue = Queue()
image_queue = ImageQueue()

# creates the interactions object
interactions = Interactions(sp, token_info, sp_oauth, exit_app, queue)

# UI
ui = Ui(interactions, use_x_colors=args.xcolors)

# Create icon
icon = QIcon(f"{ASSETS_DIR}img{sep}logo_small.png")

# Create tray
tray = QSystemTrayIcon()
tray.setIcon(icon)
tray.setVisible(True)
tray.setToolTip("Spotlightify")
tray.activated.connect(show_ui)

# Create menu
Exemplo n.º 18
0
def caeAnalysis(p):
    # check parameter consistency
    assert not (p['withNucleus'] and p['internalPressure']), "do not set internal pressure if nucleus is modelled"
    
    # MODEL
    myModel = mdb.Model(p['modelName'])
    abaqusTools.deleteDefaultModel()
    myAssembly = myModel.rootAssembly
    
    #check if creating annulusCut and NucleusCut makes sense (ie check if there is indeed a cut to make with the dimensions given)
    cutAnnulus = False
    cutNucleus = False
    if p['punchCut']:
        D = getDistanceBetweenCentres((0,0,0),p['punchCentre']) 
        if D<p['punchRadius']+p['innerRadius']:
            cutNucleus = True
            if D>p['punchRadius']-p['innerRadius']:
                cutAnnulus = True      
        
        if (cutAnnulus or cutNucleus):
            punch =  genericIVDCreation.CylNucleus(myModel)
            punch.setCentre(p['punchCentre'])
            punch.setRadius(p['punchRadius'])
            punch.setHeight(p['height'])
            punch.setName('punch')
            #punchInstance,punchPart = punch.create()
    
    annulus =  genericIVDCreation.CylAnnulus(myModel)
    annulus.setInnerRadius(p['innerRadius'])
    annulus.setOuterRadius(p['outerRadius'])
    annulus.setHeight(p['height'])
    annulus.setNbLamellae(p['nbLamellae'])
    annulus.setNbCuts(p['nbCut'])
    if p['matType'] != 'Holzapfel':
        annulus.setToIncompressible()#will use hybrid elements for almost incompressible material - check if can be used for Holzapfel
    if cutAnnulus:
        annulus.cutWithPunch(punch)
    annulusParts,cutAnnulusPart = annulus.create()

    matNames = list()
    directions = list()
    for cyl in range(p['nbLamellae']):
        thisPart = annulusParts[cyl]
        for arc in range(p['nbCut'][cyl]):
            sectionName = 'annulus%i_section%i'%(cyl,arc)
            domainNb = int(sum(p['nbCut'][0:cyl]))+arc
            if cutAnnulusPart[cyl][arc]:# if the lamella is completely severed then two points are needed to define the cells
                lamellarThickness = (p['outerRadius']-p['innerRadius'])/p['nbLamellae']
                r0 = p['innerRadius']+(p['outerRadius']-p['innerRadius'])/p['nbLamellae']*cyl
                alpha = 360./p['nbCut'][cyl]*(arc+1-0.01)*math.pi/180.
                middlePt = ((r0*math.cos(alpha),p['height']/2.,r0*math.sin(alpha)),)
                pickedCells2 = (thisPart.cells.findAt(middlePt),thisPart.cells.findAt((annulus.midPoint[domainNb],)))
            else: pickedCells2 = thisPart.cells.findAt((annulus.midPoint[domainNb],))
            # create material
            matName = 'annulus%i'%domainNb
            if isinstance(p['holzapfelParameters'],list) and len(p['holzapfelParameters']) == int(sum(p['nbCut'])):#there is one set of parameters per cut
                matParam = p['holzapfelParameters'][domainNb]
            elif len(p['holzapfelParameters']) == 5:
                matParam = p['holzapfelParameters']
            else: raise("parameter 'holzapfelParameters' of unknown type or wrong length")
            if p['matType'] == 'Holzapfel':
                if isinstance(p['fiberDirection'],list) and len(p['fiberDirection']) == int(sum(p['nbCut'])):
                    fibreAngle = p['fiberDirection'][domainNb]
                elif isinstance(p['fiberDirection'],float) and p['nbLamellae']<2:
                    fibreAngle = p['fiberDirection']
                else: raise Exception("parameter 'fiberDirection' of unknown type or wrong length")
                matNames.append(matName)
                directions.append((0.,math.cos(fibreAngle),math.sin(fibreAngle)))
            
            material = genericIVDCreation.annulusMaterial(matName,p['matType'],myModel)
            material.setMatParam(matParam)
            if p['nbLamellae']<2:material.setToTwoDirections()
            material.define()
            
            # coordinate system
            csysCyl = thisPart.DatumCsysByThreePoints(coordSysType=CYLINDRICAL,origin=(0.,0.,0.),point1=(1.,0.,0.),point2=(0.,0.,-1.))
            # assign material
            #abaqusTools.assignMaterialToPart(matName,thisPart,myModel,orientation=csysCyl)
            abaqusTools.assignMaterialToPartition(matName,thisPart,sectionName,pickedCells2,myModel,orientation=csysCyl)
            
    if p['withNucleus']:
        #geometry and mesh
        nucleus =  genericIVDCreation.CylNucleus(myModel,annulus.bottomFaces,annulus.topFaces)
        nucleus.setRadius(p['innerRadius'])
        nucleus.setHeight(p['height'])
        if cutNucleus:
            nucleus.cutWithPunch(punch)
        nucleusInstance,nucleusPart = nucleus.create()
        # material - compressive modulus H~2G (Neo-Hookean model of sig = H/2(lambda-1/lambda)) and C10=G/2  ==> C10 = H/4
        myMat = myModel.Material(name='nucleus')
        myMat.Hyperelastic(testData=OFF,table=((p['compressiveModulus']/4.,0.0),),materialType=ISOTROPIC,type=NEO_HOOKE,behaviorType=INCOMPRESSIBLE)
        abaqusTools.assignMaterialToPart('nucleus',nucleusPart,myModel)
        #nucleus/annulus connection (rough contact)
        innerAnnulus = tuple(annulus.innerFaces[i] for i in range(p['nbCut'][0]))
        outerNucleus = nucleusInstance.faces.findAt((nucleus.outerPoint,))
        masterSurface = myAssembly.Surface(name='innerAnnulus',side1Faces=innerAnnulus)
        slaveSurface = myAssembly.Surface(name='outerNucleus',side1Faces=outerNucleus)
        from interactions import Interactions
        inter = Interactions(myModel)
        inter.setMasterSlave(masterSurface,slaveSurface)
        inter.setName('annulusNucleusInterface')
        inter.setFrictionBehaviour('Rough')
        inter.setNormalStiffness(1e8)
        inter.createInteraction()

        topFace = nucleus.topFaces
        bottomFace = nucleus.bottomFaces
    else:    
        topFace = annulus.topFaces
        bottomFace = annulus.bottomFaces
        
    ## SETS FOR OUTPUT ANALYSIS
    myAssembly.Set(faces=tuple(topFace), name='topFaces')
    myAssembly.Set(faces=tuple(bottomFace), name='bottomFaces')
        
    if p['nbLamellae']>1:
        ##CONSTRAINTS - same for all interfaces!!
        for nb in range(1,p['nbLamellae']):
            domainNb = int(sum(p['nbCut'][0:nb]))
            outerFaces = tuple(annulus.outerFaces[domainNb-i-1] for i in range(p['nbCut'][nb-1]))
            if any(cutAnnulusPart[cyl]):outerFaces2 = tuple(annulus.outerFaces2[domainNb-i-1] for i in range(p['nbCut'][nb-1]))
            innerFaces = tuple(annulus.innerFaces[domainNb+i] for i in range(p['nbCut'][nb]))
            if any(cutAnnulusPart[cyl]):masterSurface = myAssembly.Surface(name='master%d'%(nb),side1Faces=(outerFaces,outerFaces2))
            else:masterSurface = myAssembly.Surface(name='master%d'%(nb),side1Faces=outerFaces)
            slaveSurface = myAssembly.Surface(name='slave%d'%(nb),side1Faces=innerFaces)
            from interactions import Interactions
            inter = Interactions(myModel)
            inter.setMasterSlave(masterSurface,slaveSurface)
            inter.setName('interface%d'%(nb))
            if p['interfaceType'] == 'Tie':
                inter.setInteractionToTie()
            elif p['interfaceType'] == 'Friction':
                inter.setFrictionBehaviour('Friction')
            elif p['interfaceType'] == 'Cohesive':
                inter.setCohesiveBehaviour()
            elif p['interfaceType'] == 'CohesiveFriction':
                inter.setCohesiveBehaviour()
                inter.setFrictionBehaviour('Friction')
            inter.createInteraction()

	##STEP
    myModel.StaticStep(name='Load',previous='Initial',timePeriod=p['timePeriod'],initialInc=p['initialInc'],nlgeom=ON,
    maxInc=p['maxInc'],minInc=p['minInc'],maxNumInc=10000)
    myModel.steps['Load'].control.setValues(allowPropagation=OFF, resetDefaultValues=OFF, discontinuous=ON, timeIncrementation=(8, 10, 9., 16., 10.0, 4., 12., 10.0, 5., 3., 50.0))
    #I0=8(nb equ ite - cannot change if discontinuous ON),Ir=10 (nb conseq equ ite - cannot change if discontinuous ON),Ip=9,Ic=16,Il=10,Ig=4,Is=12,Ia=5,Ij=5,It=3,Isc=50
    myModel.steps['Load'].solverControl.setValues(allowPropagation=OFF, resetDefaultValues=OFF, maxIterations=10)
	
    ##LOAD/BC - after step as the step names are used!!!
    myTopSurface = myAssembly.Surface(name='topSurface',side1Faces=topFace)
    cylSys = myAssembly.DatumCsysByThreePoints(name='cylC',coordSysType=CYLINDRICAL, origin=(0,0,0),\
    point1=(1.0, 0.0, 0), point2=(0.0, 0.0, -1.0) )
    datumCyl = myAssembly.datums[cylSys.id]
    if p['load'] =='Pressure':#default !! magnitude provided = PRESSURE
        myModel.Pressure(name='Pressure',createStepName='Load',region=myTopSurface,magnitude=p['loadMagnitude'],
        distributionType=UNIFORM)
        myModel.PinnedBC(name='Fixed',createStepName='Load',region=tuple(bottomFace))
    elif p['load'] == 'Pressure_total': #!!magnitude provided = total INITIAL FORCE, when the area varies -> force = magnitude*area1/area0!!
        myModel.Pressure(name='Pressure',createStepName='Load',region=myTopSurface,magnitude=p['loadMagnitude'],
        distributionType=TOTAL_FORCE)
        myModel.PinnedBC(name='Fixed',createStepName='Load',region=tuple(bottomFace))
    elif p['load'] == 'vertDispl':
        myModel.DisplacementBC(name='Displ',createStepName='Load',region=tuple(topFace),u1=0.,u2=0.,u3=-p['displ'], localCsys=datumCyl)
        myModel.PinnedBC(name='Fixed',createStepName='Load',region=tuple(bottomFace))
    elif p['load'] == 'PressurePlane':# magnitude provided = concentrated FORCE on the rigid plane
        import regionToolset
        p['outerRadius'] = p['outerRadius']
        surf = myModel.ConstrainedSketch(name='surf', sheetSize=200.0)
        surf.Line(point1=(0.0, p['outerRadius']), point2=(0.0, -p['outerRadius']))
        surfPart = myModel.Part(name='crushingPart', dimensionality=THREE_D, type=ANALYTIC_RIGID_SURFACE)
        surfPart.AnalyticRigidSurfExtrude(sketch=surf, depth=2.*p['outerRadius'])
        surfPart.ReferencePoint(point=(0.0, 0.0, 0.0))
        crushPlane = myAssembly.Instance(name='crushingPlane', part=surfPart, dependent=ON)
        f1 = myAssembly.instances['crushingPlane'].faces[0]
        f2 = myAssembly.instances['cylinder0_instance'].faces[1]
        myAssembly.ParallelFace(movablePlane=f1, fixedPlane=f2, flip=ON)
        myAssembly.translate(instanceList=('crushingPlane', ), vector=(0.0, p['height'], 0.0))
        side1Faces1 = crushPlane.faces.getSequenceFromMask(mask=('[#1 ]', ), )
        myCrushingSurface = myAssembly.Surface(side1Faces=side1Faces1, name='crushingSurface')
        myModel.Tie(name='tieTop', master=myCrushingSurface, slave=myTopSurface)
        region = regionToolset.Region(referencePoints=(crushPlane.referencePoints[2], ))
        myModel.ConcentratedForce(name='Load-1', createStepName='Load', region=region, cf2=-p['loadMagnitude'], distributionType=UNIFORM,
        follower=ON)
        myModel.DisplacementBC(name='BC-2', createStepName='Load', region=region, u1=0.0, u3=0.0, ur1=0.0, ur2=0.0, ur3=0.0,
        distributionType=UNIFORM)
        myModel.DisplacementBC(name='noRadialDispl',createStepName='Load',region=tuple(topFace),u1=0.,u2=0.,localCsys=datumCyl)
        myModel.PinnedBC(name='Fixed',createStepName='Load',region=tuple(bottomFace))
    else:#load only in internal pressure
        if p['internalPressure']:
            myModel.XsymmBC(name='FixedTop',createStepName='Load',region=tuple(topFace),localCsys=datumCyl)
            myModel.XsymmBC(name='Fixed',createStepName='Load',region=tuple(bottomFace),localCsys=datumCyl)
        else: raise Exception("no BC's have been defined!!")
    if p['internalPressure']:
        myInnerSurface = myAssembly.Surface(name='innerSurface',side1Faces=tuple(annulus.innerFaces[i] for i in range(p['nbCut'][0])))
        myModel.Pressure(name='intPressure',createStepName='Load',region=myInnerSurface,magnitude=p['internalPressure'],
        distributionType=UNIFORM)
    	
    ## OUTPUT REQUESTS
    #fieldVariable = ('S', 'LE', 'U', 'RT', 'P', 'CSTRESS', 'CDISP', 'CFORCE')
    #myModel.fieldOutputRequests['F-Output-1'].setValues(variables=fieldVariable)
    
    ## JOB
    from jobCreation import JobDefinition
    myJobDef = JobDefinition(p['modelName'])
    myJobDef.setScratchDir(p['scratchDir'])
    if p['matType'] == 'Holzapfel':
        myJobDef.setToFibrous()        
        myJobDef.fibreDirections(directions)
        myJobDef.setFibreInputType('partition')
        myJobDef.setMatNames(matNames)
        if p['nbLamellae']<2:myJobDef.setPartitionTwoDirections()
    myNewJob = myJobDef.create()
    if p['numCpus']>1:
        myNewJob.setValues(numCpus=p['numCpus'],numDomains=p['numCpus'],multiprocessingMode=THREADS)
    myNewJob.setValues(memory=3, memoryUnits=GIGA_BYTES)
    mdb.saveAs(myNewJob.name)
    #-------------------------------------------------------
    return myNewJob,mdb
Exemplo n.º 19
0
    def run(self):
        print('/1')
        index, r_index = 0, 0
        dict_locid_catid,dict_catid_to_index,dict_r_catid_to_index= {},{},{}
        for ny_vcat_i in self.ny_vcat[1:]:
            ny_vcat_i = ny_vcat_i.split(',')
            locid_ = ny_vcat_i[0]
            catid_ = ny_vcat_i[1]
            if locid_ not in dict_locid_catid:
                dict_locid_catid[locid_] = (catid_, int(catid_))
                if catid_ not in dict_catid_to_index:
                    dict_catid_to_index[catid_] = index
                    index += 1
                if int(catid_) not in dict_r_catid_to_index:
                    dict_r_catid_to_index[int(catid_)] = r_index
                    r_index += 1
        set_locid_name = set(dict_locid_catid.keys())
        locid_name = list(set(dict_locid_catid.keys()))
        catid_set = list(set(dict_catid_to_index.values()))
        relation_catid_set = list(set(dict_r_catid_to_index.values()))
        print('/2')
        for i in range(len(locid_name)):
            locid_catid_ = dict_locid_catid[locid_name[i]]
            index_ = dict_catid_to_index[locid_catid_[0]]
            r_index_ = dict_r_catid_to_index[locid_catid_[1]]
            dict_locid_catid[locid_name[i]] = (index_, r_index_)
        print('/3')
        dict_uid_category,uid_category_set,gender_set,race_set = {},[],[],[]
        for ny_demo_i in self.ny_demo[1:]:
            ny_demo_i = ny_demo_i.split(',')
            uid_ = ny_demo_i[0]
            gender_ = int(ny_demo_i[1]) - 1
            race_ = int(ny_demo_i[3]) - 1
            category_ = gender_ * race_ + race_
            dict_uid_category[uid_] = (category_, (gender_, race_))
            uid_category_set.append(category_)
            gender_set.append(gender_)
            race_set.append(race_)
        uid_category_set = list(set(uid_category_set))
        uid_name = set(dict_uid_category.keys())
        self.gender_set = list(set(gender_set))
        self.race_set = list(set(race_set))
        print('/4')
        dict_uid_time_locid = {}
        for i in range(len(self.ny_checkin) - 1):
            ny_checkin_i = self.ny_checkin[i + 1].split(',')
            uid_, time_, locid_ = ny_checkin_i[1], ny_checkin_i[
                2], ny_checkin_i[5].strip('\n')
            time_day, time_hms = time_.split()[0], time_.split()[1]
            if uid_ in uid_name and locid_ in set_locid_name:
                year_ = int(time_day.split('-')[0])
                if year_ in self.dict_restrict['year']:
                    if uid_ not in dict_uid_time_locid:
                        dict_uid_time_locid[uid_] = [(time_day, time_hms,
                                                      locid_)]
                    else:
                        dict_uid_time_locid[uid_].append(
                            (time_day, time_hms, locid_))
        print('/5')
        uid_name = list(set(dict_uid_time_locid.keys()))
        dict_uid_locid_relation, dict_locid_to_entity, index = {}, {}, 0
        for u_i in range(len(uid_name)):
            uid_time_locid_ = dict_uid_time_locid[uid_name[u_i]]
            if len(uid_time_locid_) >= self.dict_restrict['num_checkin']:
                sorted_uid_time_locid_ = sorted(uid_time_locid_)
                for i in range(len(sorted_uid_time_locid_)):
                    time_day_, time_hms_, locid_ = sorted_uid_time_locid_[i][
                        0], sorted_uid_time_locid_[i][
                            1], sorted_uid_time_locid_[i][2]
                    daynum_time_day_ = int(time_day_.split('-')[1]) - 1
                    time_h = int(time_hms_.split('-')[0].split(':')[0])
                    if time_h > 11:
                        time_h = 1
                    else:
                        time_h = 0
                    relation_time = time_h * daynum_time_day_ + daynum_time_day_
                    sorted_uid_time_locid_[i] = (locid_, relation_time)
                    if locid_ not in dict_locid_to_entity:
                        dict_locid_to_entity[locid_] = index
                        index += 1
                dict_uid_locid_relation[uid_name[u_i]] = sorted_uid_time_locid_
        uid_name = list(set(dict_uid_locid_relation.keys()))
        locid_name = list(set(dict_locid_to_entity.keys()))
        print('/6')
        test_X_num = self.dict_restrict['test_X_num']
        test_Y_num = self.dict_restrict['test_Y_num']
        train_set,test_set,test_X_set,test_Y_set,relation_time_set,data_set = [],[],[],[],[],[]
        for i in range(len(uid_name)):
            uid_locid_relation_ = dict_uid_locid_relation[uid_name[i]]
            data_set_, catid_set_ = [], []
            for j in range(len(uid_locid_relation_)):
                locid_ = uid_locid_relation_[j][0]
                catid_ = dict_locid_catid[locid_][0]
                entity_locid_ = dict_locid_to_entity[locid_]
                relation_time_ = uid_locid_relation_[j][1]
                data_set_.append(entity_locid_)
                relation_time_set.append(relation_time_)
                catid_set_.append(catid_)
            data_set.append(data_set_)
            train_set_ = data_set_[:len(data_set_) - test_X_num - test_Y_num]
            test_X_set_ = data_set_[len(data_set_) - test_X_num -
                                    test_Y_num:len(data_set_) - test_Y_num]
            test_Y_set_ = data_set_[len(data_set_) - test_Y_num:]
            test_set_ = data_set_[len(data_set_) - test_X_num - test_Y_num:]
            train_set.append(train_set_)
            test_set.append(test_set_)
            test_X_set.append(test_X_set_)
            test_Y_set.append(test_Y_set_)
        self.test_X_set = np.array(test_X_set)
        relation_time_set = list(set(relation_time_set))
        entity_set = list(set(dict_locid_to_entity.values()))
        print('/7')
        dict_uid_category_to_entity = {}
        for i in range(len(uid_category_set)):
            dict_uid_category_to_entity[
                uid_category_set[i]] = uid_category_set[i] + len(entity_set)
        dict_catid_to_entity = {}
        for i in range(len(catid_set)):
            dict_catid_to_entity[catid_set[i]] = len(entity_set) + len(
                uid_category_set) + catid_set[i]
        dict_relation_catid_to_r = {}
        for i in range(len(relation_catid_set)):
            dict_relation_catid_to_r[relation_catid_set[i]] = len(
                relation_time_set) + relation_catid_set[i]
        print('/8')
        self.dict_KG, self.uid_attribute_category = {}, []
        for i in range(len(uid_name)):
            uid_category_, uid_category_detail_ = dict_uid_category[
                uid_name[i]]
            entity_uid_category_ = dict_uid_category_to_entity[uid_category_]
            uid_locid_relation_ = dict_uid_locid_relation[uid_name[i]]
            gender_, race_ = uid_category_detail_[0], uid_category_detail_[1]
            self.uid_attribute_category.append((gender_, race_))
            for j in range(len(uid_locid_relation_)):
                locid_ = uid_locid_relation_[j][0]
                relation_time_ = uid_locid_relation_[j][1]
                entity_locid_ = dict_locid_to_entity[locid_]
                catid_, relation_catid_ = dict_locid_catid[locid_][
                    0], dict_locid_catid[locid_][1]
                entity_catid_ = dict_catid_to_entity[catid_]
                relation_catid_ = dict_relation_catid_to_r[relation_catid_]
                if entity_locid_ not in self.dict_KG:
                    self.dict_KG[entity_locid_] = {}
                    for k in range(len(self.gender_set)):
                        self.dict_KG[entity_locid_]['gender' + '-' + str(
                            self.gender_set[k])] = []
                        if self.gender_set[k] == gender_:
                            self.dict_KG[entity_locid_]['gender' + '-' + str(
                                self.gender_set[k])].append([
                                    entity_locid_, relation_time_,
                                    entity_uid_category_
                                ])
                            self.dict_KG[entity_locid_]['gender' + '-' + str(
                                self.gender_set[k])].append([
                                    entity_locid_, relation_catid_,
                                    entity_catid_
                                ])
                    for k in range(len(self.race_set)):
                        self.dict_KG[entity_locid_]['race' + '-' + str(
                            self.race_set[k])] = []
                        if self.race_set[k] == race_:
                            self.dict_KG[entity_locid_]['race' + '-' + str(
                                self.race_set[k])].append([
                                    entity_locid_, relation_time_,
                                    entity_uid_category_
                                ])
                            self.dict_KG[entity_locid_]['race' + '-' + str(
                                self.race_set[k])].append([
                                    entity_locid_, relation_catid_,
                                    entity_catid_
                                ])
                else:
                    for k in range(len(self.gender_set)):
                        if self.gender_set[k] == gender_:
                            self.dict_KG[entity_locid_]['gender' + '-' + str(
                                self.gender_set[k])].append([
                                    entity_locid_, relation_time_,
                                    entity_uid_category_
                                ])
                            self.dict_KG[entity_locid_]['gender' + '-' + str(
                                self.gender_set[k])].append([
                                    entity_locid_, relation_catid_,
                                    entity_catid_
                                ])
                    for k in range(len(self.race_set)):
                        if self.race_set[k] == race_:
                            self.dict_KG[entity_locid_]['race' + '-' + str(
                                self.race_set[k])].append([
                                    entity_locid_, relation_time_,
                                    entity_uid_category_
                                ])
                            self.dict_KG[entity_locid_]['race' + '-' + str(
                                self.race_set[k])].append([
                                    entity_locid_, relation_catid_,
                                    entity_catid_
                                ])

        self.dict_itemid_upfdf = dict()
        for i in range(len(uid_name)):
            uid_upf_ = self.uid_attribute_category[i]
            uid_data_set_ = data_set[i]
            for j in range(len(uid_data_set_)):
                if uid_data_set_[j] not in self.dict_itemid_upfdf:
                    self.dict_itemid_upfdf[uid_data_set_[j]] = list()
                self.dict_itemid_upfdf[uid_data_set_[j]].append(uid_upf_)
        itemid_name = list(set(self.dict_itemid_upfdf.keys()))
        for i in range(len(itemid_name)):
            itemid_upfdf_ = self.dict_itemid_upfdf[itemid_name[i]]
            b_0, b_1, b_2, g_0, g_1, g_2 = 0, 0, 0, 0, 0, 0
            for j in range(len(itemid_upfdf_)):
                if itemid_upfdf_[j] == (0, 0):
                    b_0 += 1
                elif itemid_upfdf_[j] == (0, 1):
                    b_1 += 1
                elif itemid_upfdf_[j] == (0, 2):
                    b_2 += 1
                elif itemid_upfdf_[j] == (1, 0):
                    g_0 += 1
                elif itemid_upfdf_[j] == (1, 1):
                    g_1 += 1
                elif itemid_upfdf_[j] == (1, 2):
                    g_2 += 1
                else:
                    print('Error!!')
            b_0_p, b_1_p, b_2_p = b_0 / len(itemid_upfdf_), b_1 / len(
                itemid_upfdf_), b_2 / len(itemid_upfdf_)
            g_0_p, g_1_p, g_2_p = g_0 / len(itemid_upfdf_), g_1 / len(
                itemid_upfdf_), g_2 / len(itemid_upfdf_)
            self.dict_itemid_upfdf[itemid_name[i]] = [
                b_0_p, b_1_p, b_2_p, g_0_p, g_1_p, g_2_p
            ]

        num_user = len(uid_name)
        num_item = len(entity_set)
        n_entities = len(entity_set) + len(uid_category_set) + len(catid_set)
        n_relation = len(relation_time_set) + len(relation_catid_set)

        train = Interactions(train_set, num_user, num_item)
        train.to_sequence(self.L_hgn, self.T_hgn)

        sequences_np = train.sequences.sequences
        targets_np = train.sequences.targets
        users_np = train.sequences.user_ids
        train_matrix = train.tocsr()

        param_ = [self.args, num_user, num_item, n_entities, n_relation]
        data_ = [users_np, sequences_np, targets_np, train_matrix]
        test_ = [
            train, self.test_X_set, test_Y_set, self.uid_attribute_category
        ]

        train_data, test_data = list(), list()
        entity_set = set(entity_set)
        for i in range(len(train_set)):
            uid_ = i
            neg_entity_set_ = list(entity_set - set(train_set[i]))
            neg_train_set_ = random.sample(neg_entity_set_, len(train_set[i]))
            for j in range(len(train_set[i])):
                train_data.append([uid_, train_set[i][j], 1])
                train_data.append([uid_, neg_train_set_[j], 0])
        for i in range(len(test_set)):
            uid_ = i
            neg_entity_set_ = list(entity_set - set(test_set[i]))
            neg_test_set_ = random.sample(neg_entity_set_, len(test_set[i]))
            for j in range(len(test_set[i])):
                test_data.append([uid_, test_set[i][j], 1])
                test_data.append([uid_, neg_test_set_[j], 0])
        train_data = np.array(train_data)
        eval_data = train_data
        test_data = np.array(test_data)

        user_history_dict = dict()
        for i in range(len(data_set)):
            uid_ = i
            user_history_dict[uid_] = data_set[i]

        pickle_data = {
            'train_data': train_data,
            'eval_data': eval_data,
            'test_data': test_data,
            'n_entity': n_entities,
            'n_relation': n_relation,
            'kg_np': self.KG_random_generator(),
            'user_history_dict': user_history_dict,
        }
        return param_, data_, test_
Exemplo n.º 20
0
print(data_time[-1])
print(data_time[-2])

subgraphs_mapping_i, subgraphs_G, subgraphs_mapping_u = hgut.subgraph_con(
    train_set, data_time[0], data_time[-2])

subgraphs_mapping_i, reversed_subgraphs_mapping_i, sorted_time, subgraphs_sequence_i, reversed_subgraphs_mapping_last_i = hgut.subgraph_key_building(
    subgraphs_mapping_i, num_items)

subgraphs_mapping_u, reversed_subgraphs_mapping_u, sorted_time_u, subgraphs_sequence_u, reversed_subgraphs_mapping_last_u = hgut.subgraph_key_building(
    subgraphs_mapping_u, num_users)

assert sorted_time == sorted_time_u

train_data = Interactions(train_set, data_time[0], num_users, num_items,
                          sorted_time)
train_data.to_sequence(subgraphs_mapping_i, subgraphs_mapping_u,
                       subgraphs_sequence_i, subgraphs_sequence_u,
                       args.seq_len, args.T)

#for bpr pretrain
bpr_tuples = list(zip(train_data.user_ids, train_data.item_ids))
train_dic = {}
for i in train_set:
    train_dic[i] = set(train_set[i])

neg_test_dy = np.zeros((num_users, 101), dtype=np.int64)
for i in range(1, num_users):
    for j in range(101):
        neg_test_dy[i][j] = subgraphs_sequence_i[neg_test[i][j]][-1]