| 93 | // ---------------------------------------------------------------------------------------- |
| 94 | |
| 95 | int main(int argc, char** argv) |
| 96 | { |
| 97 | try |
| 98 | { |
| 99 | command_line_parser parser; |
| 100 | parser.add_option("train", "Train a small transformer on the built-in Shakespeare text"); |
| 101 | parser.add_option("generate", "Generate text from a previously trained model (needs shakespeare_prompt)"); |
| 102 | parser.add_option("learning-rate", "Set the learning rate for training (default: 1e-4)", 1); |
| 103 | parser.add_option("batch-size", "Set the mini-batch size for training (default: 64)", 1); |
| 104 | parser.add_option("generation-length", "Set the length of generated text (default: 400)", 1); |
| 105 | parser.add_option("alpha", "Set the weight decay for Adam optimizer (default: 0.004)", 1); |
| 106 | parser.add_option("beta1", "Set the first moment coefficient (default: 0.9)", 1); |
| 107 | parser.add_option("beta2", "Set the second moment coefficient (default: 0.999)", 1); |
| 108 | parser.add_option("max-samples", "Set the maximum number of training samples (default: 50000)", 1); |
| 109 | parser.add_option("shuffle", "Shuffle training sequences and labels before training (default: false)"); |
| 110 | parser.parse(argc, argv); |
| 111 | |
| 112 | if (parser.number_of_arguments() == 0 && !parser.option("train") && !parser.option("generate")) |
| 113 | { |
| 114 | parser.print_options(); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | // Default values |
| 119 | const double learning_rate = get_option(parser, "learning-rate", 1e-4); |
| 120 | const long batch_size = get_option(parser, "batch-size", 64); |
| 121 | const int generation_length = get_option(parser, "generation-length", 400); |
| 122 | const double alpha = get_option(parser, "alpha", 0.004); // Initial learning rate for Adam |
| 123 | const double beta1 = get_option(parser, "beta1", 0.9); // Decay rate for the first moment estimate |
| 124 | const double beta2 = get_option(parser, "beta2", 0.999); // Decay rate for the second moment estimate |
| 125 | const size_t max_samples = get_option(parser, "max-samples",50000); // Default maximum number of training samples |
| 126 | |
| 127 | // We define a minimal config for demonstration |
| 128 | const long vocab_size = MAX_TOKEN_ID + 1 + 1; // 256 for chars + 1 pad token |
| 129 | const long num_layers = 3; |
| 130 | const long num_heads = 4; |
| 131 | const long embedding_dim = 64; |
| 132 | const long max_seq_len = 80; // a small sequence length for the example |
| 133 | const bool use_squeezing = false; |
| 134 | |
| 135 | using my_transformer_cfg = transformer::transformer_config< |
| 136 | vocab_size, |
| 137 | num_layers, |
| 138 | num_heads, |
| 139 | embedding_dim, |
| 140 | max_seq_len, |
| 141 | use_squeezing, |
| 142 | gelu, |
| 143 | dropout_10 |
| 144 | >; |
| 145 | |
| 146 | // For GPU usage (if any), set gpus = {0} for a single GPU, etc. |
| 147 | std::vector<int> gpus{ 0 }; |
| 148 | |
| 149 | // The model file to store or load |
| 150 | const std::string model_file = "shakespeare_lm_char_model.dat"; |
| 151 | |
| 152 | // ---------------------------------------------------------------------------------------- |
nothing calls this directly
no test coverage detected