|
| 1 | +# Copyright 2017 Google Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""BlueNet: and out of the blue network to experiment with shake-shake.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +# Dependency imports |
| 22 | + |
| 23 | +from six.moves import xrange # pylint: disable=redefined-builtin |
| 24 | + |
| 25 | +from tensor2tensor.models import common_hparams |
| 26 | +from tensor2tensor.models import common_layers |
| 27 | +from tensor2tensor.utils import registry |
| 28 | +from tensor2tensor.utils import t2t_model |
| 29 | + |
| 30 | +import tensorflow as tf |
| 31 | + |
| 32 | + |
| 33 | +def residual_module(x, hparams, train, n, sep): |
| 34 | + """A stack of convolution blocks with residual connection.""" |
| 35 | + k = (hparams.kernel_height, hparams.kernel_width) |
| 36 | + dilations_and_kernels = [((1, 1), k) for _ in xrange(n)] |
| 37 | + with tf.variable_scope("residual_module%d_sep%d" % (n, sep)): |
| 38 | + y = common_layers.subseparable_conv_block( |
| 39 | + x, |
| 40 | + hparams.hidden_size, |
| 41 | + dilations_and_kernels, |
| 42 | + padding="SAME", |
| 43 | + separability=sep, |
| 44 | + name="block") |
| 45 | + x = common_layers.layer_norm(x + y, hparams.hidden_size, name="lnorm") |
| 46 | + return tf.nn.dropout(x, 1.0 - hparams.dropout * tf.to_float(train)) |
| 47 | + |
| 48 | + |
| 49 | +def residual_module1(x, hparams, train): |
| 50 | + return residual_module(x, hparams, train, 1, 1) |
| 51 | + |
| 52 | + |
| 53 | +def residual_module1_sep(x, hparams, train): |
| 54 | + return residual_module(x, hparams, train, 1, 0) |
| 55 | + |
| 56 | + |
| 57 | +def residual_module2(x, hparams, train): |
| 58 | + return residual_module(x, hparams, train, 2, 1) |
| 59 | + |
| 60 | + |
| 61 | +def residual_module2_sep(x, hparams, train): |
| 62 | + return residual_module(x, hparams, train, 2, 0) |
| 63 | + |
| 64 | + |
| 65 | +def residual_module3(x, hparams, train): |
| 66 | + return residual_module(x, hparams, train, 3, 1) |
| 67 | + |
| 68 | + |
| 69 | +def residual_module3_sep(x, hparams, train): |
| 70 | + return residual_module(x, hparams, train, 3, 0) |
| 71 | + |
| 72 | + |
| 73 | +def norm_module(x, hparams, train): |
| 74 | + del train # Unused. |
| 75 | + return common_layers.layer_norm(x, hparams.hidden_size, name="norm_module") |
| 76 | + |
| 77 | + |
| 78 | +def identity_module(x, hparams, train): |
| 79 | + del hparams, train # Unused. |
| 80 | + return x |
| 81 | + |
| 82 | + |
| 83 | +def run_modules(blocks, cur, hparams, train, dp): |
| 84 | + """Run blocks in parallel using dp as data_parallelism.""" |
| 85 | + assert len(blocks) % dp.n == 0 |
| 86 | + res = [] |
| 87 | + for i in xrange(len(blocks) // dp.n): |
| 88 | + res.extend(dp(blocks[i * dp.n:(i + 1) * dp.n], cur, hparams, train)) |
| 89 | + return res |
| 90 | + |
| 91 | + |
| 92 | +@registry.register_model |
| 93 | +class BlueNet(t2t_model.T2TModel): |
| 94 | + |
| 95 | + def model_fn_body_sharded(self, sharded_features, train): |
| 96 | + dp = self._data_parallelism |
| 97 | + dp._reuse = False # pylint:disable=protected-access |
| 98 | + hparams = self._hparams |
| 99 | + blocks = [identity_module, norm_module, |
| 100 | + residual_module1, residual_module1_sep, |
| 101 | + residual_module2, residual_module2_sep, |
| 102 | + residual_module3, residual_module3_sep] |
| 103 | + inputs = sharded_features["inputs"] |
| 104 | + |
| 105 | + cur = tf.concat(inputs, axis=0) |
| 106 | + cur_shape = cur.get_shape() |
| 107 | + for i in xrange(hparams.num_hidden_layers): |
| 108 | + with tf.variable_scope("layer_%d" % i): |
| 109 | + processed = run_modules(blocks, cur, hparams, train, dp) |
| 110 | + cur = common_layers.shakeshake(processed) |
| 111 | + cur.set_shape(cur_shape) |
| 112 | + |
| 113 | + return list(tf.split(cur, len(inputs), axis=0)), 0.0 |
| 114 | + |
| 115 | + |
| 116 | +@registry.register_hparams |
| 117 | +def bluenet_base(): |
| 118 | + """Set of hyperparameters.""" |
| 119 | + hparams = common_hparams.basic_params1() |
| 120 | + hparams.batch_size = 4096 |
| 121 | + hparams.hidden_size = 768 |
| 122 | + hparams.dropout = 0.2 |
| 123 | + hparams.symbol_dropout = 0.2 |
| 124 | + hparams.label_smoothing = 0.1 |
| 125 | + hparams.clip_grad_norm = 2.0 |
| 126 | + hparams.num_hidden_layers = 8 |
| 127 | + hparams.kernel_height = 3 |
| 128 | + hparams.kernel_width = 3 |
| 129 | + hparams.learning_rate_decay_scheme = "exp50k" |
| 130 | + hparams.learning_rate = 0.05 |
| 131 | + hparams.learning_rate_warmup_steps = 3000 |
| 132 | + hparams.initializer_gain = 1.0 |
| 133 | + hparams.weight_decay = 3.0 |
| 134 | + hparams.num_sampled_classes = 0 |
| 135 | + hparams.sampling_method = "argmax" |
| 136 | + hparams.optimizer_adam_epsilon = 1e-6 |
| 137 | + hparams.optimizer_adam_beta1 = 0.85 |
| 138 | + hparams.optimizer_adam_beta2 = 0.997 |
| 139 | + hparams.add_hparam("imagenet_use_2d", True) |
| 140 | + return hparams |
| 141 | + |
| 142 | + |
| 143 | +@registry.register_hparams |
| 144 | +def bluenet_tiny(): |
| 145 | + hparams = bluenet_base() |
| 146 | + hparams.batch_size = 1024 |
| 147 | + hparams.hidden_size = 128 |
| 148 | + hparams.num_hidden_layers = 4 |
| 149 | + hparams.learning_rate_decay_scheme = "none" |
| 150 | + return hparams |
0 commit comments