Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init(self):
sess = tf.InteractiveSession()
tf.Variable(0, name='global_step', trainable=False)
agent = NFSPAgent(sess=sess,
scope='nfsp',
action_num=10,
state_shape=[10],
hidden_layers_sizes=[10,10],
q_mlp_layers=[10,10])
self.assertEqual(agent._action_num, 10)
sess.close()
tf.reset_default_graph()
def test_evaluate_with(self):
# Test average policy and value error here
sess = tf.InteractiveSession()
tf.Variable(0, name='global_step', trainable=False)
agent = NFSPAgent(sess=sess,
scope='nfsp',
action_num=2,
state_shape=[2],
hidden_layers_sizes=[10,10],
q_mlp_layers=[10,10],
evaluate_with='average_policy')
sess.run(tf.global_variables_initializer())
predicted_action = agent.eval_step({'obs': np.random.random_sample((2,)), 'legal_actions': [0, 1]})
self.assertGreaterEqual(predicted_action, 0)
self.assertLessEqual(predicted_action, 1)
sess.close()
tf.reset_default_graph()
sess = tf.InteractiveSession()
tf.Variable(0, name='global_step', trainable=False)
def test_train(self):
norm_step = 100
memory_init_size = 20
step_num = 1000
sess = tf.InteractiveSession()
tf.Variable(0, name='global_step', trainable=False)
agent = NFSPAgent(sess=sess,
scope='nfsp',
action_num=2,
state_shape=[2],
hidden_layers_sizes=[10,10],
reservoir_buffer_capacity=50,
batch_size=4,
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_size=50,
q_replay_memory_init_size=memory_init_size,
q_batch_size=4,
q_norm_step=norm_step,
q_mlp_layers=[10,10])
sess.run(tf.global_variables_initializer())
predicted_action = agent.eval_step({'obs': np.random.random_sample((2,)), 'legal_actions': [0, 1]})
self.assertGreaterEqual(predicted_action, 0)
def __init__(self):
''' Load pretrained model
'''
self.graph = tf.Graph()
self.sess = tf.Session(graph=self.graph)
env = rlcard.make('leduc-holdem')
with self.graph.as_default():
self.agents = []
for i in range(env.player_num):
agent = NFSPAgent(self.sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=env.state_shape,
hidden_layers_sizes=[128,128],
q_norm_step=1000,
q_mlp_layers=[128,128])
self.agents.append(agent)
normalize(env, self.agents, 1000)
self.sess.run(tf.global_variables_initializer())
check_point_path = os.path.join(ROOT_PATH, 'leduc_holdem_nfsp')
with self.sess.as_default():
with self.graph.as_default():
saver = tf.train.Saver(tf.model_variables())
saver.restore(self.sess, tf.train.latest_checkpoint(check_point_path))
# The paths for saving the logs and learning curves
root_path = './experiments/uno_nfsp_result/'
log_path = root_path + 'log.txt'
csv_path = root_path + 'performance.csv'
figure_path = root_path + 'figures/'
# Set a global seed
set_global_seed(0)
with tf.Session() as sess:
# Set agents
global_step = tf.Variable(0, name='global_step', trainable=False)
agents = []
for i in range(env.player_num):
agent = NFSPAgent(sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=env.state_shape,
hidden_layers_sizes=[512,1024,2048,1024,512],
anticipatory_param=0.5,
batch_size=256,
rl_learning_rate=0.00005,
sl_learning_rate=0.00001,
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_size=int(1e5),
q_replay_memory_init_size=memory_init_size,
q_norm_step=norm_step,
q_batch_size=256,
q_mlp_layers=[512,1024,2048,1024,512])
agents.append(agent)
episode_num = 10000000
# Set the the number of steps for collecting normalization statistics
# and intial memory size
memory_init_size = 1000
norm_step = 1000
# Set a global seed
set_global_seed(0)
with tf.Session() as sess:
# Set agents
global_step = tf.Variable(0, name='global_step', trainable=False)
agents = []
for i in range(env.player_num):
agent = NFSPAgent(sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=[52],
hidden_layers_sizes=[512,512],
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_init_size=memory_init_size,
q_norm_step=norm_step,
q_mlp_layers=[512,512])
agents.append(agent)
sess.run(tf.global_variables_initializer())
random_agent = RandomAgent(action_num=eval_env.action_num)
env.set_agents(agents)
eval_env.set_agents([agents[0], random_agent])
# The paths for saving the logs and learning curves
root_path = './experiments/limit_holdem_nfsp_result/'
log_path = root_path + 'log.txt'
csv_path = root_path + 'performance.csv'
figure_path = root_path + 'figures/'
# Set a global seed
set_global_seed(0)
with tf.Session() as sess:
# Set agents
global_step = tf.Variable(0, name='global_step', trainable=False)
agents = []
for i in range(env.player_num):
agent = NFSPAgent(sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=env.state_shape,
hidden_layers_sizes=[512,512],
anticipatory_param=0.1,
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_init_size=memory_init_size,
q_norm_step=norm_step,
q_mlp_layers=[512,512])
agents.append(agent)
sess.run(tf.global_variables_initializer())
random_agent = RandomAgent(action_num=eval_env.action_num)
env.set_agents(agents)
episode_num = 10000000
# Set the the number of steps for collecting normalization statistics
# and intial memory size
memory_init_size = 1000
norm_step = 1000
# Set a global seed
set_global_seed(0)
with tf.Session() as sess:
# Set agents
global_step = tf.Variable(0, name='global_step', trainable=False)
agents = []
for i in range(env.player_num):
agent = NFSPAgent(sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=[6, 5, 15],
hidden_layers_sizes=[512,1024,2048,1024,512],
anticipatory_param=0.5,
batch_size=256,
rl_learning_rate=0.00005,
sl_learning_rate=0.00001,
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_size=int(1e5),
q_replay_memory_init_size=memory_init_size,
q_norm_step=norm_step,
q_batch_size=256,
q_mlp_layers=[512,1024,2048,1024,512])
agents.append(agent)
# The paths for saving the logs and learning curves
root_path = './experiments/mahjong_nfsp_result/'
log_path = root_path + 'log.txt'
csv_path = root_path + 'performance.csv'
figure_path = root_path + 'figures/'
# Set a global seed
set_global_seed(0)
with tf.Session() as sess:
# Set agents
global_step = tf.Variable(0, name='global_step', trainable=False)
agents = []
for i in range(env.player_num):
agent = NFSPAgent(sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=env.state_shape,
hidden_layers_sizes=[512,1024,2048,1024,512],
anticipatory_param=0.5,
batch_size=256,
rl_learning_rate=0.00005,
sl_learning_rate=0.00001,
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_size=int(1e5),
q_replay_memory_init_size=memory_init_size,
q_norm_step=norm_step,
q_batch_size=256,
q_mlp_layers=[512,1024,2048,1024,512])
agents.append(agent)
episode_num = 10000000
# Set the the number of steps for collecting normalization statistics
# and intial memory size
memory_init_size = 1000
norm_step = 1000
# Set a global seed
set_global_seed(0)
with tf.Session() as sess:
# Set agents
global_step = tf.Variable(0, name='global_step', trainable=False)
agents = []
for i in range(env.player_num):
agent = NFSPAgent(sess,
scope='nfsp' + str(i),
action_num=env.action_num,
state_shape=[6],
hidden_layers_sizes=[128,128],
min_buffer_size_to_learn=memory_init_size,
q_replay_memory_init_size=memory_init_size,
q_norm_step=norm_step,
q_mlp_layers=[128,128])
agents.append(agent)
sess.run(tf.global_variables_initializer())
random_agent = RandomAgent(action_num=eval_env.action_num)
env.set_agents(agents)
eval_env.set_agents([agents[0], random_agent])