Python tensorflow.ConfigProto() Examples

The following are 30 code examples of tensorflow.ConfigProto(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: agents.py    From soccer-matlab with BSD 2-Clause "Simplified" License 7 votes vote down vote up
def __init__(self):

        self.session = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,log_device_placement=False))
        self.actor = networks.Actor_MLP(scope="actor1",units=[settings.S_DIM,100,settings.A_DIM],activations=[None,'relu','tanh'],trainable=True)
        self.old_actor = networks.Actor_MLP(scope="actor0",units=[settings.S_DIM,100,settings.A_DIM],activations=[None,'relu','tanh'],trainable=False)
        self.critic =  networks.Critic_MLP(scope="critic1",units=[settings.S_DIM,100,1],activations=[None,'relu',None],trainable=True)

        self.state_tf = tf.placeholder(dtype=tf.float32,shape=[None,settings.S_DIM])
        self.action_tf = tf.placeholder(dtype=tf.float32,shape=[None,settings.A_DIM])
        self.return_tf = tf.placeholder(dtype=tf.float32,shape=[None,1]) 
        self.adv_tf = tf.placeholder(dtype=tf.float32,shape=[None,1]) 
        
        # global steps to keep track of training
        self.actor_step = tf.get_variable('actor_global_step', [], initializer=tf.constant_initializer(0), trainable=False)
        self.critic_step = tf.get_variable('critic_global_step', [], initializer=tf.constant_initializer(0), trainable=False)

        # build computation graphs
        self.actor.build_graph(self.state_tf,self.actor_step) 
        self.old_actor.build_graph(self.state_tf,0)
        self.critic.build_graph(self.state_tf,self.critic_step)
        self.build_graph() 
Example #2
Source File: run_mujoco.py    From HardRLWithYoutube with MIT License 6 votes vote down vote up
def train(env_id, num_timesteps, seed):
    env = make_mujoco_env(env_id, seed)

    with tf.Session(config=tf.ConfigProto()):
        ob_dim = env.observation_space.shape[0]
        ac_dim = env.action_space.shape[0]
        with tf.variable_scope("vf"):
            vf = NeuralNetValueFunction(ob_dim, ac_dim)
        with tf.variable_scope("pi"):
            policy = GaussianMlpPolicy(ob_dim, ac_dim)

        learn(env, policy=policy, vf=vf,
            gamma=0.99, lam=0.97, timesteps_per_batch=2500,
            desired_kl=0.002,
            num_timesteps=num_timesteps, animate=False)

        env.close() 
Example #3
Source File: nn_model.py    From mercari-price-suggestion with MIT License 6 votes vote down vote up
def __init__(self, train_df, word_count, batch_size, epochs):
        tf.set_random_seed(4)
        session_conf = tf.ConfigProto(intra_op_parallelism_threads=2, inter_op_parallelism_threads=8)
        backend.set_session(tf.Session(graph=tf.get_default_graph(), config=session_conf))

        self.batch_size = batch_size
        self.epochs = epochs

        self.max_name_seq = 10
        self.max_item_desc_seq = 75
        self.max_text = word_count + 1
        self.max_brand = np.max(train_df.brand_name.max()) + 1
        self.max_condition = np.max(train_df.item_condition_id.max()) + 1
        self.max_subcat0 = np.max(train_df.subcat_0.max()) + 1
        self.max_subcat1 = np.max(train_df.subcat_1.max()) + 1
        self.max_subcat2 = np.max(train_df.subcat_2.max()) + 1 
Example #4
Source File: ensemble_gpu.py    From kaggle-carvana-2017 with MIT License 6 votes vote down vote up
def predictor(q, gpu, pq):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    with sess.as_default():
        model = create_model(gpu)
        while True:
            batch_fnames, x_batch = q.get()
            if x_batch is None:
                break

            preds = model.predict_on_batch(x_batch)

            for i, pred in enumerate(preds):
                filename = batch_fnames[i]
                pq.put((os.path.join(ensembling_dir, filename[:-4] + ".png"), pred)) 
