Skip to content

Commit

Permalink
bool arguments fix
Browse files Browse the repository at this point in the history
  • Loading branch information
saransh-mehta committed Jun 13, 2020
1 parent 889f978 commit 2a6db66
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@
" --eval_batch_size 16 \\\n",
" --grad_accumulation_steps 2 \\\n",
" --log_per_updates 250 \\\n",
" --save_per_updates 16000 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 324 \\\n",
" --silent True "
" --save_per_updates 16000 \\\n",
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/entailment_detection/entailment_snli.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@
" --eval_batch_size 64 \\\n",
" --grad_accumulation_steps 1 \\\n",
" --log_per_updates 100 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 128 \\\n",
" --silent True "
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent"
]
},
{
Expand Down
7 changes: 3 additions & 4 deletions examples/intent_ner_fragment/intent_ner_fragment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"!python ../../train.py \\\n",
" --data_dir '../../data/bert-base-uncased_prepared_data' \\\n",
" --task_file 'tasks_file_snips.yml' \\\n",
Expand All @@ -178,10 +177,10 @@
" --eval_batch_size 32 \\\n",
" --grad_accumulation_steps 2 \\\n",
" --log_per_updates 50 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 50 \\\n",
" --silent True "
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent "
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/ner_pos_tagging/ner_pos_tagging_conll.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
" --eval_batch_size 32 \\\n",
" --grad_accumulation_steps 1 \\\n",
" --log_per_updates 50 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 50 \\\n",
" --silent True "
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/query_correctness/query_correctness.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
" --eval_batch_size 32 \\\n",
" --grad_accumulation_steps 1 \\\n",
" --log_per_updates 20 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 50 \\\n",
" --silent True"
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions examples/query_pair_similarity/query_similarity_qqp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
" --eval_batch_size 16 \\\n",
" --grad_accumulation_steps 2 \\\n",
" --log_per_updates 50 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 200 \\\n",
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent True "
]
},
Expand Down
6 changes: 3 additions & 3 deletions examples/query_type_detection/query_type_detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
" --eval_batch_size 64 \\\n",
" --grad_accumulation_steps 1 \\\n",
" --log_per_updates 100 \\\n",
" --eval_while_train True \\\n",
" --test_while_train True \\\n",
" --max_seq_len 60 \\\n",
" --silent True"
" --eval_while_train \\\n",
" --test_while_train \\\n",
" --silent"
]
},
{
Expand Down
5 changes: 2 additions & 3 deletions models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,8 @@ def update_step(self, batchMetaData, batchData):
if self.lossClassList[taskId] and (target is not None):
self.taskLoss = self.lossClassList[taskId](logits, target, attnMasks=modelInputs[2])
#tensorboard details
if self.params['tensorboard']:
self.tbTaskId = taskId
self.tbTaskLoss = self.taskLoss.item()
self.tbTaskId = taskId
self.tbTaskLoss = self.taskLoss.item()
taskLoss = self.taskLoss / self.params['grad_accumulation_steps']
taskLoss.backward()
self.accumulatedStep += 1
Expand Down
48 changes: 22 additions & 26 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,51 +29,48 @@ def make_arguments(parser):
help = 'path to save the model')
parser.add_argument('--epochs', type = int, required=True,
help = 'number of epochs to train')
parser.add_argument('--finetune', type = bool, default= False,
help = "If only the shared model is to be loaded with saved pre-trained multi-task model.\
In this case, you can specify your own tasks with task file and use the pre-trained shared model\
to finetune upon.")
parser.add_argument('--freeze_shared_model', type = bool, default=False,
parser.add_argument('--freeze_shared_model', default=False, action='store_true',
help = "True to freeze the loaded pre-trained shared model and only finetune task specific headers")
parser.add_argument('--train_batch_size', type = int, default=8,
help='batch size to use for training')
parser.add_argument('--eval_batch_size', type = int, default = 32,
help = "batch size to use during evaluation")
parser.add_argument('--eval_while_train', type = bool, default= True,
help = "if evaluation on dev set is required during training.")
parser.add_argument('--test_while_train', type = bool, default = True,
help = "if evaluation on test set is required during training.")
parser.add_argument('--grad_accumulation_steps', type =int, default = 1,
help = "number of steps to accumulate gradients before update")
parser.add_argument('--num_of_warmup_steps', type=int, default = 0,
help = "warm-up value for scheduler")
parser.add_argument('--grad_clip_value', type = float, default=1.0,
help = "gradient clipping value to avoid gradient overflowing" )
parser.add_argument('--debug_mode', default = False, type = bool,
help = "record logs for debugging if True")
parser.add_argument('--log_file', default='multi_task_logs.log', type = str,
help = "name of log file to store")
parser.add_argument('--log_per_updates', default = 10, type = int,
help = "number of steps after which to log loss")
parser.add_argument('--silent', type = bool, default = True,
help = "Only write logs to file if True")
parser.add_argument('--seed', default=42, type = int,
help = "seed to set for modules")
parser.add_argument('--max_seq_len', default=128, type =int,
help = "max seq length used for model at time of data preparation")
parser.add_argument('--tensorboard', default=True, type = bool,
help = "To create tensorboard logs")
parser.add_argument('--save_per_updates', default = 0, type = int,
help = "to keep saving model after this number of updates")
parser.add_argument('--limit_save', default = 10, type = int,
help = "max number recent checkpoints to keep saved")
parser.add_argument('--load_saved_model', type=str, default=None,
help="path to the saved model in case of loading from saved")
parser.add_argument('--resume_train', type=bool, default=False,
help="True for resuming training from a saved model")
parser.add_argument('--eval_while_train', default = False, action = 'store_true',
help = "if evaluation on dev set is required during training.")
parser.add_argument('--test_while_train', default=False, action = 'store_true',
help = "if evaluation on test set is required during training.")
parser.add_argument('--resume_train', default=False, action = 'store_true',
help="Set for resuming training from a saved model")
parser.add_argument('--finetune', default= False, action = 'store_true',
help = "If only the shared model is to be loaded with saved pre-trained multi-task model.\
In this case, you can specify your own tasks with task file and use the pre-trained shared model\
to finetune upon.")
parser.add_argument('--debug_mode', default = False, action = 'store_true',
help = "record logs for debugging if True")
parser.add_argument('--silent', default = False, action = 'store_true',
help = "Only write logs to file if True")
return parser


parser = argparse.ArgumentParser()
parser = make_arguments(parser)
args = parser.parse_args()
Expand Down Expand Up @@ -193,9 +190,8 @@ def main():
allParams['gpu'] = torch.cuda.is_available()
logger.info('task parameters:\n {}'.format(taskParams.taskDetails))

if args.tensorboard:
tensorboard = SummaryWriter(log_dir = os.path.join(logDir, 'tb_logs'))
logger.info("Tensorboard writing at {}".format(os.path.join(logDir, 'tb_logs')))
tensorboard = SummaryWriter(log_dir = os.path.join(logDir, 'tb_logs'))
logger.info("Tensorboard writing at {}".format(os.path.join(logDir, 'tb_logs')))

# making handlers for train
logger.info("Creating data handlers for training...")
Expand Down Expand Up @@ -268,11 +264,11 @@ def main():
taskName,
avgLoss,
model.taskLoss.item()))
if args.tensorboard:
tensorboard.add_scalar('train/avg_loss', avgLoss, global_step= model.globalStep)
tensorboard.add_scalar('train/{}_loss'.format(taskName),
model.taskLoss.item(),
global_step=model.globalStep)

