Exemplo n.º 1
0
                # self.diff_output = tf.nn.softmax(diff_output)
                self.action_pred_output = tf.nn.softmax(action_pred_output)

                with tf.variable_scope('action_pred_cost'):
                    action_pred_loss = get_action_pred_lossfunc(
                        self.action_pred_output,
                        self.action_pred_target_ph,
                        condition,
                        if_last_output=True)
                    self.action_pred_loss = action_pred_loss

                    # self.win_acc, win_acc_op = tf.metrics.accuracy(labels=tf.argmax(self.win_target_ph, 1),
                    #                                                predictions=tf.argmax(self.win_output, 1))
            if self.config.Learn.integral_update_flag:
                tvars_action_pred = tf.trainable_variables()
            else:
                tvars_action_pred = tf.trainable_variables(scope='prediction')
            for t in tvars_action_pred:
                print('tvars_action_pred: ' + str(t.name))
            action_grads = tf.gradients(tf.reduce_mean(self.action_pred_loss),
                                        tvars_action_pred)
            self.train_action_pred_op = optimizer.apply_gradients(
                zip(action_grads, tvars_action_pred))


if __name__ == '__main__':
    cvrnn_config_path = "../environment_settings/icehockey_cvrnn_PlayerLocalId_predict_nex_goal_config_embed_random.yaml"
    cvrnn_config = CVRNNCongfig.load(cvrnn_config_path)
    cvrnn = CVRNN(config=cvrnn_config)
    cvrnn()
Exemplo n.º 2
0
    acc_diff_var_by_event_all = []
    game_time_all = []
    acc_diff_mean_by_time_all = []
    acc_diff_var_by_time_all = []
    for model_info_setting in validated_model_type:

        model_category = model_info_setting.get('model_category')
        model_name = model_info_setting.get('model_name')
        model_number = model_info_setting.get('model_number')
        player_info = model_info_setting.get('player_info')
        if model_category == 'cvrnn':
            embed_mode = '_embed_random'
            predicted_target = '_PlayerLocalId_predict_nex_goal'
            icehockey_cvrnn_config_path = "../../environment_settings/icehockey_cvrnn{0}_config{1}{2}.yaml".format(
                predicted_target, player_info, embed_mode)
            icehockey_model_config = CVRNNCongfig.load(
                icehockey_cvrnn_config_path)
        elif model_category == 'cvae':
            predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
            # player_id_cluster_dir = '../../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
            icehockey_config_path = "../../environment_settings/icehockey_cvae_lstm{0}_config{1}.yaml".format(
                predicted_target, player_info)
            icehockey_model_config = CVAECongfig.load(icehockey_config_path)
        elif model_category == 'vhe':
            predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
            # player_id_cluster_dir = '../../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
            icehockey_config_path = "../../environment_settings/icehockey_vhe_lstm{0}_config{1}.yaml".format(
                predicted_target, player_info)
            icehockey_model_config = CVAECongfig.load(icehockey_config_path)
        elif model_category == 'encoder':
            predicted_target = '_PlayerLocalId_predict_next_goal'
            # player_id_cluster_dir = '../../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
