Example #1
0
    def train():
        mode = menu.mode_selection()

        # build the model
        opt = menu.single_choice('Optimizer?', ['Adam', 'RMSProp'],
                                 ['adam', 'rmsprop'])
        lr = menu.single_choice('Learning rate?', ['e-3', 'e-4', 'e-5'],
                                [1e-3, 1e-4, 1e-5])
        if opt == 'adam':
            optim = keras.optimizers.Adam(lr=lr)
        else:
            optim = keras.optimizers.RMSprop(lr=lr)

        model = interactive_model(mode, optim=optim)

        # fit the model
        model.fit(epochs=10000)
        print('\nFit completed!')

        best_accuracy = np.max(model.history.history['val_acc'])

        model.save(folderpath='saved_models/',
                   suffix='_{}'.format(round(best_accuracy,
                                             5)).replace('.', ''))

        # evaluate
        report = model.evaluate()
        bot.send_message(report, account='parro')

        print('Opt: {}'.format(opt))
        print('Lr: {}'.format(lr))
    def train():
        mode = menu.mode_selection()
        # fit the model
        model = interactive_model(mode)
        model.fit(epochs=10000,
                  early_stopping_patience=25,
                  early_stopping_on='val_mrr',
                  mode='max')
        print('\nFit completed!')

        # recommend
        target_indices = data.target_indices(mode, 'cluster_recurrent')
        print('Recommending...')
        recommendations = model.recommend_batch(target_indices)
        print('Recommendation count: ', len(recommendations))
        mrr = model.compute_MRR(recommendations)

        model.save(folderpath='saved_models/',
                   suffix='_{}'.format(round(mrr, 5)).replace('.', ''))
                             actions_involving_impression_session_no_action))
                    count += 1
                return r

        train = data.train_df(mode=self.mode, cluster=self.cluster)
        test = data.test_df(mode=self.mode, cluster=self.cluster)
        df = pd.concat([train, test])
        df = df.drop(['timestamp', 'step', 'platform', 'city', 'device', 'current_filters', 'prices'], axis=1)
        s = df.groupby(
            ['user_id', 'session_id']).progress_apply(func)
        s = s.apply(pd.Series).reset_index().melt(id_vars=['user_id', 'session_id'], value_name='tuple').sort_values(
            by=['user_id', 'session_id']).dropna()
        s[['item_id', 'actions_involving_impression_session_clickout_item',
           'actions_involving_impression_session_interaction_item_deals',
           'actions_involving_impression_session_interaction_item_image',
           'actions_involving_impression_session_interaction_item_info',
           'actions_involving_impression_session_interaction_item_rating',
           'actions_involving_impression_session_search_for_item',
           'actions_involving_impression_session_no_action']] = pd.DataFrame(s['tuple'].tolist(), index=s.index)
        s = s.drop(['variable', 'tuple'], axis=1)
        s = s.reset_index(drop=True)
        return s


if __name__ == '__main__':
    from utils.menu import mode_selection, cluster_selection
    cluster = cluster_selection()
    mode = mode_selection()
    c = ActionsInvolvingImpressionSession(mode=mode, cluster=cluster)
    c.save_feature()
            'item_id'] = df_times_in_impressions.index.astype(int)
        df_times_in_impressions = df_times_in_impressions.reindex(
            columns=['item_id', 'num_times_item_impressed'])
        df_times_in_impressions = df_times_in_impressions.sort_values(
            by=['item_id']).reset_index(drop=True)

        feature = pd.merge(clk_expanded,
                           df_times_in_impressions,
                           how='left',
                           on=['item_id']).fillna(0)
        feature.num_times_item_impressed = feature.num_times_item_impressed.astype(
            int)

        return feature[[
            'user_id', 'session_id', 'item_id', 'num_times_item_impressed'
        ]]


if __name__ == '__main__':
    import utils.menu as menu

    mode = menu.mode_selection()
    cluster = menu.cluster_selection()

    c = NumTimesItemImpressed(mode, cluster)

    print('Creating {} for {} {}'.format(c.name, c.mode, c.cluster))
    c.save_feature()

    print(c.read_feature())
        model = TensorflowRankig(mode=flags_dict['mode'],
                                 cluster='no_cluster',
                                 dataset_name=flags_dict['dataset_name'],
                                 pred_name=name)
        model.name = f'tf_ranking_{name}'
        model.run()
        HERA.send_message('EXPORTED... mode:{}, name:{}'.format(
            flags_dict['mode'], name))


if __name__ == '__main__':
    global flags_dict
    flags_dict = {}

    # let the user insert the mode cluster and dataset name
    _MODE = menu.mode_selection()
    _CLUSTER = menu.cluster_selection()
    _DATASET_NAME = input('insert dataset name: \n')

    _BASE_PATH = f'dataset/preprocessed/tf_ranking/{_CLUSTER}/{_MODE}/{_DATASET_NAME}'
    _TRAIN_PATH = f'{_BASE_PATH}/train.hdf'
    _TEST_PATH = f'{_BASE_PATH}/test.hdf'
    _VALI_PATH = f'{_BASE_PATH}/vali.hdf'

    min_mrr = float(input('insert the min_MRR from which export the sub: \n'))

    # lets load the context features id
    context_features_id = list(
        np.load(f'{_BASE_PATH}/context_features_id.npy'))
    print(f'context features id are: {context_features_id}')
    flags_dict['context_features_id'] = context_features_id