Exemplo n.º 1
0
def evaluate_emb(reviews, model, config):

    graph = tf.Graph()
    with graph.as_default():
        tf.set_random_seed(27)
        # construct model graph
        print('Building graph...')
        builder = GraphBuilder()
        inputs, outputs, model_param = builder.construct_model_graph(
            reviews, config, model, training=False)
        init = tf.global_variables_initializer()

    with tf.Session(graph=graph) as session:
        # We must initialize all variables before we use them.
        print('Initializing...')
        init.run()

        llh_array = []
        pos_llh_array = []
        mul_llh_array = []
        review_size = reviews['scores'].shape[0]
        print('Calculating llh of instances...')
        for step in xrange(review_size):
            att, index, label = generate_batch(reviews, step)
            if index.size <= 1:  # neglect views with only one entry
                continue
            feed_dict = {
                inputs['input_att']: att,
                inputs['input_ind']: index,
                inputs['input_label']: label
            }
            ins_llh_val, pos_llh_val, mul_llh_val = session.run(
                (outputs['ins_llh'], outputs['pos_llh'], outputs['mul_llh']),
                feed_dict=feed_dict)

            #if step == 0:
            #    predicts = session.run(outputs['debugv'], feed_dict=feed_dict)
            #    print('%d movies ' % predicts.shape[0])
            #    print(predicts)

            llh_array.append(ins_llh_val)
            pos_llh_array.append(pos_llh_val)
            mul_llh_array.append(mul_llh_val)

        llh_array = np.concatenate(llh_array, axis=0)
        pos_llh_array = np.concatenate(pos_llh_array, axis=0)
        mul_llh_array = np.array(mul_llh_array)

        print("Loss and pos_loss mean: ", np.mean(llh_array),
              np.mean(pos_llh_array))

        return llh_array, pos_llh_array, mul_llh_array
def evaluate_emb(reviews, batch_feeder, model, config):

    graph = tf.Graph()
    with graph.as_default():
        tf.set_random_seed(27)
        # construct model graph
        print('Building evaluation graph...')
        builder = GraphBuilder()
        inputs, outputs, model_param = builder.construct_model_graph(
            None, config, model,
            training=False)  # necessary sizes are in `model'
        init = tf.global_variables_initializer()

    with tf.Session(graph=graph) as session:
        # We must initialize all variables before we use them.
        print('Initializing evaluation graph...')
        init.run()

        pos_llh_list = []
        neg_llh_list = []

        review_size = reviews.shape[0]
        print('Calculating llh of instances...')
        for step in xrange(review_size):
            index, label = batch_feeder(reviews[step])
            if index.size <= 1:  # neglect views with only one entry
                continue
            feed_dict = {
                inputs['input_ind']: index,
                inputs['input_label']: label
            }
            llh_item, nums = session.run(
                (outputs['llh_item'], outputs['num_items']),
                feed_dict=feed_dict)
            num_pos = nums[0]
            num_neg = nums[1]

            pos_llh_list.append(llh_item[0:num_pos])
            neg_llh_list.append(llh_item[num_pos:])

        pos_llh = np.concatenate(pos_llh_list)
        neg_llh = np.concatenate(neg_llh_list)

        mean_llh = (np.sum(pos_llh) + np.sum(neg_llh)) / (pos_llh.shape[0] +
                                                          neg_llh.shape[0])

        print("ELBO mean on the test set: ", mean_llh)

        return dict(pos_llh=pos_llh, neg_llh=neg_llh)
Exemplo n.º 3
0
def fit_emb(reviews, config, init_model):
    np.random.seed(27)

    use_valid_set = True
    if use_valid_set:
        reviews, valid_reviews = separate_valid(reviews, 0.1)

    graph = tf.Graph()
    with graph.as_default():
        tf.set_random_seed(27)

        builder = GraphBuilder()
        inputs, outputs, model_param = builder.construct_model_graph(
            reviews, config, init_model, training=True)

        optimizer = tf.train.AdagradOptimizer(0.05).minimize(
            outputs['objective'])
        init = tf.global_variables_initializer()

    with tf.Session(graph=graph) as session:
        # We must initialize all variables before we use them.
        init.run()

        nprint = 5000
        val_accum = np.array([0.0, 0.0])
        train_logg = np.zeros([int(config['max_iter'] / nprint) + 1, 3])

        review_size = reviews['scores'].shape[0]
        for step in xrange(1, config['max_iter'] + 1):

            rind = np.random.choice(review_size)
            atts, indices, labels = generate_batch(reviews, rind)
            if indices.size <= 1:  # neglect views with only one entry
                raise Exception(
                    'Row %d of the data has only one non-zero entry.' % rind)
            feed_dict = {
                inputs['input_att']: atts,
                inputs['input_ind']: indices,
                inputs['input_label']: labels
            }

            _, llh_val, obj_val, debug_val = session.run(
                (optimizer, outputs['llh'], outputs['objective'],
                 outputs['debugv']),
                feed_dict=feed_dict)
            val_accum = val_accum + np.array([llh_val, obj_val])

            # print loss every nprint iterations
            if step % nprint == 0 or np.isnan(llh_val) or np.isinf(llh_val):

                valid_llh = 0.0
                break_flag = False
                if use_valid_set:
                    valid_llh = validate(valid_reviews, session, inputs,
                                         outputs)
                    #if ivalid > 0 and valid_llh[ivalid] < valid_llh[ivalid - 1]: # performance becomes worse
                    #    print('validation llh: ', valid_llh[ivalid - 1], ' vs ', valid_llh[ivalid])
                    #    break_flag = True

                # record the three values
                ibatch = int(step / nprint)
                train_logg[ibatch, :] = np.append(val_accum / nprint,
                                                  valid_llh)
                val_accum[:] = 0.0  # reset the accumulater
                print("iteration[", step,
                      "]: average llh, obj, and valid_llh are ",
                      train_logg[ibatch, :])

                if np.isnan(llh_val) or np.isinf(llh_val):
                    print('Loss value is ', llh_val,
                          ', and the debug value is ', debug_val)
                    raise Exception('Bad values')

                if break_flag:
                    break

        # save model parameters to dict
        model = dict(alpha=model_param['alpha'].eval(),
                     rho=model_param['rho'].eval(),
                     invmu=model_param['invmu'].eval(),
                     weight=model_param['weight'].eval(),
                     nbr=model_param['nbr'].eval())

        return model, train_logg
