Coding 6

[Python] HackerRank - Capitalize!

난이도: Easy 이름이 입력으로 들어오면 대문자화를 잘 시키는 것이다. chris alan → Chris Alan # solve 함수를 작성해야 한다. def solve(s): ... if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') s = input() result = solve(s) fptr.write(result + '\n') fptr.close() # 나의 코드 def solve(s): lst = list() for word in s.split(' '): lst.append(word.capitalize()) return ' '.join(lst) # 1. 입력 string을 띄어쓰기 한번을 기준으로 word 단위로 쪼개주..

Coding 2023.07.23

[Python] torch.nn.CrossEntropyLoss 에서 ignore_index

상황는 이렇다. Segmentation task를 진행하다가 CrossentropyLoss에 ground truth label (mask)를 넣어야 하는 상황이었는데, 원하는 segmentation class 개수는 3개(물, 하늘, 장애물)이지만 mask에는 총 4개의 label이 달려있었던 상황. 그래서 Data 설명을 읽어봤다. '4'라고 분류된 픽셀들은 물체/물/하늘 사이 boundary에 해당해서 모호한 픽셀로 남겨놨던 것이다. 학습에 사용되면 안되는 픽셀들이기 때문에 '4'라고 분류된 픽셀들은 포함시키면 안된다. 그래서 이 픽셀들을 어떡하지? 그냥 '0'이나 '1' 혹은 '2'로 labeling 후처리를 해야하나... 그러기엔 픽셀들이 너무 애매한걸? 이러고 있다가 동료가 찾아준 아주 효과적인..

Coding 2023.04.18

[Python] torch.scatter_ 이해하기

우선 scatter와 scatter_ 메소드를 비교하면 다음과 같다: torch.scatter는 out-of-place 버전, torch.scatter_는 in-place 버전이다. 즉, torch.scatter는 메서드를 실행시키면 즉시 scatter가 tensor에 적용된다. 여기서 각 argument들이 의미하는 바는 다음과 같다. dim: scatter 할 기준이 되는 축. '0'이면 행방향 (아래 방향), '1'이면 열방향 (오른쪽 방향). index (LongTensor): 흩뿌릴 element들의 index. 즉, 어떤 숫자를 어떤 규칙으로 옮길지 결정하는 tensor. src: 어떤 숫자들이 옮겨지는지 그 후보를 담은 소스 tensor. 그렇다면 scatter가 어떤 효과를 내는지 예시를 ..

Coding 2023.04.18

[Python] string split과 rsplit method 차이

Python에는 string을 분할하기 위한 .split 과 .rsplit 이라는 method가 있다. 문법은 똑같다. string.split(separator, maxsplit) string.rsplit(separator, maxsplit) separator: 분할을 하고 싶은 기준 string. maxsplit: 몇번 split을 할지 정하는 integer. 기본은 -1로 설정되어 있어 가능한 분할은 전부 실행한다. 예를 들어, "1"이면 한 번만 분할을 하여 2개의 element가 list에 저장된다. 참고로 output은 분할된 word chunk들이 element로 저장된 list이다. 둘은 쓰임새가 아주 비슷한데 한 가지 차이가 있다. str1 = "hello hi I am a boy" ## ..

Coding 2023.04.16

[Python] 클래스 상속(Class inheritance) 그리고 Pytorch 모델에서의 해석.

1. 클래스 상속 (Class inheritance) 클래스는 다른 클래스의 메서드등을 상속받아 그대로 사용할 수 있다. Parent class가 상속을 해주는 클래스, Child class가 상속을 받는 클래스이다. class Person: def __init__(self, fname, lname): # init으로 fname, lanme을 받아 firstname, lastname에 각각 저장함. self.firstname = fname self.lastname = lname def printname(self): # 저장한 firstname, lastname을 출력함. print(self.firstname, self.lastname) x = Person("John", "Doe") x.printname(..

Coding 2023.02.24

torch.nn.utils.prune에서 L1Unstructured와 l1_unstructured의 차이

torch.nn.utils.prune에 보면 L1 norm + unstructured의 방식으로 prune을 하는 방식이 두 가지나 있다. torch.nn.utils.prune.L1Unstructured torch.nn.utils.prune.l1_unstructured 두 방식에는 어떤 차이가 있는 걸까? 1. L1Unstructured 공식 문서에서도 볼 수 있듯이 class로 구성되어 있다. 그리고 class method들로 apply, apply_mask, prune이 있다. import torch import torch.nn as nn import torch.nn.utils.prune as prune conv1 = nn.Conv2d(2, 3, 3) Output: Parameter containin..

Coding 2023.01.25