11/07/2022

python text read line by line

 refer to code

..

fn = './text.txt'
# Using readlines()
with open(fn, 'r') as fp:
Lines = fp.readlines()
count = 0
for line in Lines:
count += 1
print("{}: {}".format(count, line.strip()))

..


Thank you.

www.marearts.com

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