(data,
max_epoch,
hidden_size=100,
seq_length=100,
batch_size=16,
model_path='model')
| 210 | |
| 211 | |
| 212 | def train(data, |
| 213 | max_epoch, |
| 214 | hidden_size=100, |
| 215 | seq_length=100, |
| 216 | batch_size=16, |
| 217 | model_path='model'): |
| 218 | # SGD with L2 gradient normalization |
| 219 | cuda = device.create_cuda_gpu() |
| 220 | model = CharRNN(data.vocab_size, hidden_size) |
| 221 | model.graph(True, False) |
| 222 | |
| 223 | inputs, labels = None, None |
| 224 | |
| 225 | for epoch in range(max_epoch): |
| 226 | model.train() |
| 227 | train_loss = 0 |
| 228 | for b in tqdm(range(data.num_train_batch)): |
| 229 | batch = data.train_dat[b * batch_size:(b + 1) * batch_size] |
| 230 | inputs, labels = convert(batch, batch_size, seq_length, |
| 231 | data.vocab_size, cuda, inputs, labels) |
| 232 | out, loss = model(inputs, labels) |
| 233 | model.reset_states(cuda) |
| 234 | train_loss += tensor.to_numpy(loss)[0] |
| 235 | |
| 236 | print('\nEpoch %d, train loss is %f' % |
| 237 | (epoch, train_loss / data.num_train_batch / seq_length)) |
| 238 | |
| 239 | evaluate(model, data, batch_size, seq_length, cuda, inputs, labels) |
| 240 | sample(model, data, cuda) |
| 241 | |
| 242 | |
| 243 | if __name__ == '__main__': |
no test coverage detected