Пример #1
0
    def testing(self, fea2obj):
        config = self._config
        dsdir = config['dsdir']
        devfile = dsdir + '/dev.txt'
        testfile = dsdir + '/test.txt'
        networkfile = config['net']
        batch_size = 10000#int(config['batchsize'])
        devMentions = load_ent_ds(devfile)
        tstMentions = load_ent_ds(testfile)
        logger.info('#dev: %d #test: %d', len(devMentions), len(tstMentions))
        
        main_loop = load(networkfile + '.best.pkl')
        logger.info('Model loaded. Building prediction function...')
        old_model = main_loop.model
        logger.info(old_model.inputs)
        sources = [inp.name for inp in old_model.inputs]
#         fea2obj = build_input_objs(sources, config)
        t2idx = fea2obj['targets'].t2idx
        deterministic = str_to_bool(config['use_mean_pred']) if 'use_mean_pred' in config else True 
        kl_weight = shared_floatx(0.001, 'kl_weight')
        entropy_weight= shared_floatx(0.001, 'entropy_weight')
       
       
        cost, _, y_hat, _, _,_,_ = build_model_new(fea2obj, len(t2idx), self._config, kl_weight, entropy_weight, deterministic=deterministic, test=True)
        model = Model(cost)
        model.set_parameter_values(old_model.get_parameter_values())
        
        theinputs = []
        for fe in fea2obj.keys():
            if 'targets' in fe:
                continue
            for inp in model.inputs:
                if inp.name == fe:
                    theinputs.append(inp)
                    
#         theinputs = [inp for inp in model.inputs if inp.name != 'targets']
        print "theinputs: ", theinputs
        predict = theano.function(theinputs, y_hat)
        
        test_stream, num_samples_test = get_comb_stream(fea2obj, 'test', batch_size, shuffle=False)
        dev_stream, num_samples_dev = get_comb_stream(fea2obj, 'dev', batch_size, shuffle=False)
        logger.info('sources: %s -- number of test/dev samples: %d/%d', test_stream.sources, num_samples_test, num_samples_dev)
        idx2type = {idx:t for t,idx in t2idx.iteritems()}
        
        logger.info('Starting to apply on dev inputs...')
        self.applypredict(theinputs, predict, dev_stream, devMentions, num_samples_dev, batch_size, os.path.join(config['exp_dir'], config['matrixdev']), idx2type)
        logger.info('...apply on dev data finished')
        
        logger.info('Starting to apply on test inputs...')
        self.applypredict(theinputs, predict, test_stream, tstMentions, num_samples_test, batch_size, os.path.join(config['exp_dir'], config['matrixtest']), idx2type)
        logger.info('...apply on test data finished')
Пример #2
0
            f.write(onestr.strip() + '\n')
    f.close()
    logger.info('P@1 is = %f ', goods / num_samples)


if __name__ == '__main__':
    logger.info('loading config file: %s', sys.argv[1])
    config = cmn.loadConfig(sys.argv[1])
    dsdir = config['dsdir']
    devfile = dsdir + '/dev.txt'
    testfile = dsdir + '/test.txt'
    networkfile = config['net']
    num_of_hidden_units = int(config['hidden_units'])
    targetTypesFile = config['typefile']
    batch_size = int(config['batchsize'])
    devMentions = load_ent_ds(devfile)
    tstMentions = load_ent_ds(testfile)
    logger.info('#dev: %d #test: %d', len(devMentions), len(tstMentions))

    main_loop = load(networkfile + '.best.pkl')
    logger.info('Model loaded. Building prediction function...')
    model = main_loop.model
    logger.info(model.inputs)
    sources = [inp.name for inp in model.inputs]
    theinputs = [inp for inp in model.inputs if inp.name != 'targets']

    linear_output = [v for v in model.variables
                     if v.name == 'linear_output'][0]
    y_hat = Logistic().apply(linear_output)
    predict = theano.function(theinputs, y_hat)