def bytenet_internal(inputs, targets, hparams, train): """ByteNet, main step used for training.""" with tf.variable_scope("bytenet"): # Flatten inputs and extend length by 50%. inputs = tf.expand_dims(common_layers.flatten4d3d(inputs), axis=2) extend_length = tf.to_int32(0.5 * tf.to_float(tf.shape(inputs)[1])) inputs_shape = inputs.shape.as_list() inputs = tf.pad(inputs, [[0, 0], [0, extend_length], [0, 0], [0, 0]]) inputs_shape[1] = None inputs.set_shape( inputs_shape) # Don't lose the other shapes when padding. # Pad inputs and targets to be the same length, divisible by 50. inputs, targets = common_layers.pad_to_same_length( inputs, targets, final_length_divisible_by=50) final_encoder = residual_dilated_conv(inputs, hparams.num_block_repeat, "SAME", "encoder", hparams, train) shifted_targets = common_layers.shift_left(targets) kernel = (hparams.kernel_height, hparams.kernel_width) decoder_start = common_layers.conv_block( tf.concat([final_encoder, shifted_targets], axis=3), hparams.hidden_size, [((1, 1), kernel)], padding="LEFT") return residual_dilated_conv(decoder_start, hparams.num_block_repeat, "LEFT", "decoder", hparams, train)
def testPadToSameLength(self): x1 = np.random.rand(5, 7, 11) x2 = np.random.rand(5, 9, 11) with self.test_session() as session: a, b = common_layers.pad_to_same_length( tf.constant(x1, dtype=tf.float32), tf.constant(x2, dtype=tf.float32)) c, d = common_layers.pad_to_same_length( tf.constant(x1, dtype=tf.float32), tf.constant(x2, dtype=tf.float32), final_length_divisible_by=4) res1, res2 = session.run([a, b]) res1a, res2a = session.run([c, d]) self.assertEqual(res1.shape, (5, 9, 11)) self.assertEqual(res2.shape, (5, 9, 11)) self.assertEqual(res1a.shape, (5, 12, 11)) self.assertEqual(res2a.shape, (5, 12, 11))
def bytenet_internal(inputs, targets, hparams, train): """ByteNet, main step used for training.""" with tf.variable_scope("bytenet"): # Flatten inputs and extend length by 50%. inputs = tf.expand_dims(common_layers.flatten4d3d(inputs), axis=2) extend_length = tf.to_int32(0.5 * tf.to_float(tf.shape(inputs)[1])) inputs_shape = inputs.shape.as_list() inputs = tf.pad(inputs, [[0, 0], [0, extend_length], [0, 0], [0, 0]]) inputs_shape[1] = None inputs.set_shape(inputs_shape) # Don't lose the other shapes when padding. # Pad inputs and targets to be the same length, divisible by 50. inputs, targets = common_layers.pad_to_same_length( inputs, targets, final_length_divisible_by=50) final_encoder = residual_dilated_conv( inputs, hparams.num_block_repeat, "SAME", "encoder", hparams, train) shifted_targets = common_layers.shift_left(targets) kernel = (hparams.kernel_height, hparams.kernel_width) decoder_start = common_layers.conv_block( tf.concat([final_encoder, shifted_targets], axis=3), hparams.hidden_size, [((1, 1), kernel)], padding="LEFT") return residual_dilated_conv( decoder_start, hparams.num_block_repeat, "LEFT", "decoder", hparams, train)
def testPadToSameLength(self): x1 = np.random.rand(5, 7, 11) x2 = np.random.rand(5, 9, 11) with self.test_session() as session: a, b = common_layers.pad_to_same_length( tf.constant(x1, dtype=tf.float32), tf.constant(x2, dtype=tf.float32)) c, d = common_layers.pad_to_same_length( tf.constant(x1, dtype=tf.float32), tf.constant(x2, dtype=tf.float32), final_length_divisible_by=4) res1, res2 = session.run([a, b]) res1a, res2a = session.run([c, d]) self.assertEqual(res1.shape, (5, 9, 11)) self.assertEqual(res2.shape, (5, 9, 11)) self.assertEqual(res1a.shape, (5, 12, 11)) self.assertEqual(res2a.shape, (5, 12, 11))
def similarity_cost(inputs_encoded, targets_encoded): """Loss telling to be more similar to your own targets than to others.""" # This is a first very simple version: handle variable-length by padding # to same length and putting everything into batch. In need of a better way. x, y = common_layers.pad_to_same_length(inputs_encoded, targets_encoded) depth = tf.shape(inputs_encoded)[3] x, y = tf.reshape(x, [-1, depth]), tf.reshape(y, [-1, depth]) return rank_loss(x, y)
def similarity_cost(inputs_encoded, targets_encoded): """Loss telling to be more similar to your own targets than to others.""" # This is a first very simple version: handle variable-length by padding # to same length and putting everything into batch. In need of a better way. x, y = common_layers.pad_to_same_length(inputs_encoded, targets_encoded) depth = tf.shape(inputs_encoded)[3] x, y = tf.reshape(x, [-1, depth]), tf.reshape(y, [-1, depth]) return rank_loss(x, y)