def _build_target(self):
activation_fn = tf.nn.relu
with tf.variable_scope('target'):
self.t_s_t = tf.placeholder('float32', [None, self.state_size], name='t_s_t')
# MLP Feature Extraction
l1, self.w['target']['l1_w'], self.w['target']['l1_b'] = linear(self.t_s_t, 96, activation_fn=activation_fn, name='l1')
#l2, self.w['target']['l2_w'], self.w['target']['l2_b'] = linear(l1, 16, activation_fn=activation_fn, name='l2')
#l3, self.w['target']['l3_w'], self.w['target']['l3_b'] = linear(l2, 16, activation_fn=activation_fn, name='l3')
l3 = l1
if self.dueling:
# Value Net : V(s) is scalar
value_hid, self.w['target']['l4_val_w'], self.w['target']['l4_val_b'] = linear(l3, 32, activation_fn=activation_fn, name='value_hid')
value, self.w['target']['val_w_out'], self.w['target']['val_w_b'] = linear(value_hid, 1, name='value_out')
# Advantage Net : A(s) is vector with advantage given each action
adv_hid, self.w['target']['l4_adv_w'], self.w['target']['l4_adv_b'] = linear(l3, 32, activation_fn=activation_fn, name='adv_hid')
advantage, self.w['target']['adv_w_out'], self.w['target']['adv_w_b'] = linear(adv_hid, self.action_size, name='adv_out')
# Average Dueling (Subtract mean advantage)
q_target = value + (advantage - tf.reduce_mean(advantage, reduction_indices=1, keep_dims=True))
else:
l4, self.w['target']['l4_w'], self.w['target']['l4_b'] = linear(l3, 16, activation_fn=activation_fn, name='l4')
q_target, self.w['target']['q_w'], self.w['target']['q_b'] = linear(l4, self.action_size, name='q')
# The action we use will depend if we use double q learning
target_q_idx = tf.placeholder('int32', [None, None], name='q_id')
# Get the q values of the specified state/action indices
target_q_with_idx = tf.gather_nd(q_target, target_q_idx)
return q_target, target_q_idx, target_q_with_idx
python类linear()的实例源码
def cnn(self, state, input_dims, num_actions):
w = {}
initializer = tf.truncated_normal_initializer(0, 0.02)
activation_fn = tf.nn.relu
state = tf.transpose(state, perm=[0, 2, 3, 1])
l1, w['l1_w'], w['l1_b'] = conv2d(state,
32, [8, 8], [4, 4], initializer, activation_fn, 'NHWC', name='l1')
l2, w['l2_w'], w['l2_b'] = conv2d(l1,
64, [4, 4], [2, 2], initializer, activation_fn, 'NHWC', name='l2')
shape = l2.get_shape().as_list()
l2_flat = tf.reshape(l2, [-1, reduce(lambda x, y: x * y, shape[1:])])
l3, w['l3_w'], w['l3_b'] = linear(l2_flat, 256, activation_fn=activation_fn, name='value_hid')
value, w['val_w_out'], w['val_w_b'] = linear(l3, 1, name='value_out')
V = tf.reshape(value, [-1])
pi_, w['pi_w_out'], w['pi_w_b'] = \
linear(l3, num_actions, activation_fn=tf.nn.softmax, name='pi_out')
sums = tf.tile(tf.expand_dims(tf.reduce_sum(pi_, 1), 1), [1, num_actions])
pi = pi_ / sums
#A3C is l1 = (16, [8,8], [4,4], ReLu), l2 = (32, [4,4], [2,2], ReLu), l3 = (256, Conn, ReLu), V = (1, Conn, Lin), pi = (#act, Conn, Softmax)
return pi, V, [ v for v in w.values() ]
# Adapted from github.com/devsisters/DQN-tensorflow/
def GeneratorCNN( z, config, reuse=None):
'''
maps z to a 64x64 images with values in [-1,1]
uses batch normalization internally
'''
#trying to get around batch_size like this:
batch_size=tf.shape(z)[0]
#batch_size=tf.placeholder_with_default(64,[],'bs')
with tf.variable_scope("generator",reuse=reuse) as vs:
g_bn0 = batch_norm(name='g_bn0')
g_bn1 = batch_norm(name='g_bn1')
g_bn2 = batch_norm(name='g_bn2')
g_bn3 = batch_norm(name='g_bn3')
s_h, s_w = config.gf_dim, config.gf_dim#64,64
s_h2, s_w2 = conv_out_size_same(s_h, 2), conv_out_size_same(s_w, 2)
s_h4, s_w4 = conv_out_size_same(s_h2, 2), conv_out_size_same(s_w2, 2)
s_h8, s_w8 = conv_out_size_same(s_h4, 2), conv_out_size_same(s_w4, 2)
s_h16, s_w16 = conv_out_size_same(s_h8, 2), conv_out_size_same(s_w8, 2)
# project `z` and reshape
z_, self_h0_w, self_h0_b = linear(
z, config.gf_dim*8*s_h16*s_w16, 'g_h0_lin', with_w=True)
self_h0 = tf.reshape(
z_, [-1, s_h16, s_w16, config.gf_dim * 8])
h0 = tf.nn.relu(g_bn0(self_h0))
h1, h1_w, h1_b = deconv2d(
h0, [batch_size, s_h8, s_w8, config.gf_dim*4], name='g_h1', with_w=True)
h1 = tf.nn.relu(g_bn1(h1))
h2, h2_w, h2_b = deconv2d(
h1, [batch_size, s_h4, s_w4, config.gf_dim*2], name='g_h2', with_w=True)
h2 = tf.nn.relu(g_bn2(h2))
h3, h3_w, h3_b = deconv2d(
h2, [batch_size, s_h2, s_w2, config.gf_dim*1], name='g_h3', with_w=True)
h3 = tf.nn.relu(g_bn3(h3))
h4, h4_w, h4_b = deconv2d(
h3, [batch_size, s_h, s_w, config.c_dim], name='g_h4', with_w=True)
out=tf.nn.tanh(h4)
variables = tf.contrib.framework.get_variables(vs)
return out, variables
def DiscriminatorCNN(image, config, reuse=None):
'''
Discriminator for GAN model.
image : batch_size x 64x64x3 image
config : see causal_dcgan/config.py
reuse : pass True if not calling for first time
returns: probabilities(real)
: logits(real)
: first layer activation used to estimate z from
: variables list
'''
with tf.variable_scope("discriminator",reuse=reuse) as vs:
d_bn1 = batch_norm(name='d_bn1')
d_bn2 = batch_norm(name='d_bn2')
d_bn3 = batch_norm(name='d_bn3')
if not config.stab_proj:
h0 = lrelu(conv2d(image, config.df_dim, name='d_h0_conv'))#16,32,32,64
else:#method to restrict disc from winning
#I think this is equivalent to just not letting disc optimize first layer
#and also removing nonlinearity
#k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
#paper used 8x8 kernel, but I'm using 5x5 because it is more similar to my achitecture
#n_projs=config.df_dim#64 instead of 32 in paper
n_projs=config.n_stab_proj#64 instead of 32 in paper
print("WARNING:STAB_PROJ active, using ",n_projs," projections")
w_proj = tf.get_variable('w_proj', [5, 5, image.get_shape()[-1],n_projs],
initializer=tf.truncated_normal_initializer(stddev=0.02),trainable=False)
conv = tf.nn.conv2d(image, w_proj, strides=[1, 2, 2, 1], padding='SAME')
b_proj = tf.get_variable('b_proj', [n_projs],#does nothing
initializer=tf.constant_initializer(0.0),trainable=False)
h0=tf.nn.bias_add(conv,b_proj)
h1_ = lrelu(d_bn1(conv2d(h0, config.df_dim*2, name='d_h1_conv')))#16,16,16,128
h1 = add_minibatch_features(h1_, config.df_dim)
h2 = lrelu(d_bn2(conv2d(h1, config.df_dim*4, name='d_h2_conv')))#16,16,16,248
h3 = lrelu(d_bn3(conv2d(h2, config.df_dim*8, name='d_h3_conv')))
#print('h3shape: ',h3.get_shape().as_list())
#print('8df_dim:',config.df_dim*8)
#dim3=tf.reduce_prod(tf.shape(h3)[1:])
dim3=np.prod(h3.get_shape().as_list()[1:])
h3_flat=tf.reshape(h3, [-1,dim3])
h4 = linear(h3_flat, 1, 'd_h3_lin')
prob=tf.nn.sigmoid(h4)
variables = tf.contrib.framework.get_variables(vs,collection=tf.GraphKeys.TRAINABLE_VARIABLES)
return prob, h4, h1_, variables
def __call__(self, inputs, is_train=True, is_debug=False):
self.is_train = is_train
self.is_debug = is_debug
inputs = tf.convert_to_tensor(inputs) # Check if necessary
# Assert that input is in [-1, 1]
encoder_max_assert_op = tf.Assert(tf.less_equal(tf.reduce_max(inputs), 1.), [
inputs], summarize=0, name='assert/encoder_max')
encoder_min_assert_op = tf.Assert(tf.greater_equal(tf.reduce_max(inputs), -1.),
[inputs], summarize=0, name='assert/encoder_min')
tf.add_to_collection('Assert', encoder_max_assert_op)
tf.add_to_collection('Assert', encoder_min_assert_op)
assert(inputs.get_shape().as_list() == [self.batch_size] + self.configs.conv_info.input)
with tf.variable_scope(self.name) as scope:
print_message(scope.name)
with tf.variable_scope('conv1') as vscope:
outputs, self.net['w1'], self.net['b1'] = conv3d(
inputs, [self.batch_size] + self.configs.conv_info.l1, is_train=self.is_train,
k=self.configs.conv_info.k1, s=self.configs.conv_info.s1, with_w=True)
if is_debug:
print(vscope.name, outputs)
outputs = tf.layers.dropout(outputs, rate=self.configs.dropout, training=self.is_train, name='outputs')
assert(outputs.get_shape().as_list() == [self.batch_size] + self.configs.conv_info.l1)
self.net['conv1_outputs'] = outputs
with tf.variable_scope('conv2') as vscope:
outputs, self.net['w2'], self.net['b2'] = conv3d(
outputs, [self.batch_size] + self.configs.conv_info.l2, is_train=self.is_train,
k=self.configs.conv_info.k2, s=self.configs.conv_info.s2, with_w=True)
if is_debug:
print(vscope.name, outputs)
outputs = tf.layers.dropout(outputs, rate=self.configs.dropout, training=self.is_train, name='outputs')
assert(outputs.get_shape().as_list() == [self.batch_size] + self.configs.conv_info.l2)
self.net['conv2_outputs'] = outputs
with tf.variable_scope('conv3') as vscope:
outputs, self.net['w3'], self.net['b3'] = conv3d(
outputs, [self.batch_size] + self.configs.conv_info.l3, is_train=self.is_train,
k=self.configs.conv_info.k3, s=self.configs.conv_info.s3, with_w=True)
if is_debug:
print(vscope.name, outputs)
outputs = tf.layers.dropout(outputs, rate=self.configs.dropout, training=self.is_train, name='outputs')
assert(outputs.get_shape().as_list() == [self.batch_size] + self.configs.conv_info.l3)
self.net['conv3_outputs'] = outputs
with tf.variable_scope('fc') as vscope:
fc_dim = reduce(mul, self.configs.conv_info.l3, 1)
outputs = tf.reshape(outputs, [self.batch_size] + [fc_dim], name='reshape')
outputs = linear(outputs, self.latent_dimension)
outputs = tf.nn.relu(outputs)
if is_debug:
print(vscope.name, outputs)
self.net['fc_outputs'] = outputs
self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name)
return outputs
def generator(self, opts, noise, is_training, reuse=False):
"""Generator function, suitable for bigger simple pictures.
Args:
noise: [num_points, dim] array, where dim is dimensionality of the
latent noise space.
is_training: bool, defines whether to use batch_norm in the train
or test mode.
Returns:
[num_points, dim1, dim2, dim3] array, where the first coordinate
indexes the points, which all are of the shape (dim1, dim2, dim3).
"""
output_shape = self._data.data_shape # (dim1, dim2, dim3)
# Computing the number of noise vectors on-the-go
dim1 = tf.shape(noise)[0]
num_filters = opts['g_num_filters']
with tf.variable_scope("GENERATOR", reuse=reuse):
height = output_shape[0] / 16
width = output_shape[1] / 16
h0 = ops.linear(opts, noise, num_filters * height * width,
scope='h0_lin')
h0 = tf.reshape(h0, [-1, height, width, num_filters])
h0 = ops.batch_norm(opts, h0, is_training, reuse, scope='bn_layer1')
h0 = tf.nn.relu(h0)
_out_shape = [dim1, height * 2, width * 2, num_filters / 2]
# for 128 x 128 does 8 x 8 --> 16 x 16
h1 = ops.deconv2d(opts, h0, _out_shape, scope='h1_deconv')
h1 = ops.batch_norm(opts, h1, is_training, reuse, scope='bn_layer2')
h1 = tf.nn.relu(h1)
_out_shape = [dim1, height * 4, width * 4, num_filters / 4]
# for 128 x 128 does 16 x 16 --> 32 x 32
h2 = ops.deconv2d(opts, h1, _out_shape, scope='h2_deconv')
h2 = ops.batch_norm(opts, h2, is_training, reuse, scope='bn_layer3')
h2 = tf.nn.relu(h2)
_out_shape = [dim1, height * 8, width * 8, num_filters / 8]
# for 128 x 128 does 32 x 32 --> 64 x 64
h3 = ops.deconv2d(opts, h2, _out_shape, scope='h3_deconv')
h3 = ops.batch_norm(opts, h3, is_training, reuse, scope='bn_layer4')
h3 = tf.nn.relu(h3)
_out_shape = [dim1, height * 16, width * 16, num_filters / 16]
# for 128 x 128 does 64 x 64 --> 128 x 128
h4 = ops.deconv2d(opts, h3, _out_shape, scope='h4_deconv')
h4 = ops.batch_norm(opts, h4, is_training, reuse, scope='bn_layer5')
h4 = tf.nn.relu(h4)
_out_shape = [dim1] + list(output_shape)
# data_shape[0] x data_shape[1] x ? -> data_shape
h5 = ops.deconv2d(opts, h4, _out_shape,
d_h=1, d_w=1, scope='h5_deconv')
h5 = ops.batch_norm(opts, h5, is_training, reuse, scope='bn_layer6')
if opts['input_normalize_sym']:
return tf.nn.tanh(h5)
else:
return tf.nn.sigmoid(h5)
def dcgan_like_arch(self, opts, noise, is_training, reuse, keep_prob):
output_shape = self._data.data_shape
num_units = opts['g_num_filters']
batch_size = tf.shape(noise)[0]
num_layers = opts['g_num_layers']
if opts['g_arch'] == 'dcgan':
height = output_shape[0] / 2**num_layers
width = output_shape[1] / 2**num_layers
elif opts['g_arch'] == 'dcgan_mod':
height = output_shape[0] / 2**(num_layers-1)
width = output_shape[1] / 2**(num_layers-1)
else:
assert False
h0 = ops.linear(
opts, noise, num_units * height * width, scope='h0_lin')
h0 = tf.reshape(h0, [-1, height, width, num_units])
h0 = tf.nn.relu(h0)
layer_x = h0
for i in xrange(num_layers-1):
scale = 2**(i+1)
if opts['g_stride1_deconv']:
# Sylvain, I'm worried about this part!
_out_shape = [batch_size, height * scale / 2,
width * scale / 2, num_units / scale * 2]
layer_x = ops.deconv2d(
opts, layer_x, _out_shape, d_h=1, d_w=1,
scope='h%d_deconv_1x1' % i)
layer_x = tf.nn.relu(layer_x)
_out_shape = [batch_size, height * scale, width * scale, num_units / scale]
layer_x = ops.deconv2d(opts, layer_x, _out_shape, scope='h%d_deconv' % i)
if opts['batch_norm']:
layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='bn%d' % i)
layer_x = tf.nn.relu(layer_x)
if opts['dropout']:
_keep_prob = tf.minimum(
1., 0.9 - (0.9 - keep_prob) * float(i + 1) / (num_layers - 1))
layer_x = tf.nn.dropout(layer_x, _keep_prob)
_out_shape = [batch_size] + list(output_shape)
if opts['g_arch'] == 'dcgan':
last_h = ops.deconv2d(
opts, layer_x, _out_shape, scope='hlast_deconv')
elif opts['g_arch'] == 'dcgan_mod':
last_h = ops.deconv2d(
opts, layer_x, _out_shape, d_h=1, d_w=1, scope='hlast_deconv')
else:
assert False
if opts['input_normalize_sym']:
return tf.nn.tanh(last_h)
else:
return tf.nn.sigmoid(last_h)
def conv_up_res(self, opts, noise, is_training, reuse, keep_prob):
output_shape = self._data.data_shape
num_units = opts['g_num_filters']
batch_size = tf.shape(noise)[0]
num_layers = opts['g_num_layers']
data_height = output_shape[0]
data_width = output_shape[1]
data_channels = output_shape[2]
height = data_height / 2**num_layers
width = data_width / 2**num_layers
h0 = ops.linear(
opts, noise, num_units * height * width, scope='h0_lin')
h0 = tf.reshape(h0, [-1, height, width, num_units])
h0 = tf.nn.relu(h0)
layer_x = h0
for i in xrange(num_layers-1):
layer_x = tf.image.resize_nearest_neighbor(layer_x, (2 * height, 2 * width))
layer_x = ops.conv2d(opts, layer_x, num_units / 2, d_h=1, d_w=1, scope='conv2d_%d' % i)
height *= 2
width *= 2
num_units /= 2
if opts['g_3x3_conv'] > 0:
before = layer_x
for j in range(opts['g_3x3_conv']):
layer_x = ops.conv2d(opts, layer_x, num_units, d_h=1, d_w=1,
scope='conv2d_3x3_%d_%d' % (i, j),
conv_filters_dim=3)
layer_x = tf.nn.relu(layer_x)
layer_x += before # Residual connection.
if opts['batch_norm']:
layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='bn%d' % i)
layer_x = tf.nn.relu(layer_x)
if opts['dropout']:
_keep_prob = tf.minimum(
1., 0.9 - (0.9 - keep_prob) * float(i + 1) / (num_layers - 1))
layer_x = tf.nn.dropout(layer_x, _keep_prob)
layer_x = tf.image.resize_nearest_neighbor(layer_x, (2 * height, 2 * width))
layer_x = ops.conv2d(opts, layer_x, data_channels, d_h=1, d_w=1, scope='last_conv2d_%d' % i)
if opts['input_normalize_sym']:
return tf.nn.tanh(layer_x)
else:
return tf.nn.sigmoid(layer_x)
def ali_encoder(self, opts, input_, is_training=False, reuse=False, keep_prob=1.):
num_units = opts['e_num_filters']
layer_params = []
layer_params.append([5, 1, num_units / 8])
layer_params.append([4, 2, num_units / 4])
layer_params.append([4, 1, num_units / 2])
layer_params.append([4, 2, num_units])
layer_params.append([4, 1, num_units * 2])
# For convolution: (n - k) / stride + 1 = s
# For transposed: (s - 1) * stride + k = n
layer_x = input_
height = int(layer_x.get_shape()[1])
width = int(layer_x.get_shape()[2])
assert height == width
for i, (kernel, stride, channels) in enumerate(layer_params):
height = (height - kernel) / stride + 1
width = height
# print((height, width))
layer_x = ops.conv2d(
opts, layer_x, channels, d_h=stride, d_w=stride,
scope='h%d_conv' % i, conv_filters_dim=kernel, padding='VALID')
if opts['batch_norm']:
layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='bn%d' % i)
layer_x = ops.lrelu(layer_x, 0.1)
assert height == 1
assert width == 1
# Then two 1x1 convolutions.
layer_x = ops.conv2d(opts, layer_x, num_units * 2, d_h=1, d_w=1, scope='conv2d_1x1', conv_filters_dim=1)
if opts['batch_norm']:
layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='bnlast')
layer_x = ops.lrelu(layer_x, 0.1)
layer_x = ops.conv2d(opts, layer_x, num_units / 2, d_h=1, d_w=1, scope='conv2d_1x1_2', conv_filters_dim=1)
if opts['e_is_random']:
latent_mean = ops.linear(
opts, layer_x, opts['latent_space_dim'], scope='hlast_lin')
log_latent_sigmas = ops.linear(
opts, layer_x, opts['latent_space_dim'], scope='hlast_lin_sigma')
return latent_mean, log_latent_sigmas
else:
return ops.linear(opts, layer_x, opts['latent_space_dim'], scope='hlast_lin')
def generator(self, z, y=None, is_train=True, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
s = self.output_size
if np.mod(s, 16) == 0:
s2, s4, s8, s16 = int(s/2), int(s/4), int(s/8), int(s/16)
# project `z` and reshape
self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*8*s16*s16, 'g_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_, [-1, s16, s16, self.gf_dim * 8])
h0 = tf.nn.relu(self.g_bn0(self.h0, train=is_train))
self.h1, self.h1_w, self.h1_b = deconv2d(h0,
[self.batch_size, s8, s8, self.gf_dim*4], name='g_h1', with_w=True)
h1 = tf.nn.relu(self.g_bn1(self.h1, train=is_train))
h2, self.h2_w, self.h2_b = deconv2d(h1,
[self.batch_size, s4, s4, self.gf_dim*2], name='g_h2', with_w=True)
h2 = tf.nn.relu(self.g_bn2(h2, train=is_train))
h3, self.h3_w, self.h3_b = deconv2d(h2,
[self.batch_size, s2, s2, self.gf_dim*1], name='g_h3', with_w=True)
h3 = tf.nn.relu(self.g_bn3(h3, train=is_train))
h4, self.h4_w, self.h4_b = deconv2d(h3,
[self.batch_size, s, s, self.c_dim], name='g_h4', with_w=True)
return tf.nn.tanh(h4)
else:
s = self.output_size
s2, s4 = int(s/2), int(s/4)
self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*2*s4*s4, 'g_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_, [-1, s4, s4, self.gf_dim * 2])
h0 = tf.nn.relu(self.g_bn0(self.h0, train=is_train))
self.h1, self.h1_w, self.h1_b = deconv2d(h0,
[self.batch_size, s2, s2, self.gf_dim*1], name='g_h1', with_w=True)
h1 = tf.nn.relu(self.g_bn1(self.h1, train=is_train))
h2, self.h2_w, self.h2_b = deconv2d(h1,
[self.batch_size, s, s, self.c_dim], name='g_h2', with_w=True)
return tf.nn.tanh(h2)
def generator(self, z, y=None, is_train=True, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
s = self.output_size
if np.mod(s, 16) == 0:
s2, s4, s8, s16 = int(s/2), int(s/4), int(s/8), int(s/16)
# project `z` and reshape
self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*8*s16*s16, 'g_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_, [-1, s16, s16, self.gf_dim * 8])
h0 = tf.nn.relu(self.g_bn0(self.h0, train=is_train))
self.h1, self.h1_w, self.h1_b = deconv2d(h0,
[self.batch_size, s8, s8, self.gf_dim*4], name='g_h1', with_w=True)
h1 = tf.nn.relu(self.g_bn1(self.h1, train=is_train))
h2, self.h2_w, self.h2_b = deconv2d(h1,
[self.batch_size, s4, s4, self.gf_dim*2], name='g_h2', with_w=True)
h2 = tf.nn.relu(self.g_bn2(h2, train=is_train))
h3, self.h3_w, self.h3_b = deconv2d(h2,
[self.batch_size, s2, s2, self.gf_dim*1], name='g_h3', with_w=True)
h3 = tf.nn.relu(self.g_bn3(h3, train=is_train))
h4, self.h4_w, self.h4_b = deconv2d(h3,
[self.batch_size, s, s, self.c_dim], name='g_h4', with_w=True)
return tf.nn.tanh(h4)
else:
s = self.output_size
s2, s4 = int(s/2), int(s/4)
self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*2*s4*s4, 'g_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_, [-1, s4, s4, self.gf_dim * 2])
h0 = tf.nn.relu(self.g_bn0(self.h0, train=is_train))
self.h1, self.h1_w, self.h1_b = deconv2d(h0,
[self.batch_size, s2, s2, self.gf_dim*1], name='g_h1', with_w=True)
h1 = tf.nn.relu(self.g_bn1(self.h1, train=is_train))
h2, self.h2_w, self.h2_b = deconv2d(h1,
[self.batch_size, s, s, self.c_dim], name='g_h2', with_w=True)
return tf.nn.tanh(h2)
def generator(self, z, y=None, is_train=True, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
s = self.output_size
if np.mod(s, 16) == 0:
s2, s4, s8, s16 = int(s/2), int(s/4), int(s/8), int(s/16)
# project `z` and reshape
self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*8*s16*s16, 'g_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_, [-1, s16, s16, self.gf_dim * 8])
h0 = tf.nn.relu(self.g_bn0(self.h0, train=is_train))
self.h1, self.h1_w, self.h1_b = deconv2d(h0,
[self.batch_size, s8, s8, self.gf_dim*4], name='g_h1', with_w=True)
h1 = tf.nn.relu(self.g_bn1(self.h1, train=is_train))
h2, self.h2_w, self.h2_b = deconv2d(h1,
[self.batch_size, s4, s4, self.gf_dim*2], name='g_h2', with_w=True)
h2 = tf.nn.relu(self.g_bn2(h2, train=is_train))
h3, self.h3_w, self.h3_b = deconv2d(h2,
[self.batch_size, s2, s2, self.gf_dim*1], name='g_h3', with_w=True)
h3 = tf.nn.relu(self.g_bn3(h3, train=is_train))
h4, self.h4_w, self.h4_b = deconv2d(h3,
[self.batch_size, s, s, self.c_dim], name='g_h4', with_w=True)
return tf.nn.tanh(h4)
else:
s = self.output_size
s2, s4 = int(s/2), int(s/4)
self.z_, self.h0_w, self.h0_b = linear(z, self.gf_dim*2*s4*s4, 'g_h0_lin', with_w=True)
self.h0 = tf.reshape(self.z_, [-1, s4, s4, self.gf_dim * 2])
h0 = tf.nn.relu(self.g_bn0(self.h0, train=is_train))
self.h1, self.h1_w, self.h1_b = deconv2d(h0,
[self.batch_size, s2, s2, self.gf_dim*1], name='g_h1', with_w=True)
h1 = tf.nn.relu(self.g_bn1(self.h1, train=is_train))
h2, self.h2_w, self.h2_b = deconv2d(h1,
[self.batch_size, s, s, self.c_dim], name='g_h2', with_w=True)
return tf.nn.tanh(h2)