def main(args): features, targets = generate_synthetic_data(args.model_type, args.num_samples) # split train/test sets x_train, x_val, y_train, y_val = train_test_split(features, targets, test_size=0.2) db_train = tf.data.Dataset.from_tensor_slices( (x_train, y_train)).batch(args.batch_size_train) db_val = tf.data.Dataset.from_tensor_slices( (x_val, y_val)).batch(args.batch_size_eval) if args.model_type == 'MLP': model = MLP(num_inputs=Constants._MLP_NUM_FEATURES, num_layers=Constants._MLP_NUM_LAYERS, num_dims=Constants._MLP_NUM_DIMS, num_outputs=Constants._NUM_TARGETS, dropout_rate=args.dropout) elif args.model_type == 'TCN': model = TCN(nb_filters=Constants._TCN_NUM_FILTERS, kernel_size=Constants._TCN_KERNEL_SIZE, nb_stacks=Constants._TCN_NUM_STACK, dilations=Constants._TCN_DIALATIONS, padding=Constants._TCN_PADDING, dropout_rate=args.lr) criteon = keras.losses.MeanSquaredError() optimizer = keras.optimizers.Adam(learning_rate=args.lr) for epoch in range(args.max_epoch): for step, (x, y) in enumerate(db_train): with tf.GradientTape() as tape: logits = model(x) loss = criteon(y, logits) grads = tape.gradient(loss, model.trainable_variables) optimizer.apply_gradients(zip(grads, model.trainable_variables)) if step % 100 == 0: print('Epoch: {}, Step: {}/{}, Loss: {}'.format( epoch, step, int(x_train.shape[0] / args.batch_size_train), loss)) # Perform inference and measure the speed every epoch start_time = time.time() for _, (x, _) in enumerate(db_val): _ = model.predict(x) end_time = time.time() print("Inference speed: {} samples/s\n".format( x_val.shape[0] / (end_time - start_time)))
for x, y in tqdm(train_loader): padded_text, lens, mask = seq_indexer.add_padding_tensor( x, gpu=args.gpu) label = label_indexer.instance2tensor(y, gpu=args.gpu) y = model(padded_text, lens, mask) loss = criterion(y, label) loss.backward() optimizer.step() train_loss += loss.item() optimizer.zero_grad() print('epoch', epoch + 1, ' loss: ', train_loss / (len(dataset.train_word) / args.batch_size)) pred = model.predict(dataset.train_word, embedding_indexer=seq_indexer, label_indexer=label_indexer, batch_size=args.batch_size) train_score = eval.get_socre(pred, dataset.train_label) pred = model.predict(dataset.dev_word, embedding_indexer=seq_indexer, label_indexer=label_indexer, batch_size=args.batch_size) dev_score = eval.get_socre(pred, dataset.dev_label) pred = model.predict(dataset.test_word, embedding_indexer=seq_indexer, label_indexer=label_indexer, batch_size=args.batch_size) test_score = eval.get_socre(pred, dataset.test_label)