Example #5
Source File: predict_multithreaded.py    From kaggle-carvana-2017 with MIT License 6 votes vote down vote up
def predictor(q, gpu):
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    with sess.as_default():
        model = create_model(gpu)
        while True:
            batch_fnames, x_batch = q.get()
            if x_batch is None:
                break

            preds = model.predict_on_batch(x_batch)

            if args.pred_tta:
                preds = undo_tta(preds, args.pred_tta)

            for i, pred in enumerate(preds):
                filename = batch_fnames[i]
                prediction = pred[:, 1:-1, :]
                array_to_img(prediction * 255).save(os.path.join(output_dir, filename.split('/')[-1][:-4] + ".png")) 
Example #6
Source File: chptToBin.py    From iAI with MIT License 6 votes vote down vote up
def chpt_to_dict_arrays_simple(file_name):
    """
        Convert a checkpoint into into a dictionary of numpy arrays 
        for later use in TensorRT NMT sample.
    """
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)

    saver = tf.train.import_meta_graph(file_name)
    dir_name = os.path.dirname(os.path.abspath(file_name))
    saver.restore(sess, tf.train.latest_checkpoint(dir_name))

    params = {}
    print ('\nFound the following trainable variables:')
    with sess.as_default():
        variables = tf.trainable_variables()
        for v in variables:
            params[v.name] = v.eval(session=sess)
            print ("{0}    {1}".format(v.name, params[v.name].shape))

    #use default value
    params["forget_bias"] = 1.0
    return params 
Example #7
Source File: chptToBin.py    From iAI with MIT License 6 votes vote down vote up
def chpt_to_dict_arrays_simple(file_name):
    """
        Convert a checkpoint into into a dictionary of numpy arrays 
        for later use in TensorRT NMT sample.
    """
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)

    saver = tf.train.import_meta_graph(file_name)
    dir_name = os.path.dirname(os.path.abspath(file_name))
    saver.restore(sess, tf.train.latest_checkpoint(dir_name))

    params = {}
    print ('\nFound the following trainable variables:')
    with sess.as_default():
        variables = tf.trainable_variables()
        for v in variables:
            params[v.name] = v.eval(session=sess)
            print ("{0}    {1}".format(v.name, params[v.name].shape))

    #use default value
    params["forget_bias"] = 1.0
    return params 
Example #8
Source File: chptToBin.py    From iAI with MIT License 6 votes vote down vote up
def chpt_to_dict_arrays_simple(file_name):
    """
        Convert a checkpoint into into a dictionary of numpy arrays 
        for later use in TensorRT NMT sample.
    """
    config = tf.ConfigProto(allow_soft_placement=True)
    sess = tf.Session(config=config)

    saver = tf.train.import_meta_graph(file_name)
    dir_name = os.path.dirname(os.path.abspath(file_name))
    saver.restore(sess, tf.train.latest_checkpoint(dir_name))

    params = {}
    print ('\nFound the following trainable variables:')
    with sess.as_default():
        variables = tf.trainable_variables()
        for v in variables:
            params[v.name] = v.eval(session=sess)
            print ("{0}    {1}".format(v.name, params[v.name].shape))

    #use default value
    params["forget_bias"] = 1.0
    return params 
Example #9
Source File: mdbt.py    From ConvLab with MIT License 6 votes vote down vote up
def test_update():
    os.environ["CUDA_VISIBLE_DEVICES"] = '0'
    _config = tf.ConfigProto()
    _config.gpu_options.allow_growth = True
    _config.allow_soft_placement = True
    start_time = time.time()
    mdbt = MDBTTracker()
    print('\tMDBT: model build time: {:.2f} seconds'.format(time.time() - start_time))
    saver = tf.train.Saver()
    mdbt.restore_model(mdbt.sess, saver)
    # demo state history
    mdbt.state['history'] = [['null', 'I\'m trying to find an expensive restaurant in the centre part of town.'],
                             [
                                 'The Cambridge Chop House is an good expensive restaurant in the centre of town. Would you like me to book it for you?',
                                 'Yes, a table for 1 at 16:15 on sunday.  I need the reference number.']]
    new_state = mdbt.update(None, 'hi, this is not good')
    print(json.dumps(new_state, indent=4))
    print('all time: {:.2f} seconds'.format(time.time() - start_time)) 
