'Python' 카테고리의 글 목록 (3 Page) — 뚝딱이

Python

Python/numpy & Pytorch

[Pytorch] Tensor shuffle, 텐서 랜덤 섞기

randperm(int) 입력된 숫자를 임의로 섞어주는 함수 torch.randperm(10) tensor([9, 8, 2, 1, 3, 4, 5, 6, 7, 0]) Tensor 행 섞기 tensor의 행이 랜덤으로 섞이는 코드 a = torch.rand(3,3) a = a[torch.randperm(a.size()[0])] tensor([[0.5326, 0.3624, 0.3423], [0.9065, 0.8168, 0.5219], [0.9516, 0.3635, 0.8481]]) tensor([[0.9065, 0.8168, 0.5219], [0.9516, 0.3635, 0.8481], [0.5326, 0.3624, 0.3423]]) Tensor 열 섞기 a = torch.rand(3,3) a = a[:,to..

Python/numpy & Pytorch

[Pytorch] tensor 합치기는 방법 cat(), stack()

'+' 연산자 list list에서는 '+' 연산자를 쓰면 list가 합쳐진다. x = [1,2] x2 = [3,4] x+x2 [1, 2, 3, 4] Tensor tensor는 합쳐지지 않고 각 원소마다 더해진다. 이는 같은 차원끼리 더하거나 한 차원이 1일 때만 가능함 x = torch.randint(0, 10,(3,1)) x2 = torch.randint(0, 10,(3,1)) x3 = torch.randint(0, 10,(1,1)) x, x2, x+x2, x+x3 tensor([[4], [2], [1]]) tensor([[2], [1], [2]]) tensor([[6], [3], [3]]) tensor([[5], [3], [2]]) x = torch.randint(0, 10,(3,1)) x2 = t..

Python/numpy & Pytorch

[Pytorch] torch 설정, 랜덤 tensor 생성

Pythorch를 모듈을 사용하여 tensor 객체 만드는 방법 tensor numpy의 array와 같지만 GPU 계산에서 사용됨 설정 행렬 사용자가 원하는 값을 넣어 행렬을 만듦 Zeros(size) 0이 들어있는 행렬을 return 함 x = torch.zeros(5, 3) x tensor([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]) x = torch.zeros(2, 5, dtype=torch.bool) x tensor([[False, False, False, False, False], [False, False, False, False, False]]) Ones(size) 1이 들어있는 행렬을 returng 함 x = torch.ones(3,2) x tensor..

Python/이론, 기초

[Python] Set 정리 사용법

Set() python은 집합을 표현할 수 있는 자료형이며 중복이 허용되지 않음 set 자료형에 index로 접근할 수 없어 list나 tuple로 변환 후 접근해야함 s1 = set([1, 2, 3, 4, 5, 5, 0, 4, 5]) print(s1) s1 = set('hello world') print(s1) list와 문장을 넣어서 set을 만들 수 있다. 집합이기 때문에 중복된 수가 들어오면 제거되고 하나만 들어간다. Set 연산 교집합 두 set에 공통으로 들어있는 원소 s1 = set([1,2,3,5,7,9]) s2 = set([1,2,4,6,8,9]) print(s1 & s2) print(s1.intersection(s2)) 합집합 s1 = set([1,2,3,5,7,9]) s2 = set(..

Python/이론, 기초

[Python] 파이썬 any(), all()

파이썬의 내장 함수로 iterable 한 객체를 받아 조건에 맞으면 True 맞지 않으면 False를 출력한다. any() 하나라도 True가 있다면 True를 반환(OR과 유사함) any([False, True, False]) any([False, False, False]) any([True, True, True]) all() 모두 True일 때, True를 반환(And와 유사함) all([False, True, False]) all([False, False, False]) all([True, True, True]) for for 문과 함께 사용하여 반복문 전체를 하나의 list로 볼 수 있다. cur = 5 temp = [1,3,2,2,4] if any(cur

Python/구현

[Python] 최대공약수, 최소공배수 구현

최대공약수 GCD (Greatest Common Divisor) 두 수 이상의 공통의 약수 중 최대인 수 위의 경우 72와 90의 최대공약수는 18이다. gcd = 1이라면 서로소 관계에 있다고 표현한다. 최대공배수 LCM (Least Common Multiple) 두 수 이상의 수들의 공통인 배수 중 최소인 수 위의 경우 24, 30의 최대공배수는 120이 나온다. 구현 최소공약수 def solution(a, b): for i in range(min(a,b),0,-1): if a%i == 0 and b%i == 0: return i 최대공배수 def solution(a, b): for i in range(max(a,b),(a*b)+1): if i%a == 0 and i%b == 0: return i 구..

Python/이론, 기초

[Python] enumerate, map 값 확인하기

반복문을 돌리지 않고 enumerate, map을 print하면 내가 원하는 결과가 나오지 않는다. print(enumerate(citations, start=1)) print(map(min, enumerate(citations, start=1))) 이럴때는 list를 사용하여 안의 값을 볼 수 있다. print(list(enumerate(citations, start=1))) print(list(map(min, enumerate(citations, start=1))))

Python/이론, 기초

[Python] for문에서 pop 사용하기

for 문 안에서 pop을 사용하면 내가 생각한 대로 작동하지 않을 수 있다. 문제 a = [1,2,3,4] for i in a: print(a.pop(0)) print(a) 1, 2, 3, 4가 출력되고 a에 아무것도 남지 않기를 바랐는데 다음과 같은 결과가 나왔다. 왜 이런 결과가 나오게 됐을까? for문은 현재 a의 index를 따르기 때문이다. 해결 나같은 경우에는 list를 copy 하여 사용한다. a = [1,2,3,4] b = a.copy() for i in b: print(a.pop(0)) print(a) 이 방법이 아니더라도 자신의 쓰임에 맞게 쓰면 된다.

파송송
'Python' 카테고리의 글 목록 (3 Page)