def run_calibration():
    model_type = 'lstm_Qs'
    player_info = '_pid'
    apply_old = False
    apply_difference = False
    running_numbers = [0, 1, 2, 3, 4]
    if model_type == 'cvrnn':
        model_number = 601
        embed_mode = '_embed_random'
        predicted_target = '_PlayerLocalId_predict_nex_goal'
        icehockey_config_path = "../../environment_settings/" \
                                "icehockey_cvrnn{0}_config{1}{2}.yaml" \
            .format(predicted_target, player_info, embed_mode)
        config = CVRNNCongfig.load(icehockey_config_path)
    elif model_type == 'cvae':
        model_number = 601
        embed_mode = ''
        predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
        icehockey_config_path = "../../environment_settings/icehockey_cvae_lstm{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVAECongfig.load(icehockey_config_path)
    elif model_type == 'vhe':
        model_number = 601
        embed_mode = ''
        predicted_target = '_PlayerLocalId_predict_next_goal'  # playerId_
        icehockey_config_path = "../../environment_settings/icehockey_vhe_lstm{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVAECongfig.load(icehockey_config_path)
    elif model_type == 'encoder':
        model_number = 901
        embed_mode = ''
        predicted_target = '_PlayerLocalId_predict_next_goal'
        # player_id_cluster_dir = '../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
        icehockey_encoder_config_path = "../../environment_settings/" \
                                        "icehockey_stats_lstm_encoder{0}" \
                                        "_config{1}.yaml".format(predicted_target, player_info)
        config = EncoderConfig.load(icehockey_encoder_config_path)
    elif model_type == 'lstm_Qs':
        embed_mode = ''
        # 'model_2101_three_cut_lstm_next_Qs_featurev1_next_Qs_batch32_iterate10_lr1e-05_v1_MaxTL10_LSTM512_dense256'
        model_number = 901
        icehockey_config_path = "../../environment_settings/ice_hockey_predict_Qs_lstm{0}.yaml".format(
            player_info)
        config = LSTMQsCongfig.load(icehockey_config_path)
    else:
        raise ValueError('incorrect model type {0}'.format(model_type))
    calibration_features = ['period', 'scoreDifferential', 'zone', 'manpowerSituation', 'home_away']
    calibration_bins = {'period': {'feature_name': ('period'), 'range': (1, 2, 3, 4)},
                        'scoreDifferential': {'feature_name': ('scoreDifferential'), 'range': range(-10, 10)},
                        'zone': {'feature_name': ('zone'), 'range': ('dz', 'nz', 'oz')},
                        'manpowerSituation': {'feature_name': ('manpowerSituation'),
                                              'range': ('shortHanded', 'evenStrength', 'powerPlay')},
                        'home_away': {'feature_name': ('home_away'), 'range': (1, 0)},
                        # TODO: we must add the home/away label
                        }
    source_data_dir = '/Local-Scratch/oschulte/Galen/2018-2019/'
    model_data_store_dir = '/Local-Scratch/oschulte/Galen/Ice-hockey-data/2018-2019'


    Cali = Calibration(bins=calibration_bins, source_data_dir=source_data_dir,
                       calibration_features=calibration_features, config=config,
                       model_data_store_dir=model_data_store_dir, apply_old=apply_old,
                       apply_difference=apply_difference,
                       model_type=model_type, model_number=model_number,
                       player_info=player_info, calibration_type='next_goal',
                       # testing_dir_all=testing_dir_games_all[:1],
                       embed_mode=embed_mode,
                       focus_actions_list=[],
                       if_apply_cv=True,
                       running_numbers=running_numbers)
    Cali.construct_bin_dicts()
    for running_number in running_numbers:
        saved_network_dir, log_dir = get_model_and_log_name(config=config,
                                                            model_catagoery=model_type,
                                                            running_number=running_number)
        with open(saved_network_dir + '/testing_file_dirs_all.csv', 'rb') as f:
            # with open('../../sport_resource/ice_hockey_201819/' + '/testing_file_dirs_all.csv', 'rb') as f:
            testing_dir_all = f.readlines()
        testing_dir_games_all = []
        for testing_dir in testing_dir_all:
            testing_dir_games_all.append(str(int(testing_dir)) + '-playsequence-wpoi.json')
        Cali.aggregate_calibration_values(testing_dir_games_all=testing_dir_games_all, running_number=running_number)
    Cali.compute_distance()
    print Cali.save_calibration_dir