Example #10
Source File: model_based_policy.py    From cs294-112_hws with MIT License 6 votes vote down vote up
def _setup_graph(self):
        """
        Sets up the tensorflow computation graph for training, prediction, and action selection

        The variables returned will be set as class attributes (see __init__)
        """
        tf_config = tf.ConfigProto()
        tf_config.gpu_options.allow_growth = True
        sess = tf.Session(config=tf_config)

        ### PROBLEM 1
        ### YOUR CODE HERE
        state_ph, action_ph, next_state_ph = self._setup_placeholders()
        next_state_pred = self._dynamics_func(state_ph, action_ph, False)
        loss, optimizer = self._setup_training(state_ph, next_state_ph, next_state_pred)
        ### PROBLEM 2
        ### YOUR CODE HERE
        best_action = self._setup_action_selection(state_ph)

        sess.run(tf.global_variables_initializer())

        return sess, state_ph, action_ph, next_state_ph, \
                next_state_pred, loss, optimizer, best_action 
Example #11
Source File: reaction.py    From armchair-expert with MIT License 6 votes vote down vote up
def __init__(self, path: str = None, use_gpu=False):

        import tensorflow as tf
        from keras.models import Sequential
        from keras.layers import Dense
        from keras.backend import set_session

        self.model = Sequential()
        self.model.add(Dense(AOLReactionFeatureAnalyzer.NUM_FEATURES, activation='relu',
                             input_dim=AOLReactionFeatureAnalyzer.NUM_FEATURES))
        self.model.add(Dense(AOLReactionFeatureAnalyzer.NUM_FEATURES - 2, activation='relu'))
        self.model.add(Dense(1, activation='sigmoid'))
        self.model.compile(optimizer='rmsprop',
                           loss='binary_crossentropy',
                           metrics=['accuracy'])

        if use_gpu:
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            set_session(tf.Session(config=config)) 
Example #12
Source File: structure.py    From armchair-expert with MIT License 6 votes vote down vote up
def __init__(self, use_gpu: bool = False):
        import tensorflow as tf
        from keras.models import Sequential
        from keras.layers import Dense, Embedding
        from keras.layers import LSTM
        from keras.backend import set_session

        latent_dim = StructureModel.SEQUENCE_LENGTH * 8

        model = Sequential()
        model.add(
            Embedding(StructureFeatureAnalyzer.NUM_FEATURES, StructureFeatureAnalyzer.NUM_FEATURES,
                      input_length=StructureModel.SEQUENCE_LENGTH))
        model.add(LSTM(latent_dim, dropout=0.2, return_sequences=False))
        model.add(Dense(StructureFeatureAnalyzer.NUM_FEATURES, activation='softmax'))
        model.summary()
        model.compile(loss='sparse_categorical_crossentropy', optimizer='adam')
        self.model = model

        if use_gpu:
            config = tf.ConfigProto()
            config.gpu_options.allow_growth = True
            set_session(tf.Session(config=config)) 
Example #13
Source File: interface.py    From Generative-adversarial-Nets-in-NLP with Apache License 2.0 6 votes vote down vote up
def train(self):
		# Construct model
		model = Transformer()
		print("Graph loaded")
		init = tf.global_variables_initializer()

		config = tf.ConfigProto()
		config.gpu_options.allow_growth = True

		# Start training
		sv = tf.train.Supervisor(logdir=pm.logdir, save_model_secs=0, init_op=init)
		saver = sv.saver
		with sv.managed_session(config=config) as sess:
			for epoch in range(1, pm.num_epochs + 1):
				if sv.should_stop():
					break
				for _ in tqdm(range(model.num_batch), total=model.num_batch, ncols=70, leave=False, unit='b'):
					sess.run(model.optimizer)

				gs = sess.run(model.global_step)
				saver.save(sess, pm.logdir + '/model_epoch_{}_global_step_{}'.format(epoch, gs))

		print("MSG : Done for training!") 
Example #14
Source File: audio_feature_extractor.py    From Tensorflow-Audio-Classification with Apache License 2.0 6 votes vote down vote up
def __init__(self, checkpoint, pca_params, input_tensor_name, output_tensor_name):
        """Create a new Graph and a new Session for every VGGishExtractor object."""
        super(VGGishExtractor, self).__init__()
        
        self.graph = tf.Graph()
        with self.graph.as_default():
            vggish_slim.define_vggish_slim(training=False)

        sess_config = tf.ConfigProto(allow_soft_placement=True)
        sess_config.gpu_options.allow_growth = True
        self.sess = tf.Session(graph=self.graph, config=sess_config)
        vggish_slim.load_defined_vggish_slim_checkpoint(self.sess, checkpoint)
        
        # use the self.sess to init others
        self.input_tensor = self.graph.get_tensor_by_name(input_tensor_name)
        self.output_tensor = self.graph.get_tensor_by_name(output_tensor_name)

        # postprocessor
        self.postprocess = vggish_postprocess.Postprocessor(pca_params) 
