uisrnn.arguments
Arguments for UISRNN.
1# Copyright 2018 Google LLC 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# https://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"""Arguments for UISRNN.""" 15 16import argparse 17 18_DEFAULT_OBSERVATION_DIM = 256 19 20 21def str2bool(value): 22 """A function to convert string to bool value.""" 23 if value.lower() in {'yes', 'true', 't', 'y', '1'}: 24 return True 25 if value.lower() in {'no', 'false', 'f', 'n', '0'}: 26 return False 27 raise argparse.ArgumentTypeError('Boolean value expected.') 28 29 30def parse_arguments(): 31 """Parse arguments. 32 33 Returns: 34 A tuple of: 35 36 - `model_args`: model arguments 37 - `training_args`: training arguments 38 - `inference_args`: inference arguments 39 """ 40 # model configurations 41 model_parser = argparse.ArgumentParser( 42 description='Model configurations.', add_help=False) 43 44 model_parser.add_argument( 45 '--observation_dim', 46 default=_DEFAULT_OBSERVATION_DIM, 47 type=int, 48 help='The dimension of the embeddings (e.g. d-vectors).') 49 50 model_parser.add_argument( 51 '--rnn_hidden_size', 52 default=512, 53 type=int, 54 help='The number of nodes for each RNN layer.') 55 model_parser.add_argument( 56 '--rnn_depth', 57 default=1, 58 type=int, 59 help='The number of RNN layers.') 60 model_parser.add_argument( 61 '--rnn_dropout', 62 default=0.2, 63 type=float, 64 help='The dropout rate for all RNN layers.') 65 model_parser.add_argument( 66 '--transition_bias', 67 default=None, 68 type=float, 69 help='The value of p0, corresponding to Eq. (6) in the ' 70 'paper. If the value is given, we will fix to this value. If the ' 71 'value is None, we will estimate it from training data ' 72 'using Eq. (13) in the paper.') 73 model_parser.add_argument( 74 '--crp_alpha', 75 default=1.0, 76 type=float, 77 help='The value of alpha for the Chinese restaurant process (CRP), ' 78 'corresponding to Eq. (7) in the paper. In this open source ' 79 'implementation, currently we only support using a given value ' 80 'of crp_alpha.') 81 model_parser.add_argument( 82 '--sigma2', 83 default=None, 84 type=float, 85 help='The value of sigma squared, corresponding to Eq. (11) in the ' 86 'paper. If the value is given, we will fix to this value. If the ' 87 'value is None, we will estimate it from training data.') 88 model_parser.add_argument( 89 '--verbosity', 90 default=3, 91 type=int, 92 help='How verbose will the logging information be. Higher value ' 93 'represents more verbose information. A general guideline: ' 94 '0 for fatals; 1 for errors; 2 for finishing important steps; ' 95 '3 for finishing less important steps; 4 or above for debugging ' 96 'information.') 97 model_parser.add_argument( 98 '--enable_cuda', 99 default=True, 100 type=str2bool, 101 help='Whether we should use CUDA if it is avaiable. If False, we will ' 102 'always use CPU.') 103 104 # training configurations 105 training_parser = argparse.ArgumentParser( 106 description='Training configurations.', add_help=False) 107 108 training_parser.add_argument( 109 '--optimizer', 110 '-o', 111 default='adam', 112 choices=['adam'], 113 help='The optimizer for training.') 114 training_parser.add_argument( 115 '--learning_rate', 116 '-l', 117 default=1e-3, 118 type=float, 119 help='The leaning rate for training.') 120 training_parser.add_argument( 121 '--train_iteration', 122 '-t', 123 default=20000, 124 type=int, 125 help='The total number of training iterations.') 126 training_parser.add_argument( 127 '--batch_size', 128 '-b', 129 default=10, 130 type=int, 131 help='The batch size for training.') 132 training_parser.add_argument( 133 '--num_permutations', 134 default=10, 135 type=int, 136 help='The number of permutations per utterance sampled in the training ' 137 'data.') 138 training_parser.add_argument( 139 '--sigma_alpha', 140 default=1.0, 141 type=float, 142 help='The inverse gamma shape for estimating sigma2. This value is only ' 143 'meaningful when sigma2 is not given, and estimated from data.') 144 training_parser.add_argument( 145 '--sigma_beta', 146 default=1.0, 147 type=float, 148 help='The inverse gamma scale for estimating sigma2. This value is only ' 149 'meaningful when sigma2 is not given, and estimated from data.') 150 training_parser.add_argument( 151 '--regularization_weight', 152 '-r', 153 default=1e-5, 154 type=float, 155 help='The network regularization multiplicative.') 156 training_parser.add_argument( 157 '--grad_max_norm', 158 default=5.0, 159 type=float, 160 help='Max norm of the gradient.') 161 training_parser.add_argument( 162 '--enforce_cluster_id_uniqueness', 163 default=True, 164 type=str2bool, 165 help='Whether to enforce cluster ID uniqueness across different ' 166 'training sequences. Only effective when the first input to fit() ' 167 'is a list of sequences. In general, assume the cluster IDs for two ' 168 'sequences are [a, b] and [a, c]. If the `a` from the two sequences ' 169 'are not the same label, then this arg should be True.') 170 171 # inference configurations 172 inference_parser = argparse.ArgumentParser( 173 description='Inference configurations.', add_help=False) 174 175 inference_parser.add_argument( 176 '--beam_size', 177 '-s', 178 default=10, 179 type=int, 180 help='The beam search size for inference.') 181 inference_parser.add_argument( 182 '--look_ahead', 183 default=1, 184 type=int, 185 help='The number of look ahead steps during inference.') 186 inference_parser.add_argument( 187 '--test_iteration', 188 default=2, 189 type=int, 190 help='During inference, we concatenate M duplicates of the test ' 191 'sequence, and run inference on this concatenated sequence. ' 192 'Then we return the inference results on the last duplicate as the ' 193 'final prediction for the test sequence.') 194 195 # a super parser for sanity checks 196 super_parser = argparse.ArgumentParser( 197 parents=[model_parser, training_parser, inference_parser]) 198 199 # get arguments 200 super_parser.parse_args() 201 model_args, _ = model_parser.parse_known_args() 202 training_args, _ = training_parser.parse_known_args() 203 inference_args, _ = inference_parser.parse_known_args() 204 205 return (model_args, training_args, inference_args)
def
str2bool(value):
22def str2bool(value): 23 """A function to convert string to bool value.""" 24 if value.lower() in {'yes', 'true', 't', 'y', '1'}: 25 return True 26 if value.lower() in {'no', 'false', 'f', 'n', '0'}: 27 return False 28 raise argparse.ArgumentTypeError('Boolean value expected.')
A function to convert string to bool value.
def
parse_arguments():
31def parse_arguments(): 32 """Parse arguments. 33 34 Returns: 35 A tuple of: 36 37 - `model_args`: model arguments 38 - `training_args`: training arguments 39 - `inference_args`: inference arguments 40 """ 41 # model configurations 42 model_parser = argparse.ArgumentParser( 43 description='Model configurations.', add_help=False) 44 45 model_parser.add_argument( 46 '--observation_dim', 47 default=_DEFAULT_OBSERVATION_DIM, 48 type=int, 49 help='The dimension of the embeddings (e.g. d-vectors).') 50 51 model_parser.add_argument( 52 '--rnn_hidden_size', 53 default=512, 54 type=int, 55 help='The number of nodes for each RNN layer.') 56 model_parser.add_argument( 57 '--rnn_depth', 58 default=1, 59 type=int, 60 help='The number of RNN layers.') 61 model_parser.add_argument( 62 '--rnn_dropout', 63 default=0.2, 64 type=float, 65 help='The dropout rate for all RNN layers.') 66 model_parser.add_argument( 67 '--transition_bias', 68 default=None, 69 type=float, 70 help='The value of p0, corresponding to Eq. (6) in the ' 71 'paper. If the value is given, we will fix to this value. If the ' 72 'value is None, we will estimate it from training data ' 73 'using Eq. (13) in the paper.') 74 model_parser.add_argument( 75 '--crp_alpha', 76 default=1.0, 77 type=float, 78 help='The value of alpha for the Chinese restaurant process (CRP), ' 79 'corresponding to Eq. (7) in the paper. In this open source ' 80 'implementation, currently we only support using a given value ' 81 'of crp_alpha.') 82 model_parser.add_argument( 83 '--sigma2', 84 default=None, 85 type=float, 86 help='The value of sigma squared, corresponding to Eq. (11) in the ' 87 'paper. If the value is given, we will fix to this value. If the ' 88 'value is None, we will estimate it from training data.') 89 model_parser.add_argument( 90 '--verbosity', 91 default=3, 92 type=int, 93 help='How verbose will the logging information be. Higher value ' 94 'represents more verbose information. A general guideline: ' 95 '0 for fatals; 1 for errors; 2 for finishing important steps; ' 96 '3 for finishing less important steps; 4 or above for debugging ' 97 'information.') 98 model_parser.add_argument( 99 '--enable_cuda', 100 default=True, 101 type=str2bool, 102 help='Whether we should use CUDA if it is avaiable. If False, we will ' 103 'always use CPU.') 104 105 # training configurations 106 training_parser = argparse.ArgumentParser( 107 description='Training configurations.', add_help=False) 108 109 training_parser.add_argument( 110 '--optimizer', 111 '-o', 112 default='adam', 113 choices=['adam'], 114 help='The optimizer for training.') 115 training_parser.add_argument( 116 '--learning_rate', 117 '-l', 118 default=1e-3, 119 type=float, 120 help='The leaning rate for training.') 121 training_parser.add_argument( 122 '--train_iteration', 123 '-t', 124 default=20000, 125 type=int, 126 help='The total number of training iterations.') 127 training_parser.add_argument( 128 '--batch_size', 129 '-b', 130 default=10, 131 type=int, 132 help='The batch size for training.') 133 training_parser.add_argument( 134 '--num_permutations', 135 default=10, 136 type=int, 137 help='The number of permutations per utterance sampled in the training ' 138 'data.') 139 training_parser.add_argument( 140 '--sigma_alpha', 141 default=1.0, 142 type=float, 143 help='The inverse gamma shape for estimating sigma2. This value is only ' 144 'meaningful when sigma2 is not given, and estimated from data.') 145 training_parser.add_argument( 146 '--sigma_beta', 147 default=1.0, 148 type=float, 149 help='The inverse gamma scale for estimating sigma2. This value is only ' 150 'meaningful when sigma2 is not given, and estimated from data.') 151 training_parser.add_argument( 152 '--regularization_weight', 153 '-r', 154 default=1e-5, 155 type=float, 156 help='The network regularization multiplicative.') 157 training_parser.add_argument( 158 '--grad_max_norm', 159 default=5.0, 160 type=float, 161 help='Max norm of the gradient.') 162 training_parser.add_argument( 163 '--enforce_cluster_id_uniqueness', 164 default=True, 165 type=str2bool, 166 help='Whether to enforce cluster ID uniqueness across different ' 167 'training sequences. Only effective when the first input to fit() ' 168 'is a list of sequences. In general, assume the cluster IDs for two ' 169 'sequences are [a, b] and [a, c]. If the `a` from the two sequences ' 170 'are not the same label, then this arg should be True.') 171 172 # inference configurations 173 inference_parser = argparse.ArgumentParser( 174 description='Inference configurations.', add_help=False) 175 176 inference_parser.add_argument( 177 '--beam_size', 178 '-s', 179 default=10, 180 type=int, 181 help='The beam search size for inference.') 182 inference_parser.add_argument( 183 '--look_ahead', 184 default=1, 185 type=int, 186 help='The number of look ahead steps during inference.') 187 inference_parser.add_argument( 188 '--test_iteration', 189 default=2, 190 type=int, 191 help='During inference, we concatenate M duplicates of the test ' 192 'sequence, and run inference on this concatenated sequence. ' 193 'Then we return the inference results on the last duplicate as the ' 194 'final prediction for the test sequence.') 195 196 # a super parser for sanity checks 197 super_parser = argparse.ArgumentParser( 198 parents=[model_parser, training_parser, inference_parser]) 199 200 # get arguments 201 super_parser.parse_args() 202 model_args, _ = model_parser.parse_known_args() 203 training_args, _ = training_parser.parse_known_args() 204 inference_args, _ = inference_parser.parse_known_args() 205 206 return (model_args, training_args, inference_args)
Parse arguments.
Returns: A tuple of:
- `model_args`: model arguments
- `training_args`: training arguments
- `inference_args`: inference arguments