def fit_emb(reviews, batch_feeder, config):

    do_log_save = True
    do_profiling = True
    log_save_path = 'log'

    # separate a validation set
    use_valid_set = True
    if use_valid_set:
        reviews, valid_reviews = separate_valid(reviews, 0.1)

    # build model graph
    with tf.device('/gpu:0'):
        graph = tf.Graph()
        with graph.as_default():
            tf.set_random_seed(27)
            builder = GraphBuilder()
            problem_size = {
                'num_reviews': reviews.shape[0],
                'num_items': reviews.shape[1]
            }
            inputs, outputs, model_param = builder.construct_model_graph(
                problem_size, config, init_model=None, training=True)

            model_vars = [
                model_param['alpha'], model_param['rho'],
                model_param['intercept'], model_param['prior_logit']
            ]

            optimizer = tf.train.AdagradOptimizer(0.1).minimize(
                outputs['objective'], var_list=model_vars)

            if config['model'] in ['context_select']:
                net_vars = builder.infer_net.param_list()
                net_optimizer = tf.train.AdagradOptimizer(0.1).minimize(
                    outputs['objective'], var_list=net_vars)

            init = tf.global_variables_initializer()

            # for visualization
            vis_conf = projector.ProjectorConfig()
            embedding = vis_conf.embeddings.add()
            embedding.tensor_name = model_param['alpha'].name

    # optimize the model
    with tf.Session(graph=graph) as session:
        # initialize all variables
        init.run()

        # Merge all the summaries and write them out to /tmp/mnist_logs (by defaul)
        if do_log_save:
            merged = tf.summary.merge_all()
            train_writer = tf.summary.FileWriter(log_save_path, session.graph)
            projector.visualize_embeddings(train_writer, vis_conf)
        else:
            merged = []

        if do_profiling:
            run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()

        nprint = config['nprint']

        val_accum = np.array([0.0, 0.0])
        count_accum = 0
        train_logg = np.zeros([int(config['max_iter'] / nprint) + 1, 3])

        review_size = reviews.shape[0]
        for step in xrange(1, config['max_iter'] + 1):

            rind = np.random.choice(review_size)
            indices, labels = batch_feeder(reviews[rind])

            if indices.shape[0] <= 1:  # neglect views with only one entry
                raise Exception(
                    'Row %d of the data has only one non-zero entry.' % rind)
            feed_dict = {
                inputs['input_ind']: indices,
                inputs['input_label']: labels
            }

            if config['model'] in ['context_select']:
                _, net_debugv, summary = session.run(
                    (net_optimizer, outputs['debugv'], merged),
                    feed_dict=feed_dict)
            else:
                net_debugv = ''

            _, llh_val, nums, obj_val, debug_val, summary = session.run((optimizer, outputs['llh_sum'], outputs['num_items'], \
                                                               outputs['objective'], outputs['debugv'], merged), feed_dict=feed_dict)

            if do_log_save:
                train_writer.add_summary(summary, step)

            # record llh, and objective
            val_accum = val_accum + np.array([llh_val, obj_val])
            count_accum = count_accum + (nums[0] + nums[1])

            # print loss every nprint iterations
            if step % nprint == 0 or np.isnan(llh_val) or np.isinf(llh_val):

                # do validation
                valid_llh = 0.0
                if use_valid_set:
                    valid_llh = validate(valid_reviews, batch_feeder, session,
                                         inputs, outputs)

                # record the three values
                ibatch = int(step / nprint)
                train_logg[ibatch, :] = np.array([
                    val_accum[0] / count_accum, val_accum[1] / nprint,
                    valid_llh
                ])
                val_accum[:] = 0.0  # reset the accumulater
                count_accum = 0
                print("iteration[", step,
                      "]: average llh, obj, valid_llh, and debug_val are ",
                      train_logg[ibatch, :], debug_val, net_debugv)

                #check nan value
                if np.isnan(llh_val) or np.isinf(llh_val):
                    print('Loss value is ', llh_val,
                          ', and the debug value is ', debug_val)
                    raise Exception('Bad values')

                model = get_model(model_param, session, config)

                if do_log_save:
                    tf.train.Saver().save(session, log_save_path, step)

                # Create the Timeline object, and write it to a json
                if do_profiling:
                    tl = timeline.Timeline(run_metadata.step_stats)
                    ctf = tl.generate_chrome_trace_format()
                    with open(
                            log_save_path + '/timeline_step%d.json' %
                        (step / nprint), 'w') as f:
                        f.write(ctf)

                pickle.dump(model['alpha'], open('model_%d.json' & step, 'wb'),
                            -1)

        model = get_model(model_param, session, config)

        return model, train_logg