Example #15
Source File: run_atari.py    From lirpg with MIT License 6 votes vote down vote up
def train(env_id, num_timesteps, seed, policy):

    ncpu = multiprocessing.cpu_count()
    if sys.platform == 'darwin': ncpu //= 2
    config = tf.ConfigProto(allow_soft_placement=True,
                            intra_op_parallelism_threads=ncpu,
                            inter_op_parallelism_threads=ncpu)
    config.gpu_options.allow_growth = True #pylint: disable=E1101
    tf.Session(config=config).__enter__()

    env = VecFrameStack(make_atari_env(env_id, 8, seed), 4)
    policy = {'cnn' : CnnPolicy, 'lstm' : LstmPolicy, 'lnlstm' : LnLstmPolicy}[policy]
    ppo2.learn(policy=policy, env=env, nsteps=128, nminibatches=4,
        lam=0.95, gamma=0.99, noptepochs=4, log_interval=1,
        ent_coef=.01,
        lr=lambda f : f * 2.5e-4,
        cliprange=lambda f : f * 0.1,
        total_timesteps=int(num_timesteps * 1.1)) 
Example #16
Source File: run_mujoco.py    From lirpg with MIT License 6 votes vote down vote up
def train(env_id, num_timesteps, seed):
    env = make_mujoco_env(env_id, seed)

    with tf.Session(config=tf.ConfigProto()):
        ob_dim = env.observation_space.shape[0]
        ac_dim = env.action_space.shape[0]
        with tf.variable_scope("vf"):
            vf = NeuralNetValueFunction(ob_dim, ac_dim)
        with tf.variable_scope("pi"):
            policy = GaussianMlpPolicy(ob_dim, ac_dim)

        learn(env, policy=policy, vf=vf,
            gamma=0.99, lam=0.97, timesteps_per_batch=2500,
            desired_kl=0.002,
            num_timesteps=num_timesteps, animate=False)

        env.close() 
Example #17
Source File: trainer_lib.py    From fine-lm with MIT License 6 votes vote down vote up
def create_session_config(log_device_placement=False,
                          enable_graph_rewriter=False,
                          gpu_mem_fraction=0.95,
                          use_tpu=False,
                          inter_op_parallelism_threads=0,
                          intra_op_parallelism_threads=0):
  """The TensorFlow Session config to use."""
  if use_tpu:
    graph_options = tf.GraphOptions()
  else:
    if enable_graph_rewriter:
      rewrite_options = rewriter_config_pb2.RewriterConfig()
      rewrite_options.layout_optimizer = rewriter_config_pb2.RewriterConfig.ON
      graph_options = tf.GraphOptions(rewrite_options=rewrite_options)
    else:
      graph_options = tf.GraphOptions(
          optimizer_options=tf.OptimizerOptions(
              opt_level=tf.OptimizerOptions.L1, do_function_inlining=False))

  gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_mem_fraction)

  config = tf.ConfigProto(
      allow_soft_placement=True,
      graph_options=graph_options,
      gpu_options=gpu_options,
      log_device_placement=log_device_placement,
      inter_op_parallelism_threads=inter_op_parallelism_threads,
      intra_op_parallelism_threads=intra_op_parallelism_threads)
  return config 
Example #18
Source File: tfutil.py    From disentangling_conditional_gans with MIT License 6 votes vote down vote up
def create_session(config_dict=dict(), force_as_default=False):
    config = tf.ConfigProto()
    for key, value in config_dict.items():
        fields = key.split('.')
        obj = config
        for field in fields[:-1]:
            obj = getattr(obj, field)
        setattr(obj, fields[-1], value)
    session = tf.Session(config=config)
    if force_as_default:
        session._default_session = session.as_default()
        session._default_session.enforce_nesting = False
        session._default_session.__enter__()
    return session