Exemplo n.º 4
0
def run():
    training = True
    local_test_flag = False
    box_msg = ''
    predict_action = '_predict_nex_goal'
    embed_mode = '_embed_random'
    running_number = 0  # running_number is [0, 1, 2, 3, 4] for 5-fold cross validation
    if len(predict_action) > 0:
        extra_prediction_flag = True
    else:
        extra_prediction_flag = False

    player_id_cluster_dir = '../sport_resource/ice_hockey_201819/local_player_id_2018_2019.json'
    predicted_target = '_PlayerLocalId'  # playerId_


    icehockey_cvrnn_config_path = "../environment_settings/" \
                                  "icehockey_cvrnn{0}{2}_config{1}{3}.yaml".format(predicted_target,
                                                                                   box_msg,
                                                                                   predict_action,
                                                                                   embed_mode)
    name_training_record_file = "record_training_cvrnn{0}{2}_config{1}{3}{4}.yaml".format(
        predicted_target, box_msg, predict_action, embed_mode,
        datetime.date.today().strftime('%Y-%m-%d-%H'))
    icehockey_cvrnn_config = CVRNNCongfig.load(icehockey_cvrnn_config_path)
    Prediction_MemoryBuffer.set_cache_memory(
        cache_number=icehockey_cvrnn_config.Arch.Predict.output_size)
    saved_network_dir, log_dir = get_model_and_log_name(
        config=icehockey_cvrnn_config,
        model_catagoery='cvrnn',
        running_number=running_number,
        date_msg='')

    source_data_dir = icehockey_cvrnn_config.Learn.save_mother_dir + '/oschulte/Galen/2018-2019/'  # you source data (before pre-preprocessing)
    data_store_dir = icehockey_cvrnn_config.Learn.save_mother_dir + '/oschulte/Galen/Ice-hockey-data/2018-2019/'  # your source data (after pre-preprocessing)

    dir_games_all = os.listdir(data_store_dir)
    # shuffle(dir_games_all)  # randomly shuffle the list
    if running_number == 0:
        training_dir_games_all = dir_games_all[0:len(dir_games_all) / 5 * 4 -
                                               running_number *
                                               len(dir_games_all) / 5]
    else:
        training_dir_games_all = dir_games_all[0: len(dir_games_all) / 5 * 4 - running_number * len(dir_games_all) / 5] \
                                 + dir_games_all[-running_number * len(dir_games_all) / 5:]

    test_validate_dir_games_all = [
        item for item in dir_games_all if item not in training_dir_games_all
    ]

    testing_dir_games_all = test_validate_dir_games_all[:len(
        test_validate_dir_games_all) / 2]
    validate_dir_games_all = test_validate_dir_games_all[
        len(test_validate_dir_games_all) / 2:]
    tmp_testing_dir_games_all = testing_dir_games_all[
        -10:]  # TODO: it is a small running testing, not the real one
    number_of_total_game = len(dir_games_all)
    icehockey_cvrnn_config.Learn.number_of_total_game = number_of_total_game

    os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    sess = tf.Session()
    cvrnn = CVRNN(config=icehockey_cvrnn_config,
                  extra_prediction_flag=extra_prediction_flag,
                  deterministic_decoder=True)
    cvrnn()
    sess.run(tf.global_variables_initializer())

    if not local_test_flag:
        if not os.path.exists(saved_network_dir):
            os.mkdir(saved_network_dir)
        # save the training and testing dir list
        if os.path.exists(saved_network_dir + '/training_file_dirs_all.csv'):
            os.rename(
                saved_network_dir + '/training_file_dirs_all.csv',
                saved_network_dir +
                '/bak_training_file_dirs_all_{0}.csv'.format(
                    datetime.date.today().strftime("%Y%B%d")))
        if os.path.exists(saved_network_dir + '/testing_file_dirs_all.csv'):
            os.rename(
                saved_network_dir + '/testing_file_dirs_all.csv',
                saved_network_dir +
                '/bak_testing_file_dirs_all_{0}.csv'.format(
                    datetime.date.today().strftime("%Y%B%d")))
        if os.path.exists(saved_network_dir + '/validate_file_dirs_all.csv'):
            os.rename(
                saved_network_dir + '/validate_file_dirs_all.csv',
                saved_network_dir +
                '/bak_validate_file_dirs_all_{0}.csv'.format(
                    datetime.date.today().strftime("%Y%B%d")))
        with open(saved_network_dir + '/training_file_dirs_all.csv',
                  'wb') as f:
            for dir in training_dir_games_all:
                f.write(dir + '\n')
        with open(saved_network_dir + '/validate_file_dirs_all.csv',
                  'wb') as f:
            for dir in validate_dir_games_all:
                f.write(dir + '\n')
        with open(saved_network_dir + '/testing_file_dirs_all.csv', 'wb') as f:
            for dir in testing_dir_games_all:
                f.write(dir + '\n')
    print('training the model.')
    with open(name_training_record_file, 'w') as training_file:
        run_network(sess=sess,
                    model=cvrnn,
                    config=icehockey_cvrnn_config,
                    log_dir=log_dir,
                    save_network_dir=saved_network_dir,
                    data_store=data_store_dir,
                    source_data_dir=source_data_dir,
                    training_dir_games_all=training_dir_games_all,
                    testing_dir_games_all=tmp_testing_dir_games_all,
                    player_id_cluster_dir=player_id_cluster_dir,
                    training_file=training_file)
    sess.close()
