def build_model(sess, embedding_dim, batch_size):
model = CondGAN(
lr_imsize=cfg.TEST.LR_IMSIZE,
hr_lr_ratio=int(cfg.TEST.HR_IMSIZE/cfg.TEST.LR_IMSIZE))
embeddings = tf.placeholder(
tf.float32, [batch_size, embedding_dim],
name='conditional_embeddings')
with pt.defaults_scope(phase=pt.Phase.test):
with tf.variable_scope("g_net"):
c = sample_encoded_context(embeddings, model)
z = tf.random_normal([batch_size, cfg.Z_DIM])
fake_images = model.get_generator(tf.concat(1, [c, z]))
with tf.variable_scope("hr_g_net"):
hr_c = sample_encoded_context(embeddings, model)
hr_fake_images = model.hr_get_generator(fake_images, hr_c)
ckt_path = cfg.TEST.PRETRAINED_MODEL
if ckt_path.find('.ckpt') != -1:
print("Reading model parameters from %s" % ckt_path)
saver = tf.train.Saver(tf.all_variables())
saver.restore(sess, ckt_path)
else:
print("Input a valid model path.")
return embeddings, fake_images, hr_fake_images
python类random_normal()的实例源码
birds_skip_thought_demo.py 文件源码
项目:how_to_convert_text_to_images
作者: llSourcell
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def build_encoder(self):
"""Inference Network. q(h|X)"""
with tf.variable_scope("encoder"):
q_cell = tf.nn.rnn_cell.LSTMCell(self.embed_dim, self.vocab_size)
a_cell = tf.nn.rnn_cell.LSTMCell(self.embed_dim, self.vocab_size)
l1 = tf.nn.relu(tf.nn.rnn_cell.linear(tf.expand_dims(self.x, 0), self.embed_dim, bias=True, scope="l1"))
l2 = tf.nn.relu(tf.nn.rnn_cell.linear(l1, self.embed_dim, bias=True, scope="l2"))
self.mu = tf.nn.rnn_cell.linear(l2, self.h_dim, bias=True, scope="mu")
self.log_sigma_sq = tf.nn.rnn_cell.linear(l2, self.h_dim, bias=True, scope="log_sigma_sq")
eps = tf.random_normal((1, self.h_dim), 0, 1, dtype=tf.float32)
sigma = tf.sqrt(tf.exp(self.log_sigma_sq))
_ = tf.histogram_summary("mu", self.mu)
_ = tf.histogram_summary("sigma", sigma)
self.h = self.mu + sigma * eps
def build_encoder(self):
"""Inference Network. q(h|X)"""
with tf.variable_scope("encoder"):
self.l1_lin = linear(tf.expand_dims(self.x, 0), self.embed_dim, bias=True, scope="l1")
self.l1 = tf.nn.relu(self.l1_lin)
self.l2_lin = linear(self.l1, self.embed_dim, bias=True, scope="l2")
self.l2 = tf.nn.relu(self.l2_lin)
self.mu = linear(self.l2, self.h_dim, bias=True, scope="mu")
self.log_sigma_sq = linear(self.l2, self.h_dim, bias=True, scope="log_sigma_sq")
self.eps = tf.random_normal((1, self.h_dim), 0, 1, dtype=tf.float32)
self.sigma = tf.sqrt(tf.exp(self.log_sigma_sq))
self.h = tf.add(self.mu, tf.mul(self.sigma, self.eps))
_ = tf.histogram_summary("mu", self.mu)
_ = tf.histogram_summary("sigma", self.sigma)
_ = tf.histogram_summary("h", self.h)
_ = tf.histogram_summary("mu + sigma", self.mu + self.sigma)
def baseline_forward(self, X, size, n_class):
shape = X.get_shape()
_X = tf.transpose(X, [1, 0, 2]) # batch_size x sentence_length x word_length -> batch_size x sentence_length x word_length
_X = tf.reshape(_X, [-1, int(shape[2])]) # (batch_size x sentence_length) x word_length
seq = tf.split(0, int(shape[1]), _X) # sentence_length x (batch_size x word_length)
with tf.name_scope("LSTM"):
lstm_cell = rnn_cell.BasicLSTMCell(size, forget_bias=1.0)
outputs, states = rnn.rnn(lstm_cell, seq, dtype=tf.float32)
with tf.name_scope("LSTM-Classifier"):
W = tf.Variable(tf.random_normal([size, n_class]), name="W")
b = tf.Variable(tf.random_normal([n_class]), name="b")
output = tf.matmul(outputs[-1], W) + b
return output
def test_encode(self):
inputs = tf.random_normal(
[self.batch_size, self.sequence_length, self.input_depth])
example_length = tf.ones(
self.batch_size, dtype=tf.int32) * self.sequence_length
encode_fn = rnn_encoder.UnidirectionalRNNEncoder(self.params, self.mode)
encoder_output = encode_fn(inputs, example_length)
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
encoder_output_ = sess.run(encoder_output)
np.testing.assert_array_equal(encoder_output_.outputs.shape,
[self.batch_size, self.sequence_length, 32])
self.assertIsInstance(encoder_output_.final_state,
tf.contrib.rnn.LSTMStateTuple)
np.testing.assert_array_equal(encoder_output_.final_state.h.shape,
[self.batch_size, 32])
np.testing.assert_array_equal(encoder_output_.final_state.c.shape,
[self.batch_size, 32])
def _test_encode_with_params(self, params):
"""Tests the StackBidirectionalRNNEncoder with a specific cell"""
inputs = tf.random_normal(
[self.batch_size, self.sequence_length, self.input_depth])
example_length = tf.ones(
self.batch_size, dtype=tf.int32) * self.sequence_length
encode_fn = rnn_encoder.StackBidirectionalRNNEncoder(params, self.mode)
encoder_output = encode_fn(inputs, example_length)
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
encoder_output_ = sess.run(encoder_output)
output_size = encode_fn.params["rnn_cell"]["cell_params"]["num_units"]
np.testing.assert_array_equal(
encoder_output_.outputs.shape,
[self.batch_size, self.sequence_length, output_size * 2])
return encoder_output_
def test_with_fixed_inputs(self):
inputs = tf.random_normal(
[self.batch_size, self.sequence_length, self.input_depth])
seq_length = tf.ones(self.batch_size, dtype=tf.int32) * self.sequence_length
helper = decode_helper.TrainingHelper(
inputs=inputs, sequence_length=seq_length)
decoder_fn = self.create_decoder(
helper=helper, mode=tf.contrib.learn.ModeKeys.TRAIN)
initial_state = decoder_fn.cell.zero_state(
self.batch_size, dtype=tf.float32)
decoder_output, _ = decoder_fn(initial_state, helper)
#pylint: disable=E1101
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
decoder_output_ = sess.run(decoder_output)
np.testing.assert_array_equal(
decoder_output_.logits.shape,
[self.sequence_length, self.batch_size, self.vocab_size])
np.testing.assert_array_equal(decoder_output_.predicted_ids.shape,
[self.sequence_length, self.batch_size])
return decoder_output_
def _test_with_params(self, params):
"""Tests the encoder with a given parameter configuration"""
inputs = tf.random_normal(
[self.batch_size, self.sequence_length, self.input_depth])
example_length = tf.ones(
self.batch_size, dtype=tf.int32) * self.sequence_length
encode_fn = PoolingEncoder(params, self.mode)
encoder_output = encode_fn(inputs, example_length)
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
encoder_output_ = sess.run(encoder_output)
np.testing.assert_array_equal(
encoder_output_.outputs.shape,
[self.batch_size, self.sequence_length, self.input_depth])
np.testing.assert_array_equal(
encoder_output_.attention_values.shape,
[self.batch_size, self.sequence_length, self.input_depth])
np.testing.assert_array_equal(encoder_output_.final_state.shape,
[self.batch_size, self.input_depth])
def _leapfrog_step(self, position, velocity, velocity_step_multiplier=1.):
""" Makes a single leapfrog step with friction. """
d_energy = self._d_energy_fn(position)
friction = self.friction
deceleration = -friction * self._transpose_mul(velocity, self._current_step_size)
velocity -= self._transpose_mul(d_energy, velocity_step_multiplier * self._current_step_size)
velocity += deceleration
# B_hat = 0, C = friction
noise = tf.random_normal(tf.shape(velocity))
stddevs = (2 * friction * self._current_step_size) ** 0.5
noise = self._transpose_mul(noise, stddevs)
velocity += noise
position = position + self._transpose_mul(velocity, self._current_step_size)
return position, velocity
def _validate(self, machine, n=10):
N = n * n
# same row same z
z = tf.random_normal(shape=[n, self.arch['z_dim']])
z = tf.tile(z, [1, n])
z = tf.reshape(z, [N, -1])
z = tf.Variable(z, trainable=False, dtype=tf.float32)
# same column same y
y = tf.range(0, 10, 1, dtype=tf.int64)
y = tf.reshape(y, [-1, 1])
y = tf.tile(y, [n, 1])
Xh = machine.generate(z, y) # 100, 64, 64, 3
# Xh = gray2jet(Xh)
# Xh = make_png_thumbnail(Xh, n)
Xh = make_png_jet_thumbnail(Xh, n)
return Xh
def _validate(self, machine, n=10):
N = n * n
# same row same z
z = tf.random_normal(shape=[n, self.arch['z_dim']])
z = tf.tile(z, [1, n])
z = tf.reshape(z, [N, -1])
z = tf.Variable(z, trainable=False, dtype=tf.float32)
# same column same y
y = tf.range(0, 10, 1, dtype=tf.int64)
y = tf.reshape(y, [-1,])
y = tf.tile(y, [n,])
Xh = machine.generate(z, y) # 100, 64, 64, 3
Xh = make_png_thumbnail(Xh, n)
return Xh
def __call__(self, inputs, steps):
def fn(zv, x):
"""
Transition for training, without Metropolis-Hastings.
`z` is the input state.
`v` is created as a dummy variable to allow output of v_, for training p(v).
:param x: variable only for specifying the number of steps
:return: next state `z_`, and the corresponding auxiliary variable `v_`.
"""
z, v = zv
v = tf.random_normal(shape=tf.stack([tf.shape(z)[0], self.network.v_dim]))
z_, v_ = self.network.forward([z, v])
return z_, v_
elems = tf.zeros([steps])
return tf.scan(fn, elems, inputs, back_prop=True)
def _build(self):
w_init = tf.random_normal(
stddev=0.01,
shape=(
self._filter_height,
self._filter_width,
self._input_depth,
self._output_depth
),
dtype=D_TYPE,
name='w_init'
)
b_init = tf.zeros(
shape=(self._output_depth,),
dtype=D_TYPE,
name='b_init'
)
self._w = tf.Variable(w_init, dtype=D_TYPE, name='w')
self._b = tf.Variable(b_init, dtype=D_TYPE, name='b')
def sample_encoded_context(self, embeddings):
'''Helper function for init_opt'''
c_mean_logsigma = self.model.generate_condition(embeddings)
mean = c_mean_logsigma[0]
if cfg.TRAIN.COND_AUGMENTATION:
# epsilon = tf.random_normal(tf.shape(mean))
epsilon = tf.truncated_normal(tf.shape(mean))
stddev = tf.exp(c_mean_logsigma[1])
c = mean + stddev * epsilon
kl_loss = KL_loss(c_mean_logsigma[0], c_mean_logsigma[1])
else:
c = mean
kl_loss = 0
return c, cfg.TRAIN.COEFF.KL * kl_loss
def sample_encoded_context(self, embeddings):
'''Helper function for init_opt'''
# Build conditioning augmentation structure for text embedding
# under different variable_scope: 'g_net' and 'hr_g_net'
c_mean_logsigma = self.model.generate_condition(embeddings)
mean = c_mean_logsigma[0]
if cfg.TRAIN.COND_AUGMENTATION:
# epsilon = tf.random_normal(tf.shape(mean))
epsilon = tf.truncated_normal(tf.shape(mean))
stddev = tf.exp(c_mean_logsigma[1])
c = mean + stddev * epsilon
kl_loss = KL_loss(c_mean_logsigma[0], c_mean_logsigma[1])
else:
c = mean
kl_loss = 0
# TODO: play with the coefficient for KL
return c, cfg.TRAIN.COEFF.KL * kl_loss
def build_model(sess, embedding_dim, batch_size):
model = CondGAN(
lr_imsize=cfg.TEST.LR_IMSIZE,
hr_lr_ratio=int(cfg.TEST.HR_IMSIZE/cfg.TEST.LR_IMSIZE))
embeddings = tf.placeholder(
tf.float32, [batch_size, embedding_dim],
name='conditional_embeddings')
with pt.defaults_scope(phase=pt.Phase.test):
with tf.variable_scope("g_net"):
c = sample_encoded_context(embeddings, model)
z = tf.random_normal([batch_size, cfg.Z_DIM])
fake_images = model.get_generator(tf.concat(1, [c, z]))
with tf.variable_scope("hr_g_net"):
hr_c = sample_encoded_context(embeddings, model)
hr_fake_images = model.hr_get_generator(fake_images, hr_c)
ckt_path = cfg.TEST.PRETRAINED_MODEL
if ckt_path.find('.ckpt') != -1:
print("Reading model parameters from %s" % ckt_path)
saver = tf.train.Saver(tf.all_variables())
saver.restore(sess, ckt_path)
else:
print("Input a valid model path.")
return embeddings, fake_images, hr_fake_images
def conv(self, name, filter_size, stride, num_output, stddev, bias, layer_name=None, input=None, reuse=False, relu=True):
if input is None:
input = self.last_layer
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
weight = tf.get_variable('weights', dtype=tf.float32, \
initializer=tf.random_normal([filter_size, filter_size, int(input.shape[3]), num_output], \
stddev=stddev))
bias = tf.get_variable('biases', dtype=tf.float32, initializer=np.ones(num_output, dtype=np.float32)*bias)
self.weights[name] = weight
self.biases[name] = bias
if layer_name is not None:
name = layer_name
if relu:
self.layers[name] = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input, weight, [1, stride, stride, 1], "VALID"), bias))
else:
self.layers[name] = tf.nn.bias_add(tf.nn.conv2d(input, weight, [1, stride, stride, 1], "VALID"), bias)
self.last_layer = self.layers[name]
return self
def set_input_shape(self, input_shape):
batch_size, rows, cols, input_channels = input_shape
kernel_shape = tuple(self.kernel_shape) + (input_channels,
self.output_channels)
assert len(kernel_shape) == 4
assert all(isinstance(e, int) for e in kernel_shape), kernel_shape
init = tf.random_normal(kernel_shape, dtype=tf.float32)
init = init / tf.sqrt(1e-7 + tf.reduce_sum(tf.square(init),
axis=(0, 1, 2)))
self.kernels = tf.Variable(init)
self.b = tf.Variable(
np.zeros((self.output_channels,)).astype('float32'))
input_shape = list(input_shape)
input_shape[0] = 1
dummy_batch = tf.zeros(input_shape)
dummy_output = self.fprop(dummy_batch)
output_shape = [int(e) for e in dummy_output.get_shape()]
output_shape[0] = 1
self.output_shape = tuple(output_shape)
def add_bw(gan, config, net):
x = gan.inputs.x
s = [int(x) for x in net.get_shape()]
print("S IS ", s)
shape = [s[1], s[2]]
x = tf.image.resize_images(x, shape, 1)
bwnet = tf.slice(net, [0, 0, 0, 0], [s[0],s[1],s[2], 3])
if not gan.config.add_full_image:
print( "[colorizer] Adding black and white image", x)
x = tf.image.rgb_to_grayscale(x)
if config.colorizer_noise is not None:
x += tf.random_normal(x.get_shape(), mean=0, stddev=config.colorizer_noise, dtype=tf.float32)
#bwnet = tf.image.rgb_to_grayscale(bwnet)
#x = tf.concat(axis=3, values=[x, bwnet])
else:
print( "[colorizer] Adding full image", x)
return x
def __init__(self, ob_dim, ac_dim): #pylint: disable=W0613
X = tf.placeholder(tf.float32, shape=[None, ob_dim*2+ac_dim*2+2]) # batch of observations
vtarg_n = tf.placeholder(tf.float32, shape=[None], name='vtarg')
wd_dict = {}
h1 = tf.nn.elu(dense(X, 64, "h1", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict))
h2 = tf.nn.elu(dense(h1, 64, "h2", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict))
vpred_n = dense(h2, 1, "hfinal", weight_init=U.normc_initializer(1.0), bias_init=0, weight_loss_dict=wd_dict)[:,0]
sample_vpred_n = vpred_n + tf.random_normal(tf.shape(vpred_n))
wd_loss = tf.get_collection("vf_losses", None)
loss = U.mean(tf.square(vpred_n - vtarg_n)) + tf.add_n(wd_loss)
loss_sampled = U.mean(tf.square(vpred_n - tf.stop_gradient(sample_vpred_n)))
self._predict = U.function([X], vpred_n)
optim = kfac.KfacOptimizer(learning_rate=0.001, cold_lr=0.001*(1-0.9), momentum=0.9, \
clip_kl=0.3, epsilon=0.1, stats_decay=0.95, \
async=1, kfac_update=2, cold_iter=50, \
weight_decay_dict=wd_dict, max_grad_norm=None)
vf_var_list = []
for var in tf.trainable_variables():
if "vf" in var.name:
vf_var_list.append(var)
update_op, self.q_runner = optim.minimize(loss, loss_sampled, var_list=vf_var_list)
self.do_update = U.function([X, vtarg_n], update_op) #pylint: disable=E1101
U.initialize() # Initialize uninitialized TF variables
def testExternalBias(self):
batch_size = 4
num_hidden = 6
num_dims = 8
test_inputs = tf.random_normal(shape=(batch_size, num_dims))
test_b_enc = tf.random_normal(shape=(batch_size, num_hidden))
test_b_dec = tf.random_normal(shape=(batch_size, num_dims))
nade = Nade(num_dims, num_hidden)
log_prob, cond_probs = nade.log_prob(test_inputs, test_b_enc, test_b_dec)
sample, sample_prob = nade.sample(b_enc=test_b_enc, b_dec=test_b_dec)
with self.test_session() as sess:
sess.run([tf.global_variables_initializer()])
self.assertEqual(log_prob.eval().shape, (batch_size,))
self.assertEqual(cond_probs.eval().shape, (batch_size, num_dims))
self.assertEqual(sample.eval().shape, (batch_size, num_dims))
self.assertEqual(sample_prob.eval().shape, (batch_size,))
def _sample(self, n_samples):
mean, cov_tril = self.mean, self.cov_tril
if not self.is_reparameterized:
mean = tf.stop_gradient(mean)
cov_tril = tf.stop_gradient(cov_tril)
def tile(t):
new_shape = tf.concat([[n_samples], tf.ones_like(tf.shape(t))], 0)
return tf.tile(tf.expand_dims(t, 0), new_shape)
batch_mean = tile(mean)
batch_cov = tile(cov_tril)
# n_dim -> n_dim x 1 for matmul
batch_mean = tf.expand_dims(batch_mean, -1)
noise = tf.random_normal(tf.shape(batch_mean), dtype=self.dtype)
samples = tf.matmul(batch_cov, noise) + batch_mean
samples = tf.squeeze(samples, -1)
# Update static shape
static_n_samples = n_samples if isinstance(n_samples, int) else None
samples.set_shape(tf.TensorShape([static_n_samples])
.concatenate(self.get_batch_shape())
.concatenate(self.get_value_shape()))
return samples
def pop(tensor, shape):
"""
Pop art filter
:param Tensor tensor:
:param list[int] shape:
"""
images = []
freq = random.randint(1, 3) * 2
ref = _downsample(resample(tensor, shape), shape, [int(shape[0] / (freq * 2)), int(shape[1] / (freq * 2)), shape[2]])
for i in range(freq * freq):
image = posterize(ref, random.randint(3, 6))
image = image % tf.random_normal([3], mean=.5, stddev=.25)
images.append(image)
x, y = point_cloud(freq, distrib=PointDistribution.square, shape=shape, corners=True)
out = voronoi(None, shape, diagram_type=VoronoiDiagramType.collage, xy=(x, y, len(x)), nth=random.randint(0, 3), collage_images=images, image_count=4)
return outline(out, shape, sobel_func=1)
def _create_network(self):
# Initialize autoencode network weights and biases
network_weights = self._initialize_weights(**self.network_architecture)
# Use recognition network to determine mean and
# (log) variance of Gaussian distribution in latent
# space
self.z_mean, self.z_log_sigma_sq = \
self._recognition_network(network_weights["weights_recog"],
network_weights["biases_recog"])
# Draw one sample z from Gaussian distribution
n_z = self.network_architecture["n_z"]
eps = tf.random_normal((self.batch_size, n_z), 0, 1,
dtype=tf.float32)
# z = mu + sigma*epsilon
self.z = tf.add(self.z_mean,
tf.mul(tf.sqrt(tf.exp(self.z_log_sigma_sq)), eps))
# Use generator to determine mean of
# Bernoulli distribution of reconstructed input
self.x_reconstr_mean = \
self._generator_network(network_weights["weights_gener"],
network_weights["biases_gener"])
def gaussian_stochastic(self, input_tensor, num_maps, scope):
"""
:param inputs_list: list of Tensors to be added and input into the block
:return: random variable single draw, mean, standard deviation, and intermediate representation
"""
with tf.variable_scope(scope):
input_tensor = tf.expand_dims(tf.expand_dims(input_tensor, 1), 1) if len(input_tensor.get_shape()) != 4 \
else input_tensor
intermediate = slim.conv2d(input_tensor, self._hidden_size, [1, 1], weights_initializer=self._initializer,
scope='conv1')
mean = slim.conv2d(intermediate, num_maps, [1, 1], weights_initializer=self._initializer,
activation_fn=None, scope='mean')
sigma2 = tf.nn.softplus(
slim.conv2d(intermediate, num_maps, [1, 1], weights_initializer=self._initializer,
activation_fn=None, scope='sigma2'))
rv_single_draw = mean + tf.sqrt(sigma2) * tf.random_normal(tf.shape(mean))
self.split_labeled_unlabeled(mean, '{}_mu'.format(scope))
self.split_labeled_unlabeled(sigma2, '{}_sigma2'.format(scope))
self.split_labeled_unlabeled(rv_single_draw, '{}_sample'.format(scope))
return rv_single_draw
def gaussian_stochastic(self, input_tensor, num_maps, scope):
"""
:param inputs_list: list of Tensors to be added and input into the block
:return: random variable single draw, mean, standard deviation, and intermediate representation
"""
with tf.variable_scope(scope):
input_tensor = tf.expand_dims(tf.expand_dims(input_tensor, 1), 1) if len(input_tensor.get_shape()) != 4 \
else input_tensor
intermediate = slim.conv2d(input_tensor, self._hidden_size, [1, 1], weights_initializer=self._initializer,
scope='conv1')
mean = slim.conv2d(intermediate, num_maps, [1, 1], weights_initializer=self._initializer,
activation_fn=None, scope='mean')
sigma2 = tf.nn.softplus(
slim.conv2d(intermediate, num_maps, [1, 1], weights_initializer=self._initializer,
activation_fn=None, scope='sigma2'))
rv_single_draw = mean + tf.sqrt(sigma2) * tf.random_normal(tf.shape(mean))
self.split_labeled_unlabeled(mean, '{}_mu'.format(scope))
self.split_labeled_unlabeled(sigma2, '{}_sigma2'.format(scope))
self.split_labeled_unlabeled(rv_single_draw, '{}_sample'.format(scope))
return rv_single_draw
def normal(shape=None, mean=0.0, stddev=0.02, dtype=tf.float32, seed=None):
""" Normal.
Initialization with random values from a normal distribution.
Arguments:
shape: List of `int`. A shape to initialize a Tensor (optional).
mean: Same as `dtype`. The mean of the truncated normal distribution.
stddev: Same as `dtype`. The standard deviation of the truncated
normal distribution.
dtype: The tensor data type.
seed: `int`. Used to create a random seed for the distribution.
Returns:
The Initializer, or an initialized `Tensor` if shape is specified.
"""
if shape:
return tf.random_normal(shape, mean=mean, stddev=stddev, seed=seed,
dtype=dtype)
else:
return tf.random_normal_initializer(mean=mean, stddev=stddev,
seed=seed, dtype=dtype)
def testComputation(self):
model_rnn = snt.ModelRNN(self.model)
inputs = tf.random_normal([self.batch_size, 5])
prev_state = tf.placeholder(tf.float32,
shape=[self.batch_size, self.hidden_size])
outputs, next_state = model_rnn(inputs, prev_state)
with self.test_session() as sess:
prev_state_data = np.random.randn(self.batch_size, self.hidden_size)
feed_dict = {prev_state: prev_state_data}
sess.run(tf.global_variables_initializer())
outputs_value = sess.run([outputs, next_state], feed_dict=feed_dict)
outputs_value, next_state_value = outputs_value
self.assertAllClose(prev_state_data, outputs_value)
self.assertAllClose(outputs_value, next_state_value)
def testInputExampleIndex(self):
in1 = tf.random_normal((3, 5))
in2 = tf.random_normal((3, 9))
def build(inputs):
a, b = inputs
a.get_shape().assert_is_compatible_with([3 * 5])
b.get_shape().assert_is_compatible_with([3 * 9])
return b
op = snt.Module(build)
# Checks an error is thrown when the input example contains a different
# shape for the leading dimensions as the output.
with self.assertRaises(ValueError):
snt.BatchApply(op, n_dims=2, input_example_index=0)((in1, in2))
# Check correct operation when the specified input example contains the same
# shape for the leading dimensions as the output.
output = snt.BatchApply(op, n_dims=2, input_example_index=1)((in1, in2))
with self.test_session() as sess:
in2_np, out_np = sess.run([in2, output])
self.assertAllEqual(in2_np, out_np)
def _conv(inpOp, nIn, nOut, kH, kW, dH, dW, padType):
global conv_counter
global parameters
name = 'conv' + str(conv_counter)
conv_counter += 1
with tf.variable_scope(name) as scope:
#kernel = tf.get_variable(name='weights', initializer=tf.random_normal([kH, kW, nIn, nOut], dtype=tf.float32, stddev=1e-2))
kernel = tf.get_variable(name='weights', shape=[kH, kW, nIn, nOut], initializer=tf.truncated_normal_initializer(dtype=tf.float32, stddev=1e-2))
strides = [1, dH, dW, 1]
conv = tf.nn.conv2d(inpOp, kernel, strides, padding=padType)
#biases = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),
# trainable=True, name='biases')
biases = tf.get_variable(name='biases', initializer=tf.constant(0.0, shape=[nOut], dtype=tf.float32), dtype=tf.float32)
bias = tf.reshape(tf.nn.bias_add(conv, biases),
conv.get_shape())
parameters += [kernel, biases]
return bias