#----------------------------------------------------------------------------
# Initialize all tf.Variables that have not already been initialized.
# Equivalent to the following, but more efficient and does not bloat the tf graph:
#   tf.variables_initializer(tf.report_unitialized_variables()).run() 
Example #19
Source File: sampling.py    From dcnn_textvae with MIT License 5 votes vote down vote up
def sampling():
    batchloader = BatchLoader(with_label=True)

    sess_conf = tf.ConfigProto(
        gpu_options = tf.GPUOptions(
            # allow_growth = True
        )
    )

    with tf.Graph().as_default():
        with tf.Session(config=sess_conf) as sess:
            with tf.variable_scope("VAE"):
                vae_restored = VAE[FLAGS.VAE_NAME](batchloader, is_training=False, ru=False)

            saver = tf.train.Saver()
            saver.restore(sess, MODEL_DIR + "/model40.ckpt")

            itr = SAMPLE_NUM // FLAGS.BATCH_SIZE
            res = SAMPLE_NUM - itr * FLAGS.BATCH_SIZE

            generated_texts = []
            lv_list= []
            for i in range(itr+1):
                z = np.random.normal(loc=0.0, scale=1.0,
                                     size=[FLAGS.BATCH_SIZE, FLAGS.LATENT_VARIABLE_SIZE])
                sample_logits = sess.run(vae_restored.logits,
                                         feed_dict = {vae_restored.latent_variables: z})
                lv_list.extend(z)

                if i==itr:
                    sample_num = res
                else:
                    sample_num = FLAGS.BATCH_SIZE

                sample_texts = batchloader.logits2str(logits=sample_logits, sample_num=sample_num)
                generated_texts.extend(sample_texts)

            for i in range(SAMPLE_NUM):
                log_and_print(SAVE_FILE, generated_texts[i]) 
Example #20
Source File: util.py    From HardRLWithYoutube with MIT License 5 votes vote down vote up
def simple_test(env_fn, learn_fn, min_reward_fraction, n_trials=N_TRIALS):
    np.random.seed(0)
    np_random.seed(0)

    env = DummyVecEnv([env_fn])


    with tf.Graph().as_default(), tf.Session(config=tf.ConfigProto(allow_soft_placement=True)).as_default():
        tf.set_random_seed(0)

        model = learn_fn(env)

        sum_rew = 0
        done = True

        for i in range(n_trials):
            if done:
                obs = env.reset()
                state = model.initial_state

            if state is not None:
                a, v, state, _ = model.step(obs, S=state, M=[False])
            else:
                a, v, _, _ = model.step(obs)
            
            obs, rew, done, _ = env.step(a)
            sum_rew += float(rew)

        print("Reward in {} trials is {}".format(n_trials, sum_rew))
        assert sum_rew > min_reward_fraction * n_trials, \
            'sum of rewards {} is less than {} of the total number of trials {}'.format(sum_rew, min_reward_fraction, n_trials) 
Example #21
Source File: utils.py    From ppo-lstm-parallel with MIT License 5 votes vote down vote up
def create_session(env_opts, on_gpu):
    import tensorflow as tf
    if env_opts["use_gpu"] and on_gpu:
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=env_opts["mem_fraction"])
        session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
    else:
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.05)
        session = tf.Session(config=tf.ConfigProto(device_count={'CPU': 1, 'GPU': 0}, gpu_options=gpu_options))
    return session 
Example #22
Source File: xnn.py    From tensorflow-XNN with MIT License 5 votes vote down vote up
def _init_session(self):
        config = tf.ConfigProto(device_count={"gpu": 0})
        config.gpu_options.allow_growth = True
        # the following reduce the training time for a snapshot from 180~220s to 100s in kernel
        config.intra_op_parallelism_threads = 4
        config.inter_op_parallelism_threads = 4
        sess = tf.Session(config=config)
        sess.run(tf.global_variables_initializer())
        # max_to_keep=None, keep all the models
        saver = tf.train.Saver(max_to_keep=None)
        return sess, saver 