def run_calibration():
    model_type = 'cvrnn'
    player_info = '_box'
    apply_old = False
    apply_difference = False
    if model_type == 'cvrnn':
        model_number = 2101
        predicted_target = '_PlayerLocalId'
        icehockey_config_path = "../../environment_settings/icehockey_cvrnn{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVRNNCongfig.load(icehockey_config_path)
    elif model_type == 'lstm_Qs':
        model_number = 2101
        icehockey_config_path = "../../environment_settings/ice_hockey_predict_Qs_lstm{0}.yaml".format(
            player_info)
        config = LSTMQsCongfig.load(icehockey_config_path)
    else:
        raise ValueError('incorrect model type {0}'.format(model_type))
    calibration_features = [
        'period', 'scoreDifferential', 'zone', 'manpowerSituation', 'home_away'
    ]
    calibration_bins = {
        'period': {
            'feature_name': ('period'),
            'range': (1, 2, 3, 4)
        },
        'scoreDifferential': {
            'feature_name': ('scoreDifferential'),
            'range': range(-10, 10)
        },
        'zone': {
            'feature_name': ('zone'),
            'range': ('dz', 'nz', 'oz')
        },
        'manpowerSituation': {
            'feature_name': ('manpowerSituation'),
            'range': ('shortHanded', 'evenStrength', 'powerPlay')
        },
        'home_away': {
            'feature_name': ('home_away'),
            'range': (1, 0)
        },
        # TODO: we must add the home/away label
    }
    source_data_dir = '/Local-Scratch/oschulte/Galen/2018-2019/'
    model_data_store_dir = '/Local-Scratch/oschulte/Galen/Ice-hockey-data/2018-2019'
    Cali = Calibration(bins=calibration_bins,
                       source_data_dir=source_data_dir,
                       calibration_features=calibration_features,
                       config=config,
                       model_data_store_dir=model_data_store_dir,
                       apply_old=apply_old,
                       apply_difference=apply_difference,
                       model_type=model_type,
                       model_number=model_number,
                       player_info=player_info,
                       focus_actions_list=[])
    Cali.construct_bin_dicts()
    Cali.aggregate_calibration_values()
    Cali.compute_distance()
    print Cali.save_calibration_dir
Exemplo n.º 6
0
def run_calibration():
    model_type = 'cvrnn'
    player_info = '_box'
    apply_old = False
    apply_difference = True
    if model_type == 'cvrnn':
        model_number = 2101
        predicted_target = '_PlayerLocalId'
        icehockey_config_path = "../../environment_settings/icehockey_cvrnn{0}_config{1}.yaml".format(
            predicted_target, player_info)
        config = CVRNNCongfig.load(icehockey_config_path)

    elif model_type == 'lstm_diff':
        model_number = 2101
        icehockey_config_path = "../../environment_settings/ice_hockey_predict_score_diff_lstm{0}.yaml".format(
            player_info)
        config = LSTMQsCongfig.load(icehockey_config_path)
    else:
        raise ValueError('incorrect model type {0}'.format(model_type))
    calibration_features = [
        'period', 'scoreDifferential', 'zone', 'manpowerSituation', 'home_away'
    ]
    calibration_bins = {
        'period': {
            'feature_name': ('period'),
            'range': (1, 2, 3, 4)
        },
        'scoreDifferential': {
            'feature_name': ('scoreDifferential'),
            'range': range(-10, 10)
        },
        'zone': {
            'feature_name': ('zone'),
            'range': ('dz', 'nz', 'oz')
        },
        'manpowerSituation': {
            'feature_name': ('manpowerSituation'),
            'range': ('shortHanded', 'evenStrength', 'powerPlay')
        },
        'home_away': {
            'feature_name': ('home_away'),
            'range': (1, 0)
        },
        # TODO: we must add the home/away label
    }
    source_data_dir = '/Local-Scratch/oschulte/Galen/2018-2019/'
    model_data_store_dir = '/Local-Scratch/oschulte/Galen/Ice-hockey-data/2018-2019'

    testing_dir_games_all = []
    # with open('../../sport_resource/ice_hockey_201819/testing_file_dirs_all.csv', 'rb') as f:
    saved_network_dir, log_dir = get_model_and_log_name(
        config=config, model_catagoery=model_type)
    # with open(saved_network_dir + '/testing_file_dirs_all.csv', 'rb') as f:
    with open(
            '../../sport_resource/ice_hockey_201819/' +
            '/testing_file_dirs_all.csv', 'rb') as f:
        testing_dir_all = f.readlines()
    for testing_dir in testing_dir_all:
        testing_dir_games_all.append(
            str(int(testing_dir)) + '-playsequence-wpoi.json')

    Cali = Calibration(bins=calibration_bins,
                       source_data_dir=source_data_dir,
                       calibration_features=calibration_features,
                       config=config,
                       model_data_store_dir=model_data_store_dir,
                       apply_old=apply_old,
                       apply_difference=apply_difference,
                       model_type=model_type,
                       model_number=model_number,
                       player_info=player_info,
                       calibration_type='score_diff',
                       testing_dir_all=testing_dir_games_all,
                       focus_actions_list=[])
    Cali.construct_bin_dicts()
    Cali.aggregate_calibration_values()
    Cali.compute_distance()
    print Cali.save_calibration_dir