10/28/2022

OneCycle LR set in Pytorch lightning

 

Add configure_optimizer member function in pytorch lightning model class.

refer to code:


..

def configure_optimizers(self):
optimizer = getattr(torch.optim, self.cfg.optimizer)
self.optimizer = optimizer(self.parameters(), lr=float(self.cfg.lr))

total_bs = int(self.cfg.train_dataloader_len / self.cfg.gpus)

epochs = self.cfg.epochs
self.scheduler = torch.optim.lr_scheduler.OneCycleLR(
self.optimizer, max_lr=self.cfg.lr,
anneal_strategy='linear', div_factor=100,
steps_per_epoch=total_bs, pct_start=(1/self.cfg.epochs),
epochs=epochs)
sched = {
'scheduler': self.scheduler,
'interval': 'step',
}
return [self.optimizer], [sched]

..




Thank you. ๐Ÿ™‡๐Ÿป‍♂️

www.marearts.com

python list reverse

refer to code: 

..

Test_list = [1, 2, 3, 4, 5]
Test_list.reverse()
print(Test_list)  # Output:  [5,4,3,2,1]

..


Thank you.

10/20/2022

re-install nvidia drive (cuda) in ubuntu

command it on terminal

 

..

sudo apt clean
sudo apt update
sudo apt purge nvidia-* 
sudo apt autoremove
sudo apt install -y cuda

..


Thank you.


www.marearts.com

10/14/2022

pytorch lightning set validation interval

 

refer to ex:

..

# default used by the Trainer
trainer = Trainer(val_check_interval=1.0)
# check validation set 4 times during a training epoch
trainer = Trainer(val_check_interval=0.25)
# check validation set every 1000 training batches in the current epoch
trainer = Trainer(val_check_interval=1000)

..


detail is here: https://pytorch-lightning.readthedocs.io/en/stable/common/trainer.html


www.marearts.com


10/13/2022

python load json file

 

..

# importing the module
import json
# Opening JSON file
with open(FileA) as json_file:
data = json.load(json_file)
print("Type:", type(data))
print('length: ', len(data))
print('first item type: ', type(data[0]))

..


Thank you.

www.marearts.com


10/07/2022

python, pandas, to create empty data frame with same header from other df.

 

refer to code.

..

import pandas as pd
data_df = pd.read_csv('train_data.tsv', delimiter='\t')
col = list(data_df.columns)

#make empty pandas with same header
empty_df = pd.DataFrame(columns=col)
print(empty_df)

..


Thank you.

www.marearts.com

10/02/2022

python os mkdir example code

..

if not os.path.exists(output_dir):
    os.mkdir(output_dir)

..


Thank you.

www.marearts.com