def generator(input_latent):
# input_latent = Input(batch_shape=noise_dim, dtype=im_dtype)
with tf.variable_scope('Net_Gen') as scope:
weight_decay = 0.0001
xx = layers.fully_connected(input_latent, num_outputs=4*4*512, activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = tf.reshape(xx, (batch_size, 4,4,512))
xx = layers.conv2d_transpose(xx, 256, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.conv2d_transpose(xx, 128, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.conv2d_transpose(xx, 3, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
gen_dat = tf.nn.tanh(xx)
return gen_dat
# specify discriminative model
python类fully_connected()的实例源码
train_cifar_feature_matching_tf.py 文件源码
项目:Semi_Supervised_GAN
作者: ChunyuanLI
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
train_cifar_feature_matching_ali_tf.py 文件源码
项目:Semi_Supervised_GAN
作者: ChunyuanLI
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def generator(input_latent):
# input_latent = Input(batch_shape=noise_dim, dtype=im_dtype)
with tf.variable_scope('Net_Gen') as scope:
xx = layers.fully_connected(input_latent, num_outputs=4*4*512, activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = tf.reshape(xx, (batch_size, 4,4,512))
xx = layers.conv2d_transpose(xx, 256, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.conv2d_transpose(xx, 128, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.conv2d_transpose(xx, 3, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
gen_dat = tf.nn.tanh(xx)
return gen_dat
train_cifar_feature_matching_ali_tf.py 文件源码
项目:Semi_Supervised_GAN
作者: ChunyuanLI
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def inference(input_img):
# input_latent = Input(batch_shape=noise_dim, dtype=im_dtype)
with tf.variable_scope('Net_Inf') as scope:
xx = layers.convolution2d(input_img, 128, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.convolution2d(xx, 256, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.convolution2d(xx, 512, kernel_size=(5,5), stride=(2, 2), padding='SAME', activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.flatten(xx)
xx = layers.fully_connected(xx, num_outputs=latent_size, activation_fn=None)
xx = layers.batch_norm(xx)
inf_latent = tf.nn.tanh(xx)
return inf_latent
# specify discriminative model
train_mnist_feature_matching_ali_tf.py 文件源码
项目:Semi_Supervised_GAN
作者: ChunyuanLI
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def generator(input_latent):
# input_latent = Input(batch_shape=noise_dim, dtype=im_dtype)
with tf.variable_scope('Net_Gen') as scope:
xx = layers.fully_connected(input_latent, num_outputs=500, activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.fully_connected(xx, num_outputs=500, activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.fully_connected(xx, num_outputs=28**2, activation_fn=None)
xx = layers.batch_norm(xx)
gen_dat = tf.nn.sigmoid(xx)
return gen_dat
# specify inference model
train_mnist_feature_matching_tf.py 文件源码
项目:Semi_Supervised_GAN
作者: ChunyuanLI
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def generator(input_latent):
# input_latent = Input(batch_shape=noise_dim, dtype=im_dtype)
with tf.variable_scope('Net_Gen') as scope:
xx = layers.fully_connected(input_latent, num_outputs=500, activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.fully_connected(xx, num_outputs=500, activation_fn=None)
xx = layers.batch_norm(xx)
xx = tf.nn.relu(xx)
xx = layers.fully_connected(xx, num_outputs=28**2, activation_fn=None)
xx = layers.batch_norm(xx)
gen_dat = tf.nn.sigmoid(xx)
return gen_dat
# specify discriminative model
components.py 文件源码
项目:decorrelated-adversarial-autoencoder
作者: patrickgadd
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def semi_supervised_encoder_convolutional(input_tensor, z_dim, y_dim, batch_size, network_scale=1.0, img_res=28, img_channels=1):
f_multiplier = network_scale
net = tf.reshape(input_tensor, [-1, img_res, img_res, img_channels])
net = layers.conv2d(net, int(16*f_multiplier), 3, stride=2)
net = layers.conv2d(net, int(16*f_multiplier), 3, stride=1)
net = layers.conv2d(net, int(32*f_multiplier), 3, stride=2)
net = layers.conv2d(net, int(32*f_multiplier), 3, stride=1)
net = layers.conv2d(net, int(64*f_multiplier), 3, stride=2)
net = layers.conv2d(net, int(64*f_multiplier), 3, stride=1)
net = layers.conv2d(net, int(128*f_multiplier), 3, stride=2)
net = tf.reshape(net, [batch_size, -1])
net = layers.fully_connected(net, 1000)
y = layers.fully_connected(net, y_dim, activation_fn=None, normalizer_fn=None)
z = layers.fully_connected(net, z_dim, activation_fn=None)
return y, z
def model(img_in, num_actions, scope, reuse=False):
"""As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("action_value"):
out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_actions, activation_fn=None)
return out
def dueling_model(img_in, num_actions, scope, reuse=False):
"""As described in https://arxiv.org/abs/1511.06581"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("state_value"):
state_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
state_score = layers.fully_connected(state_hidden, num_outputs=1, activation_fn=None)
with tf.variable_scope("action_value"):
actions_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
action_scores = layers.fully_connected(actions_hidden, num_outputs=num_actions, activation_fn=None)
action_scores_mean = tf.reduce_mean(action_scores, 1)
action_scores = action_scores - tf.expand_dims(action_scores_mean, 1)
return state_score + action_scores
def dueling_model(img_in, num_actions, scope, reuse=False):
"""As described in https://arxiv.org/abs/1511.06581"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("state_value"):
state_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
state_score = layers.fully_connected(state_hidden, num_outputs=1, activation_fn=None)
with tf.variable_scope("action_value"):
actions_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
action_scores = layers.fully_connected(actions_hidden, num_outputs=num_actions, activation_fn=None)
action_scores_mean = tf.reduce_mean(action_scores, 1)
action_scores = action_scores - tf.expand_dims(action_scores_mean, 1)
return state_score + action_scores
def _action_norm_dist(inpt, num_actions, w_init, activation_fn_v, activation_fn_a):
mu = layers.fully_connected(inpt, num_outputs=num_actions, weights_initializer=w_init, activation_fn=activation_fn_v)
sigma = layers.fully_connected(inpt, num_outputs=num_actions, weights_initializer=w_init, activation_fn=activation_fn_a)
return mu, sigma
# # cnn network frame
# def cnn_frame_continu(hiddens, kerners, strides, inpt, num_actions, scope=None, activation_fn=tf.nn.relu, activation_fn_mu=tf.nn.relu, activation_fn_sigma=tf.nn.relu, reuse=None):
# with tf.variable_scope(scope, reuse=reuse):
# out = inpt
# for kerner, stride in kerners, strides:
# out = tf.nn.conv2d(input=out, filter=kerner, stride=stride)
# out = layers.flatten(out)
# with tf.name_scope("out"):
# mu = layers.fully_connected(out, num_outputs=num_actions, weights_initializer=tf.truncated_normal_initializer(0 , 0.3), activation_fn=None)
# sigma = layers.fully_connected(out, num_outputs=num_actions, weights_initializer=tf.truncated_normal_initializer(0 , 0.3), activation_fn=tf.nn.softplus)
# return mu, sigma
def model(img_in, num_actions, scope, reuse=False, concat_softmax=False):
"""As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("action_value"):
out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_actions, activation_fn=None)
if concat_softmax:
out = tf.nn.softmax(out)
return out
def dueling_model(img_in, num_actions, scope, reuse=False):
"""As described in https://arxiv.org/abs/1511.06581"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("state_value"):
state_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
state_score = layers.fully_connected(state_hidden, num_outputs=1, activation_fn=None)
with tf.variable_scope("action_value"):
actions_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
action_scores = layers.fully_connected(actions_hidden, num_outputs=num_actions, activation_fn=None)
action_scores_mean = tf.reduce_mean(action_scores, 1)
action_scores = action_scores - tf.expand_dims(action_scores_mean, 1)
return state_score + action_scores
def model(img_in, num_actions, scope, reuse=False, layer_norm=False):
"""As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
conv_out = layers.flatten(out)
with tf.variable_scope("action_value"):
value_out = layers.fully_connected(conv_out, num_outputs=512, activation_fn=None)
if layer_norm:
value_out = layer_norm_fn(value_out, relu=True)
else:
value_out = tf.nn.relu(value_out)
value_out = layers.fully_connected(value_out, num_outputs=num_actions, activation_fn=None)
return value_out
def _build_net(self, input_BO, scope):
""" The Actor network.
Uses ReLUs for all hidden layers, but a tanh to the output to bound the
action. This follows their 'low-dimensional networks' using 400 and 300
units for the hidden layers. Set `reuse=False`. I don't use batch
normalization or their precise weight initialization.
"""
with tf.variable_scope(scope, reuse=False):
hidden1 = layers.fully_connected(input_BO,
num_outputs=400,
weights_initializer=layers.xavier_initializer(),
activation_fn=tf.nn.relu)
hidden2 = layers.fully_connected(hidden1,
num_outputs=300,
weights_initializer=layers.xavier_initializer(),
activation_fn=tf.nn.relu)
actions_BA = layers.fully_connected(hidden2,
num_outputs=self.ac_dim,
weights_initializer=layers.xavier_initializer(),
activation_fn=tf.nn.tanh) # Note the tanh!
# This should broadcast, but haven't tested with ac_dim > 1.
actions_BA = tf.multiply(actions_BA, self.ac_high)
return actions_BA
def _make_network(self, data_in, out_dim):
""" Build the network with the same architecture following OpenAI's paper.
Returns the final *layer* of the network, which corresponds to our
chosen action. There is no non-linearity for the last layer because
different envs have different action ranges.
"""
with tf.variable_scope("ESAgent", reuse=False):
out = data_in
out = layers.fully_connected(out, num_outputs=64,
weights_initializer = layers.xavier_initializer(uniform=True),
#weights_initializer = utils.normc_initializer(0.5),
activation_fn = tf.nn.tanh)
out = layers.fully_connected(out, num_outputs=64,
weights_initializer = layers.xavier_initializer(uniform=True),
#weights_initializer = utils.normc_initializer(0.5),
activation_fn = tf.nn.tanh)
out = layers.fully_connected(out, num_outputs=out_dim,
weights_initializer = layers.xavier_initializer(uniform=True),
#weights_initializer = utils.normc_initializer(0.5),
activation_fn = None)
return out
def var_dropout(observed, x, n, net_size, n_particles, is_training):
with zs.BayesianNet(observed=observed) as model:
h = x
normalizer_params = {'is_training': is_training,
'updates_collections': None}
for i, [n_in, n_out] in enumerate(zip(net_size[:-1], net_size[1:])):
eps_mean = tf.ones([n, n_in])
eps = zs.Normal(
'layer' + str(i) + '/eps', eps_mean, std=1.,
n_samples=n_particles, group_ndims=1)
h = layers.fully_connected(
h * eps, n_out, normalizer_fn=layers.batch_norm,
normalizer_params=normalizer_params)
if i < len(net_size) - 2:
h = tf.nn.relu(h)
y = zs.OnehotCategorical('y', h)
return model, h
def q_net(x, n_xl, n_z, n_particles, is_training):
with zs.BayesianNet() as variational:
normalizer_params = {'is_training': is_training,
'updates_collections': None}
lz_x = tf.reshape(tf.to_float(x), [-1, n_xl, n_xl, 1])
lz_x = layers.conv2d(
lz_x, 32, kernel_size=5, stride=2,
normalizer_fn=layers.batch_norm,
normalizer_params=normalizer_params)
lz_x = layers.conv2d(
lz_x, 64, kernel_size=5, stride=2,
normalizer_fn=layers.batch_norm,
normalizer_params=normalizer_params)
lz_x = layers.conv2d(
lz_x, 128, kernel_size=5, padding='VALID',
normalizer_fn=layers.batch_norm,
normalizer_params=normalizer_params)
lz_x = layers.dropout(lz_x, keep_prob=0.9, is_training=is_training)
lz_x = tf.reshape(lz_x, [-1, 128 * 3 * 3])
lz_mean = layers.fully_connected(lz_x, n_z, activation_fn=None)
lz_logstd = layers.fully_connected(lz_x, n_z, activation_fn=None)
z = zs.Normal('z', lz_mean, logstd=lz_logstd, n_samples=n_particles,
group_ndims=1)
return variational
doc2vec_train_doc_prediction.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def doc2vec_prediction_model(input_vectors, input_gene, input_variation, output_label, batch_size,
is_training, embedding_size, output_classes):
# inputs/outputs
input_vectors = tf.reshape(input_vectors, [batch_size, embedding_size])
input_gene = tf.reshape(input_gene, [batch_size, embedding_size])
input_variation = tf.reshape(input_variation, [batch_size, embedding_size])
targets = None
if output_label is not None:
output_label = tf.reshape(output_label, [batch_size, 1])
targets = tf.one_hot(output_label, axis=-1, depth=output_classes, on_value=1.0,
off_value=0.0)
targets = tf.squeeze(targets, axis=1)
net = tf.concat([input_vectors, input_gene, input_variation], axis=1)
net = layers.fully_connected(net, embedding_size * 2, activation_fn=tf.nn.relu)
net = layers.dropout(net, keep_prob=0.85, is_training=is_training)
net = layers.fully_connected(net, embedding_size, activation_fn=tf.nn.relu)
net = layers.dropout(net, keep_prob=0.85, is_training=is_training)
net = layers.fully_connected(net, embedding_size // 4, activation_fn=tf.nn.relu)
logits = layers.fully_connected(net, output_classes, activation_fn=None)
return logits, targets
text_classification_model_han.py 文件源码
项目:kaggle_redefining_cancer_treatment
作者: jorgemf
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def _attention(self, inputs, output_size, gene, variation, activation_fn=tf.tanh):
inputs_shape = inputs.get_shape()
if len(inputs_shape) != 3 and len(inputs_shape) != 4:
raise ValueError('Shape of input must have 3 or 4 dimensions')
input_projection = layers.fully_connected(inputs, output_size,
activation_fn=activation_fn)
doc_context = tf.concat([gene, variation], axis=1)
doc_context_vector = layers.fully_connected(doc_context, output_size,
activation_fn=activation_fn)
doc_context_vector = tf.expand_dims(doc_context_vector, 1)
if len(inputs_shape) == 4:
doc_context_vector = tf.expand_dims(doc_context_vector, 1)
vector_attn = input_projection * doc_context_vector
vector_attn = tf.reduce_sum(vector_attn, axis=-1, keep_dims=True)
attention_weights = tf.nn.softmax(vector_attn, dim=1)
weighted_projection = input_projection * attention_weights
outputs = tf.reduce_sum(weighted_projection, axis=-2)
return outputs
def get_arg_scope(is_training):
weight_decay_l2 = 0.1
batch_norm_decay = 0.999
batch_norm_epsilon = 0.0001
with slim.arg_scope([slim.conv2d, slim.fully_connected, layers.separable_convolution2d],
weights_regularizer = slim.l2_regularizer(weight_decay_l2),
biases_regularizer = slim.l2_regularizer(weight_decay_l2),
weights_initializer = layers.variance_scaling_initializer(),
):
batch_norm_params = {
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon
}
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training = is_training):
with slim.arg_scope([slim.batch_norm],
**batch_norm_params):
with slim.arg_scope([slim.conv2d, layers.separable_convolution2d, layers.fully_connected],
activation_fn = tf.nn.elu,
normalizer_fn = slim.batch_norm,
normalizer_params = batch_norm_params) as scope:
return scope
def test_dqn(env='Chain-v0'):
import gym_mix
env = gym.make(env)
def pp(x):
x = layers.fully_connected(x, 32)
x = layers.fully_connected(x, 32)
return x
def head(x):
x = layers.fully_connected(x, 32)
x = layers.fully_connected(x, env.action_space.n, activation_fn=None,
weights_initializer=tf.random_normal_initializer(0, 1e-4))
return x
agent = BootstrappedDQNAg(env, pp, head, replay_start=64)
for ep in range(100000):
R, _ = agent.play_episode()
if ep % 100 == 0:
print(f'Return after episode {ep} is {R}')
def deep_q_network():
""" Architecture according to:
http://www.nature.com/nature/journal/v518/n7540/full/nature14236.html
"""
@tt.model(tracker=tf.train.ExponentialMovingAverage(1 - .0005), # TODO: replace with original weight freeze
optimizer=tf.train.RMSPropOptimizer(.00025, .95, .95, .01))
def q_network(x):
x /= 255
x = layers.conv2d(x, 32, 8, 4)
x = layers.conv2d(x, 64, 4, 2)
x = layers.conv2d(x, 64, 3, 1)
x = layers.flatten(x)
x = layers.fully_connected(x, 512)
x = layers.fully_connected(x, env.action_space.n, activation_fn=None)
x = tf.identity(x, name='Q')
return x
return q_network
def dqn_test(env='OneRoundDeterministicReward-v0'):
env = gym.make(env)
env = ObservationShapeWrapper(env)
@tt.model(tracker=tf.train.ExponentialMovingAverage(1-.01),
optimizer=tf.train.AdamOptimizer(.01))
def q_network(x):
x = layers.fully_connected(x, 32)
x = layers.fully_connected(x, env.action_space.n, activation_fn=None,
weights_initializer=tf.random_normal_initializer(0, 1e-4))
return x
agent = DqnAgent(env, q_network, double_dqn=False, replay_start=100, annealing_time=100)
rs = []
for ep in range(10000):
r, _ = agent.play_episode()
rs.append(r)
if ep % 100 == 0:
print(f'Return after episode {ep} is {sum(rs)/len(rs)}')
rs = []
def model(img_in, num_actions, scope, reuse=False):
"""As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("action_value"):
out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_actions, activation_fn=None)
return out
def dueling_model(img_in, num_actions, scope, reuse=False):
"""As described in https://arxiv.org/abs/1511.06581"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("state_value"):
state_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
state_score = layers.fully_connected(state_hidden, num_outputs=1, activation_fn=None)
with tf.variable_scope("action_value"):
actions_hidden = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
action_scores = layers.fully_connected(actions_hidden, num_outputs=num_actions, activation_fn=None)
action_scores_mean = tf.reduce_mean(action_scores, 1)
action_scores = action_scores - tf.expand_dims(action_scores_mean, 1)
return state_score + action_scores
def Actor(img_in, num_actions, scope, reuse=False):
"""As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("action_value"):
out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_actions, activation_fn=None)
with tf.variable_scope("action_prob"):
out = tf.nn.softmax(out)
return out
def _build_net(self):
with tf.variable_scope("conv"):
out = layers.convolution2d(self.s, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("actor"):
a_prob = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
a_prob = layers.fully_connected(a_prob, num_outputs=N_A, activation_fn=tf.nn.softmax)
with tf.variable_scope("critic"):
v = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
v = layers.fully_connected(v, num_outputs=1, activation_fn=tf.nn.softmax)
return a_prob, v
def to_loc(input, is_simple=False):
if len(input.get_shape()) == 4:
input = layers.flatten(input)
num_inputs = input.get_shape()[1]
num_outputs = 3 if is_simple else 6
W_init = tf.constant_initializer(
np.zeros((num_inputs, num_outputs)))
if is_simple:
b_init = tf.constant_initializer(np.array([1.,0.,0.]))
else:
b_init = tf.constant_initializer(np.array([1.,0.,0.,0.,1.,0.]))
return layers.fully_connected(input, num_outputs,
activation_fn=None,
weights_initializer=W_init,
biases_initializer=b_init)
def to_loc(input, is_simple=False):
if len(input.get_shape()) == 4:
input = layers.flatten(input)
num_inputs = input.get_shape()[1]
num_outputs = 3 if is_simple else 6
W_init = tf.constant_initializer(
np.zeros((num_inputs, num_outputs)))
if is_simple:
b_init = tf.constant_initializer(np.array([1.,0.,0.]))
else:
b_init = tf.constant_initializer(np.array([1.,0.,0.,0.,1.,0.]))
return layers.fully_connected(input, num_outputs,
activation_fn=None,
weights_initializer=W_init,
biases_initializer=b_init)
def fc(layer, output_size, is_training,
weight_init, weight_reg=None, activation_fn=None,
use_batch_norm=False, scope='fc'):
if use_batch_norm:
batch_norm_args = {
'normalizer_fn': batch_norm,
'normalizer_params': {
'is_training': is_training,
}
}
else:
batch_norm_args = {}
with tf.variable_scope(scope):
return fully_connected(
layer,
num_outputs=output_size,
activation_fn=activation_fn,
weights_initializer=weight_init,
weights_regularizer=weight_reg,
biases_initializer=tf.constant_initializer(0.0),
scope=scope,
**batch_norm_args
)