tensorboard.add_scalar('train/avg_loss', avgLoss, global_step= model.globalStep)
tensorboard.add_scalar('train/{}_loss'.format(taskName),
model.taskLoss.item(),
global_step=model.globalStep)

if args.save_per_updates > 0 and ( (model.globalStep+1) % args.save_per_updates)==0 and (model.accumulatedStep+1==args.grad_accumulation_steps):
savePath = os.path.join(args.out_dir, 'multi_task_model_{}_{}.pt'.format(epoch,
Expand Down
31 changes: 0 additions & 31 deletions utils/tranform_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,37 +672,6 @@ def msmarco_answerability_detection_to_tsv(dataDir, readFile, wrtDir, transParam
devDf.to_csv(os.path.join(wrtDir, 'msmarco_answerability_test.tsv'), sep='\t', index=False, header=False)
print('Test file written at: ', os.path.join(wrtDir, 'msmarco_answerability_test.tsv'))

def query_correctness_to_tsv(dataDir, readFile, wrtDir, transParamDict, isTrainFile=False):

"""
- Query correctness transformed file
For using this transform function, set ``transform_func`` : **query_correctness_to_tsv** in transform file.
Args:
dataDir (:obj:`str`) : Path to the directory where the raw data files to be read are present..
readFile (:obj:`str`) : This is the file which is currently being read and transformed by the function.
wrtDir (:obj:`str`) : Path to the directory where to save the transformed tsv files.
transParamDict (:obj:`dict`, defaults to :obj:`None`): Dictionary of function specific parameters. Not required for this transformation function.
"""
print('Making data from file {}'.format(readFile))
df = pd.read_csv(os.path.join(dataDir, readFile), sep='\t', header=None, names = ['query', 'label'])

# we consider anything above 0.6 as structured query (3 or more annotations as structured), and others as non-structured

#df['label'] = [str(lab) for lab in df['label']]
df['label'] = [int(lab>=0.6)for lab in df['label']]

data = [ [str(i), str(row['label']), row['query'] ] for i, row in df.iterrows()]

wrtDf = pd.DataFrame(data, columns = ['uid', 'label', 'query'])

#writing
wrtDf.to_csv(os.path.join(wrtDir, 'query_correctness_{}'.format(readFile)), sep="\t", index=False, header=False)
print('File saved at: ', os.path.join(wrtDir, 'query_correctness_{}'.format(readFile)))

def clinc_out_of_scope_to_tsv(dataDir, readFile, wrtDir, transParamDict, isTrainFile=False):

"""
Expand Down

0 comments on commit 2a6db66

Please sign in to comment.