def calculate_loss_distill_relabel(self, predictions, labels_distill, labels, **unused_params):
with tf.name_scope("loss_distill_relabel"):
print("loss_distill_relabel")
epsilon = 10e-6
float_labels = tf.cast(labels, tf.float32)
sum_labels = tf.cast(tf.reduce_sum(float_labels),dtype=tf.int32)
pos_distill, _ = tf.nn.top_k(tf.reshape(labels_distill,[-1]), k=sum_labels)
labels_true = tf.ones(tf.shape(labels))
labels_false = tf.zeros(tf.shape(labels))
labels_add = tf.where(tf.greater_equal(labels_distill, pos_distill[-1]), labels_true, labels_false)
print(labels_add.get_shape().as_list())
float_labels = float_labels+labels_add*(1.0-float_labels)
cross_entropy_loss = float_labels * tf.log(predictions + epsilon) + (
1 - float_labels) * tf.log(1 - predictions + epsilon)
cross_entropy_loss = tf.negative(cross_entropy_loss)
return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
python类zeros()的实例源码
def forward(self, x):
length = lambda mx: int(mx.get_shape()[0])
with tf.variable_scope("QRNN/Forward"):
if self.c is None:
# init context cell
self.c = tf.zeros([length(x), self.kernel.size], dtype=tf.float32)
if self.conv_size <= 2:
# x is batch_size x sentence_length x word_length
# -> now, transpose it to sentence_length x batch_size x word_length
_x = tf.transpose(x, [1, 0, 2])
for i in range(length(_x)):
t = _x[i] # t is batch_size x word_length matrix
f, z, o = self.kernel.forward(t)
self._step(f, z, o)
else:
c_f, c_z, c_o = self.kernel.conv(x)
for i in range(length(c_f)):
f, z, o = c_f[i], c_z[i], c_o[i]
self._step(f, z, o)
return self.h
def create_initial_beam_state(config):
"""Creates an instance of `BeamState` that can be used on the first
call to `beam_step`.
Args:
config: A BeamSearchConfig
Returns:
An instance of `BeamState`.
"""
return BeamSearchState(
log_probs=tf.zeros([config.beam_width]),
finished=tf.zeros(
[config.beam_width], dtype=tf.bool),
lengths=tf.zeros(
[config.beam_width], dtype=tf.int32))
def generateCountMaps(self, coords):
'''Generates a count map for the provided list of coordinates.
'''
s = self.config['projective_field_size']
target_size = 3 + self.config['output_size'] + 2 * s
count_maps = np.zeros((self.config['cls_nb'], target_size, target_size), dtype=np.int16)
shift = - self.config['contextual_pad']
size = self.config['tile_size']
for coord in coords:
y = coord[1] + shift
x = coord[2] + shift
if y >= 0 and y < size and \
x >= 0 and x < size:
self.inc_region(count_maps[coord[0]], *self.target_sizes[y, x])
return np.moveaxis(count_maps, 0, -1).astype(np.float32)
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 call(self, inputs, mask=None, initial_state=None, training=None):
inputs_shape = K.shape(inputs)
zeros = tf.zeros(
shape=[
inputs_shape[0],
inputs_shape[1] - 1,
self.layer.units
]
)
outputs = self.layer.call(
inputs=inputs,
mask=mask,
initial_state=initial_state,
training=training
)
outputs = K.reshape(
tf.slice(outputs, [0, inputs_shape[1] - 1, 0], [-1, 1, -1]),
shape=(inputs_shape[0], 1, self.layer.units)
)
outputs = K.concatenate([outputs, zeros], axis=1)
if 0 < self.layer.dropout + self.layer.recurrent_dropout:
outputs._uses_learning_phase = True
return outputs
def init_params(self, trainable=True, **kwargs):
i_shape, k_shape = self.shapes
# Compute effective number of neurons per filter. Ignores padding.
conv_out = i_shape[0] * i_shape[1]
if hasattr(self, 'pool_side'): conv_out /= self.pool_side**2
elif hasattr(self, 'pool_width'): conv_out /= self.pool_width
self.params['W'] = xavier_init(self.n_visible, self.n_hidden * conv_out,
shape=k_shape + [self.n_hidden],
name='W', trainable=trainable, dtype=self.dtype)
self.params['bhid'] = tf.Variable(tf.zeros(self.n_hidden, dtype=self.dtype),
name='bhid', trainable=trainable)
self.params['bvis'] = tf.Variable(tf.zeros(i_shape, dtype=self.dtype),
name='bvis', trainable=trainable)
def __init__(self, pool_side=2, **kwargs):
"""
pool_side:
Do max pooling on pool_side x pool_side non-overlapping
patches of input.
"""
Conv.__init__(self, **kwargs)
if not kwargs.get('fromfile'):
self.pool_side = pool_side
self.shapes.append([])
# Pool shape
input_size = self.shapes[0] if self.padding == 'SAME' else \
[self.shapes[0][i] - self.shapes[1][i] + 1 for i in range(2)]
self.shapes[2] = [self.batch_size] + \
[input_size[i] / self.strides[i+1] /
self.pool_side for i in range(2)] + \
[self.pool_side**2, self.n_hidden]
self.zeros = tf.zeros(self.shapes[2], dtype=self.dtype)
self.state = {}
def _random_overlay(self, static_hidden=False):
"""Construct random max pool locations."""
s = self.shapes[2]
if static_hidden:
args = np.random.randint(s[2], size=np.prod(s) / s[2] / s[4])
overlay = np.zeros(np.prod(s) / s[4], np.bool)
overlay[args + np.arange(len(args)) * s[2]] = True
overlay = overlay.reshape([s[0], s[1], s[3], s[2]])
overlay = np.rollaxis(overlay, -1, 2)
return arrays.extend(overlay, s[4])
else:
args = np.random.randint(s[2], size=np.prod(s) / s[2])
overlay = np.zeros(np.prod(s), np.bool)
overlay[args + np.arange(len(args)) * s[2]] = True
overlay = overlay.reshape([s[0], s[1], s[3], s[4], s[2]])
return np.rollaxis(overlay, -1, 2)
a8_dynamic_memory_network.py 文件源码
项目:text_classification
作者: brightmart
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def answer_module(self):
""" Answer Module:generate an answer from the final memory vector.
Input:
hidden state from episodic memory module:[batch_size,hidden_size]
question:[batch_size, embedding_size]
"""
steps=self.sequence_length if self.decode_with_sequences else 1 #decoder for a list of tokens with sequence. e.g."x1 x2 x3 x4..."
a=self.m_T #init hidden state
y_pred=tf.zeros((self.batch_size,self.hidden_size)) #TODO usually we will init this as a special token '<GO>', you can change this line by pass embedding of '<GO>' from outside.
logits_list=[]
logits_return=None
for i in range(steps):
cell = rnn.GRUCell(self.hidden_size)
y_previous_q=tf.concat([y_pred,self.query_embedding],axis=1) #[batch_hidden_size*2]
_, a = cell( y_previous_q,a)
logits=tf.layers.dense(a,units=self.num_classes) #[batch_size,vocab_size]
logits_list.append(logits)
if self.decode_with_sequences:#need to get sequences.
logits_return = tf.stack(logits_list, axis=1) # [batch_size,sequence_length,num_classes]
else:#only need to get an answer, not sequences
logits_return = logits_list[0] #[batcj_size,num_classes]
return logits_return
def test():
#below is a function test; if you use this for text classifiction, you need to tranform sentence to indices of vocabulary first. then feed data to the graph.
num_classes=10
learning_rate=0.01
batch_size=8
decay_steps=1000
decay_rate=0.9
sequence_length=5
vocab_size=10000
embed_size=100
is_training=True
dropout_keep_prob=1#0.5
textRNN=TextRCNN(num_classes, learning_rate, batch_size, decay_steps, decay_rate,sequence_length,vocab_size,embed_size,is_training)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
input_x=np.zeros((batch_size,sequence_length)) #[None, self.sequence_length]
input_y=input_y=np.array([1,0,1,1,1,2,1,1]) #np.zeros((batch_size),dtype=np.int32) #[None, self.sequence_length]
loss,acc,predict,_=sess.run([textRNN.loss_val,textRNN.accuracy,textRNN.predictions,textRNN.train_op],
feed_dict={textRNN.input_x:input_x,textRNN.input_y:input_y,textRNN.dropout_keep_prob:dropout_keep_prob})
print("loss:",loss,"acc:",acc,"label:",input_y,"prediction:",predict)
#test()
def init_var(self):
self.rand_h = tf.random_uniform([1], 1.0 - float(self.rnd_hflip), 1.0)
self.rand_v = tf.random_uniform([1], 1.0 - float(self.rnd_vflip), 1.0)
self.rand_t = tf.random_uniform(
[1], 1.0 - float(self.rnd_transpose), 1.0)
self.offset = tf.random_uniform(
[2], dtype='int32', maxval=self.padding * 2 + self.shrink)
if self._debug:
self.offset = tf.Print(self.offset,
['Forward RND module', self.offset])
if self.rnd_size:
self.space = 2 * self.padding - self.offset
self.offset20 = tf.random_uniform(
[], dtype='int32', maxval=self.space[0] * 2) - self.space[0]
self.offset21 = tf.random_uniform(
[], dtype='int32', maxval=self.space[1] * 2) - self.space[1]
self.offset2 = tf.pack([self.offset20, self.offset21])
else:
self.offset2 = tf.zeros([2], dtype='int32')
pass
def _custom_one_step_rnn_loop_fn(self, initial_input_word_embedding, initial_cell_state):
def loop_fn(time, cell_output, cell_state, loop_state):
if cell_output is None: # time == 0
next_cell_state = initial_cell_state
context_vector, attention_logits = self._attention(next_cell_state, initial_input_word_embedding)
pointer_probabilities = self._pointer_probabilities(context_vector, next_cell_state,
initial_input_word_embedding)
next_input = tf.concat(
[initial_input_word_embedding, context_vector, self.pre_computed_query_state_placeholder], axis=1)
next_loop_state = (context_vector, attention_logits, pointer_probabilities)
else:
next_cell_state = cell_state
next_input = tf.zeros(shape=[self.batch_size,
self.word_embedding_dim +
self.encoder_output_size +
self.encoder_cell_state_size
])
next_loop_state = loop_state
elements_finished = cell_output is not None
emit_output = cell_output
return elements_finished, next_input, next_cell_state, emit_output, next_loop_state
return loop_fn
def _generate_labels(self, overlaps):
labels = tf.Variable(tf.ones(shape=(tf.shape(overlaps)[0],), dtype=tf.float32) * -1, trainable=False,
validate_shape=False)
gt_max_overlaps = tf.arg_max(overlaps, dimension=0)
anchor_max_overlaps = tf.arg_max(overlaps, dimension=1)
mask = tf.one_hot(anchor_max_overlaps, tf.shape(overlaps)[1], on_value=True, off_value=False)
max_overlaps = tf.boolean_mask(overlaps, mask)
if self._debug:
max_overlaps = tf.Print(max_overlaps, [max_overlaps])
labels = tf.scatter_update(labels, gt_max_overlaps, tf.ones((tf.shape(gt_max_overlaps)[0],)))
# TODO: extract config object
over_threshold_mask = tf.reshape(tf.where(max_overlaps > 0.5), (-1,))
if self._debug:
over_threshold_mask = tf.Print(over_threshold_mask, [over_threshold_mask], message='over threshold index : ')
labels = tf.scatter_update(labels, over_threshold_mask, tf.ones((tf.shape(over_threshold_mask)[0],)))
# TODO: support clobber positive in the origin implement
below_threshold_mask = tf.reshape(tf.where(max_overlaps < 0.3), (-1,))
if self._debug:
below_threshold_mask = tf.Print(below_threshold_mask, [below_threshold_mask], message='below threshold index : ')
labels = tf.scatter_update(labels, below_threshold_mask, tf.zeros((tf.shape(below_threshold_mask)[0],)))
return labels
def _build(self):
"""Build the linear layer.
Two parameters: weight and bias.
:return: None.
"""
bound = math.sqrt(6.0 / (self._input_size + self._output_size))
w_init = tf.random_uniform(
minval=-bound,
maxval=bound,
shape=(self._input_size, self._output_size),
dtype=D_TYPE,
name='w_init'
)
self._w = tf.Variable(w_init, dtype=D_TYPE, name='w')
if self._with_bias:
b_init = tf.zeros(
shape=(self._output_size,),
dtype=D_TYPE,
name='b_init'
)
self._b = tf.Variable(b_init, dtype=D_TYPE, name='b')
else:
self._b = None
self._batch_norm = BatchNorm('bn', self._output_size) if self._with_batch_norm else None
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 _build(self):
beta_init = tf.zeros(
shape=self._size,
dtype=D_TYPE
)
gamma_init = tf.ones(
shape=self._size,
dtype=D_TYPE
)
self._beta = tf.Variable(
name='beta',
initial_value=beta_init,
dtype=D_TYPE
)
self._gamma = tf.Variable(
name='gamma',
initial_value=gamma_init,
dtype=D_TYPE
)
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 test_record_doc_example(self):
# Test to make sure examples from the documentation compile.
example_datum = {'id': 8,
'name': 'Joe Smith',
'location': (2.5, 7.0)}
num_ids = 16
embed_len = 16
td = tdb
char_rnn = (td.InputTransform(lambda s: [ord(c) for c in s]) >>
td.Map(td.Scalar('int32') >>
td.Function(tdl.Embedding(128, 16))) >>
td.Fold(td.Concat() >> td.Function(tdl.FC(32)),
td.FromTensor(tf.zeros(32))))
r = (td.Record([('id', (td.Scalar('int32') >>
td.Function(tdl.Embedding(num_ids, embed_len)))),
('name', char_rnn),
('location', td.Vector(2))])
>> td.Concat() >> td.Function(tdl.FC(256)))
with self.test_session():
r.eval(example_datum)
def rnn_block(input_block, state_length):
"""Get a fully connected RNN block.
The input is concatenated with the state vector and put through a fully
connected layer to get the next state vector.
Args:
input_block: Put each input through this before concatenating it with the
current state vector.
state_length: Length of the RNN state vector.
Returns:
RNN Block (seq of input_block inputs -> output state)
"""
combine_block = ((td.Identity(), input_block) >> td.Concat()
>> td.Function(td.FC(state_length)))
return td.Fold(combine_block, tf.zeros(state_length))
# All characters are lowercase, so subtract 'a' to make them 0-indexed.
def _discriminator(self, input_images, dims, train_phase, activation=tf.nn.relu, scope_name="discriminator",
scope_reuse=False):
N = len(dims)
with tf.variable_scope(scope_name) as scope:
if scope_reuse:
scope.reuse_variables()
h = input_images
skip_bn = True # First layer of discriminator skips batch norm
for index in range(N - 2):
W = utils.weight_variable([4, 4, dims[index], dims[index + 1]], name="W_%d" % index)
b = tf.zeros([dims[index+1]])
h_conv = utils.conv2d_strided(h, W, b)
if skip_bn:
h_bn = h_conv
skip_bn = False
else:
h_bn = utils.batch_norm(h_conv, dims[index + 1], train_phase, scope="disc_bn%d" % index)
h = activation(h_bn, name="h_%d" % index)
utils.add_activation_summary(h)
W_pred = utils.weight_variable([4, 4, dims[-2], dims[-1]], name="W_pred")
b = tf.zeros([dims[-1]])
h_pred = utils.conv2d_strided(h, W_pred, b)
return None, h_pred, None # Return the last convolution output. None values are returned to maintatin disc from other GAN
def adam_updates(params, cost_or_grads, lr=0.001, mom1=0.9, mom2=0.999):
''' Adam optimizer '''
updates = []
if type(cost_or_grads) is not list:
grads = tf.gradients(cost_or_grads, params)
else:
grads = cost_or_grads
t = tf.Variable(1., 'adam_t')
for p, g in zip(params, grads):
mg = tf.Variable(tf.zeros(p.get_shape()), p.name + '_adam_mg')
if mom1 > 0:
v = tf.Variable(tf.zeros(p.get_shape()), p.name + '_adam_v')
v_t = mom1 * v + (1. - mom1) * g
v_hat = v_t / (1. - tf.pow(mom1, t))
updates.append(v.assign(v_t))
else:
v_hat = g
mg_t = mom2 * mg + (1. - mom2) * tf.square(g)
mg_hat = mg_t / (1. - tf.pow(mom2, t))
g_t = v_hat / tf.sqrt(mg_hat + 1e-8)
p_t = p - lr * g_t
updates.append(mg.assign(mg_t))
updates.append(p.assign(p_t))
updates.append(t.assign_add(1))
return tf.group(*updates)
def __call__(self, u_t, a, b, scope=None):
"""
:param u_t: [N, M, d]
:param a: [N, M. 1]
:param b: [N, M. 1]
:param mask: [N, M]
:return:
"""
N, M, d = self.batch_size, self.mem_size, self.hidden_size
L, sL = self.L, self.sL
with tf.name_scope(scope or self.__class__.__name__):
L = tf.tile(tf.expand_dims(L, 0), [N, 1, 1])
sL = tf.tile(tf.expand_dims(sL, 0), [N, 1, 1])
logb = tf.log(b + 1e-9)
logb = tf.concat(1, [tf.zeros([N, 1, 1]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])])
left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, M, M]
right = a * u_t # [N, M, d]
u = tf.batch_matmul(left, right) # [N, M, d]
return u
def __call__(self, u_t, a, b, scope=None):
"""
:param u_t: [N, M, d]
:param a: [N, M. d]
:param b: [N, M. d]
:param mask: [N, M]
:return:
"""
N, M, d = self.batch_size, self.mem_size, self.hidden_size
L, sL = self.L, self.sL
with tf.name_scope(scope or self.__class__.__name__):
L = tf.tile(tf.expand_dims(tf.expand_dims(L, 0), 0), [N, d, 1, 1])
sL = tf.tile(tf.expand_dims(tf.expand_dims(sL, 0), 0), [N, d, 1, 1])
logb = tf.log(b + 1e-9) # [N, M, d]
logb = tf.concat(1, [tf.zeros([N, 1, d]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])]) # [N, M, d]
logb = tf.expand_dims(tf.transpose(logb, [0, 2, 1]), -1) # [N, d, M, 1]
left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, d, M, M]
right = a * u_t # [N, M, d]
right = tf.expand_dims(tf.transpose(right, [0, 2, 1]), -1) # [N, d, M, 1]
u = tf.batch_matmul(left, right) # [N, d, M, 1]
u = tf.transpose(tf.squeeze(u, [3]), [0, 2, 1]) # [N, M, d]
return u
def __call__(self, u_t, a, b, scope=None):
"""
:param u_t: [N, M, d]
:param a: [N, M. 1]
:param b: [N, M. 1]
:param mask: [N, M]
:return:
"""
N, M, d = self.batch_size, self.mem_size, self.hidden_size
L, sL = self.L, self.sL
with tf.name_scope(scope or self.__class__.__name__):
L = tf.tile(tf.expand_dims(L, 0), [N, 1, 1])
sL = tf.tile(tf.expand_dims(sL, 0), [N, 1, 1])
logb = tf.log(b + 1e-9)
logb = tf.concat(1, [tf.zeros([N, 1, 1]), tf.slice(logb, [0, 1, 0], [-1, -1, -1])])
left = L * tf.exp(tf.batch_matmul(L, logb * sL)) # [N, M, M]
right = a * u_t # [N, M, d]
u = tf.batch_matmul(left, right) # [N, M, d]
return u
model_hypothesis.py 文件源码
项目:uai2017_learning_to_acquire_information
作者: evanthebouncy
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def get_feed_dic_obs(self, obs):
# needing to create all the nessisary feeds
obs_x = []
obs_y = []
obs_tf = []
for _ in range(OBS_SIZE):
obs_x.append(np.zeros([N_BATCH,L]))
obs_y.append(np.zeros([N_BATCH,L]))
obs_tf.append(np.zeros([N_BATCH,2]))
num_obs = len(obs)
for ob_idx in range(num_obs):
ob_coord, ob_lab = obs[ob_idx]
ob_x, ob_y = vectorize(ob_coord)
obs_x[ob_idx] = np.tile(ob_x, [50,1])
obs_y[ob_idx] = np.tile(ob_y, [50,1])
obs_tf[ob_idx] = np.tile(ob_lab, [50,1])
feed_dic = dict(zip(self.ph_obs_x + self.ph_obs_y + self.ph_obs_tf,
obs_x + obs_y + obs_tf))
return feed_dic
model_hypothesis.py 文件源码
项目:uai2017_learning_to_acquire_information
作者: evanthebouncy
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def get_feed_dic_obs(self, obs):
# needing to create all the nessisary feeds
obs_x = []
obs_y = []
obs_tf = []
for _ in range(OBS_SIZE):
obs_x.append(np.zeros([N_BATCH,L]))
obs_y.append(np.zeros([N_BATCH,L]))
obs_tf.append(np.zeros([N_BATCH,2]))
num_obs = len(obs)
for ob_idx in range(num_obs):
ob_coord, ob_lab = obs[ob_idx]
ob_x, ob_y = vectorize(ob_coord)
obs_x[ob_idx] = np.tile(ob_x, [50,1])
obs_y[ob_idx] = np.tile(ob_y, [50,1])
obs_tf[ob_idx] = np.tile(ob_lab, [50,1])
feed_dic = dict(zip(self.ph_obs_x + self.ph_obs_y + self.ph_obs_tf,
obs_x + obs_y + obs_tf))
return feed_dic
def spatial_2d_padding(x, padding=(1, 1), dim_ordering='default'):
'''Pads the 2nd and 3rd dimensions of a 4D tensor
with "padding[0]" and "padding[1]" (resp.) zeros left and right.
'''
if dim_ordering == 'default':
dim_ordering = image_dim_ordering()
if dim_ordering not in {'th', 'tf'}:
raise ValueError('Unknown dim_ordering ' + str(dim_ordering))
if dim_ordering == 'th':
pattern = [[0, 0], [0, 0],
[padding[0], padding[0]], [padding[1], padding[1]]]
else:
pattern = [[0, 0],
[padding[0], padding[0]], [padding[1], padding[1]],
[0, 0]]
return tf.pad(x, pattern)
def create(self):
config = self.config
d2 = dict(config.discriminator)
d2['class'] = self.ops.lookup("class:hypergan.discriminators.pyramid_discriminator.PyramidDiscriminator")
self.encoder = self.create_component(d2)
self.encoder.ops.describe("encoder")
self.encoder.create(self.inputs.x)
self.encoder.z = tf.zeros(0)
self.trainer = self.create_component(config.trainer)
StandardGAN.create(self)
cycloss = tf.reduce_mean(tf.abs(self.inputs.x-self.generator.sample))
cycloss_lambda = config.cycloss_lambda or 10
self.loss.sample[1] *= config.g_lambda or 1
self.loss.sample[1] += cycloss*cycloss_lambda
self.trainer.create()
self.session.run(tf.global_variables_initializer())
def test_relation_layer(self):
component = GANComponent(gan=gan, config={'test':True})
with self.test_session():
constant = tf.zeros([1, 2, 2, 1])
split = component.split_by_width_height(constant)
self.assertEqual(len(split), 4)
permute = component.permute(split, 2)
self.assertEqual(len(permute), 12)
rel_layer = component.relation_layer(constant)
self.assertEqual(gan.ops.shape(rel_layer), [1,2,2,1])
constant = tf.zeros([1, 4, 4, 1])
split = component.split_by_width_height(constant)
self.assertEqual(len(split), 16)
permute = component.permute(split, 2)
self.assertEqual(len(permute), 240)
rel_layer = component.relation_layer(constant)
self.assertEqual(gan.ops.shape(rel_layer), [1,4,4,1])