Example #23
Source File: face_model.py    From insightface with MIT License 5 votes vote down vote up
def __init__(self, args):
    model = edict()
    with tf.Graph().as_default():
      config = tf.ConfigProto()
      config.gpu_options.per_process_gpu_memory_fraction = 0.2
      sess = tf.Session(config=config)
      #sess = tf.Session()
      with sess.as_default():
        self.pnet, self.rnet, self.onet = detect_face.create_mtcnn(sess, None)

    self.threshold = args.threshold
    self.det_minsize = 50
    self.det_threshold = [0.4,0.6,0.6]
    self.det_factor = 0.9
    _vec = args.image_size.split(',')
    assert len(_vec)==2
    self.image_size = (int(_vec[0]), int(_vec[1]))
    _vec = args.model.split(',')
    assert len(_vec)==2
    prefix = _vec[0]
    epoch = int(_vec[1])
    print('loading',prefix, epoch)
    self.model = edict()
    self.model.ctx = mx.gpu(args.gpu)
    self.model.sym, self.model.arg_params, self.model.aux_params = mx.model.load_checkpoint(prefix, epoch)
    self.model.arg_params, self.model.aux_params = ch_dev(self.model.arg_params, self.model.aux_params, self.model.ctx)
    all_layers = self.model.sym.get_internals()
    self.model.sym = all_layers['fc1_output'] 
Example #24
Source File: evaluate_supervise.py    From GroundeR with MIT License 5 votes vote down vote up
def run_evaluate():
	train_list = []
	test_list = []
	config = Config()
	train_list = load_img_id_list(config.train_file_list)
	test_list = load_img_id_list(config.test_file_list)

	config.save_path = config.save_path + '_' + args.model_name
	assert(os.path.isdir(config.save_path))
	restore_id = args.restore_id
	assert(restore_id > 0)

	cur_dataset = dataprovider(train_list, test_list, config.img_feat_dir, config.sen_dir, config.vocab_size,
								batch_size=config.batch_size)

	model = ground_model(config)
	gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3)

	with tf.Graph().as_default():
		loss, train_op, loss_vec, logits = model.build_model()
		# Create a session for running Ops on the Graph.
		sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
		# Run the Op to initialize the variables.
		init = tf.global_variables_initializer()
		sess.run(init)
		saver = tf.train.Saver(max_to_keep=100)

		feed_dict = update_feed_dict(cur_dataset, model, False)
		print 'Restore model_%d'%restore_id
		saver.restore(sess, './model/%s/model_%d.ckpt'%(config.save_path, restore_id)) 
				
		print "-----------------------------------------------"
		eval_accu = run_eval(sess, cur_dataset, model, logits, feed_dict)
		print "-----------------------------------------------" 
Example #25
Source File: main.py    From mac-network with Apache License 2.0 5 votes vote down vote up
def setSession():
    sessionConfig = tf.ConfigProto(allow_soft_placement = True, log_device_placement = False)
    if config.allowGrowth:
        sessionConfig.gpu_options.allow_growth = True
    if config.maxMemory < 1.0:
        sessionConfig.gpu_options.per_process_gpu_memory_fraction = config.maxMemory
    return sessionConfig

############################################## savers #############################################
# Initializes savers (standard, optional exponential-moving-average and optional for subset of variables) 
Example #26
Source File: build.py    From Automatic-Identification-and-Counting-of-Blood-Cells with GNU General Public License v3.0 5 votes vote down vote up
def setup_meta_ops(self):
        cfg = dict({
            'allow_soft_placement': False,
            'log_device_placement': False
        })

        utility = min(self.FLAGS.gpu, 1.)
        if utility > 0.0:
            self.say('GPU mode with {} usage'.format(utility))
            cfg['gpu_options'] = tf.GPUOptions(
                per_process_gpu_memory_fraction=utility)
            cfg['allow_soft_placement'] = True
        else:
            self.say('Running entirely on CPU')
            cfg['device_count'] = {'GPU': 0}

        if self.FLAGS.train: self.build_train_op()

        if self.FLAGS.summary:
            self.summary_op = tf.summary.merge_all()
            self.writer = tf.summary.FileWriter(self.FLAGS.summary + 'train')

        self.sess = tf.Session(config=tf.ConfigProto(**cfg))
        self.sess.run(tf.global_variables_initializer())

        if not self.ntrain: return
        self.saver = tf.train.Saver(tf.global_variables(),
                                    max_to_keep=self.FLAGS.keep)
        if self.FLAGS.load != 0: self.load_from_ckpt()

        if self.FLAGS.summary:
            self.writer.add_graph(self.sess.graph) 
