improving efficiency of train_dataset lua function