Example #27
Source File: util.py    From HardRLWithYoutube with MIT License 5 votes vote down vote up
def reward_per_episode_test(env_fn, learn_fn, min_avg_reward, n_trials=N_EPISODES):
    env = DummyVecEnv([env_fn])

    with tf.Graph().as_default(), tf.Session(config=tf.ConfigProto(allow_soft_placement=True)).as_default():
        model = learn_fn(env)

        N_TRIALS = 100    

        observations, actions, rewards = rollout(env, model, N_TRIALS)
        rewards = [sum(r) for r in rewards]

        avg_rew = sum(rewards) / N_TRIALS
        print("Average reward in {} episodes is {}".format(n_trials, avg_rew))
        assert avg_rew > min_avg_reward, \
            'average reward in {} episodes ({}) is less than {}'.format(n_trials, avg_rew, min_avg_reward) 
Example #28
Source File: train_ac_exploration_f18.py    From cs294-112_hws with MIT License 5 votes vote down vote up
def init_tf_sess(self):
        tf_config = tf.ConfigProto(inter_op_parallelism_threads=1, intra_op_parallelism_threads=1)
        tf_config.gpu_options.allow_growth = True # may need if using GPU
        self.sess = tf.Session(config=tf_config)
        self.sess.__enter__() # equivalent to `with self.sess:`
        tf.global_variables_initializer().run() #pylint: disable=E1101 
Example #29
Source File: run_mujoco.py    From lirpg with MIT License 5 votes vote down vote up
def train(env_id, num_timesteps, seed, policy, r_ex_coef, r_in_coef, lr_alpha, lr_beta, reward_freq):
    from baselines.common import set_global_seeds
    from baselines.common.vec_env.vec_normalize import VecNormalize
    from baselines.ppo2 import ppo2
    from baselines.ppo2.policies import MlpPolicy, MlpPolicyIntrinsicReward
    import gym
    import tensorflow as tf
    from baselines.common.vec_env.dummy_vec_env import DummyVecEnv
    ncpu = 1
    config = tf.ConfigProto(allow_soft_placement=True,
                            intra_op_parallelism_threads=ncpu,
                            inter_op_parallelism_threads=ncpu)
    config.gpu_options.allow_growth = True
    tf.Session(config=config).__enter__()
    def make_env():
        env = gym.make(env_id)
        env = bench.Monitor(env, logger.get_dir())
        return env
    env = DummyVecEnv([make_env])
    env = VecNormalize(env)

    set_global_seeds(seed)
    if policy == 'mlp':
        policy = MlpPolicy
    elif policy == 'mlp_int':
        policy = MlpPolicyIntrinsicReward
    else:
        raise NotImplementedError
    ppo2.learn(policy=policy, env=env, nsteps=2048, nminibatches=32,
        lam=0.95, gamma=0.99, noptepochs=10, log_interval=1,
        ent_coef=0.0,
        lr_alpha=lr_alpha,
        cliprange=0.2,
        total_timesteps=num_timesteps,
        r_ex_coef=r_ex_coef,
        r_in_coef=r_in_coef,
        lr_beta=lr_beta,
        reward_freq=reward_freq) 
Example #30
Source File: build.py    From Traffic_sign_detection_YOLO with MIT License 5 votes vote down vote up
def setup_meta_ops(self):
		cfg = dict({
			'allow_soft_placement': False,
			'log_device_placement': False
		})

		utility = min(self.FLAGS.gpu, 1.)
		if utility > 0.0:
			self.say('GPU mode with {} usage'.format(utility))
			cfg['gpu_options'] = tf.GPUOptions(
				per_process_gpu_memory_fraction = utility)
			cfg['allow_soft_placement'] = True
		else: 
			self.say('Running entirely on CPU')
			cfg['device_count'] = {'GPU': 0}

		if self.FLAGS.train: self.build_train_op()
		
		if self.FLAGS.summary:
			self.summary_op = tf.summary.merge_all()
			self.writer = tf.summary.FileWriter(self.FLAGS.summary + 'train')
		
		self.sess = tf.Session(config = tf.ConfigProto(**cfg))
		self.sess.run(tf.global_variables_initializer())

		if not self.ntrain: return
		self.saver = tf.train.Saver(tf.global_variables(), 
			max_to_keep = self.FLAGS.keep)
		if self.FLAGS.load != 0: self.load_from_ckpt()
		
		if self.FLAGS.summary:
			self.writer.add_graph(self.sess.graph)