Repository navigation
Expand file tree
/
Copy path1-4.py
More file actions
129 lines (105 loc) · 5.45 KB
/
Copy path1-4.py
File metadata and controls
129 lines (105 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import torch
A = torch.randn(3,3)#Noraml의 N 정규분포 즉 평균이 0 분산이 1인 분포
B = torch.rand(3,3)#이건 Uniform 0~1사이 값
A = torch.randn(3,3)
print(torch.abs(A))#absolute(절댓값)
print(torch.sqrt(torch.abs(A)))#sqrt 루트(양의 제곱근)
print(torch.exp(A))#A에 있는 모든 각각의 행,열의 값을 e의 제곱근에 넣음
print(torch.log(torch.abs(A)))#자연로그라서 log_e n형식으로 들어감
print(torch.log(torch.exp(torch.tensor(1))))#자연 로그라서 1
#파이토치는 좀 엄격해서 함수를 쓸때 함수안에는 반드시 torch형태로 만들어진 tensor형의 변수만 사용 가능
print(torch.log10(torch.tensor(10)))#상용로그로 바꿔서 1
print(torch.round(A))#반올림
print(torch.round(A,decimals=2))#소숫점 2째자리까리 반올림
print(torch.floor(A))#내림
print(torch.ceil(A))#올림
print(torch.sin(torch.tensor(torch.pi/6)))# type(torch.pi) <= float형 만약 tensor와 연산하면 tensor로 바꿔줌
print(torch.cos(torch.tensor(torch.pi/3)))
print(torch.tan(torch.tensor(torch.pi/4)))
print(torch.tanh(torch.tensor(-10)))
type(torch.tensor(1)/6)
torch.nan
print(torch.log(torch.tensor(-1)))
print(torch.isnan(torch.tensor([1,2,torch.nan,3,4])))#nan이 어딨냐
print(torch.isinf(torch.tensor([1,2,3,4,torch.inf])))#inf가 어딨냐
A = torch.randn(3,4)
print(torch.max(A))
print(torch.max(A,dim=0))#dim=0은 0번째 차원에서 검사 즉 행의 방향으로 가장 큰 값들을 검사해서 출력
print(torch.max(A,dim=1))#dim=1 방향 즉 열의 방향으로 가장 큰 값들을 검사해서 출력
print(torch.max(A,dim=0,keepdims=True))#keepdims는 말 그대로 차원 보존 0번째 차원 행의 방향으로 보존
print(torch.max(A,dim=1, keepdims=True))#열의 방향으로 차원보존 아까와 다르게 3,1로 만들어짐
print(torch.argmax(A))#indices(어디에 최댓값이 있는지)만 얻을 수 있음
import torch
a = torch.randn(6,1)
print(a)
a_sorted=torch.sort(a,dim=0)#정렬
print(a_sorted)
a_descending= torch.sort(a,dim=0,descending=True)
A = torch.randn(3,4)
print(torch.sum(A))
print(torch.sum(A,dim=1))#열의 방향으로 더함
print(torch.sum(A,dim=1,keepdim=True))#keepsdim으로 차원보존
print(torch.mean(A))
print(torch.mean(A,dim=1))
print(torch.mean(A,dim=1,keepdim=True))
print(torch.std(A))#standard deviation 표준 편차
#reshape
A = torch.randint(1,5,size=(12,))#1부터 5까지의 랜덤 숫자로 12개 만드는 거
B = A.reshape(2,2,3)#2행3열짜리 2개로 만들어라
print(B.ndim)#ndim은 차원의 갯수
import torch
A = torch.arange(20,)
print(A)
print(A.reshape(4,5))#당연히 숫자가 맞아야함 4X5 = 20
print(A.reshape(4,-1).shape)#-1은 알아서 채워라는 뜻 행은 4개로 열은 알아서 채워
a = torch.tensor([1,2,3])
b = torch.tensor([2,2,1])
print(torch.sum(a*b))
a = a.reshape(3,1)
b = b.reshape(3,1)
print(a.transpose(1,0)@b)#transpose(0번째,1번째)차원 자리 바꿔라 이말임 거기다가 @b로 행렬곱 수행
print(a.permute(1,0)@b)#얘도 비슷함permute(0번째에 몇번째 얘 오게 할래,1번째엔 몇번쨰 애 오게 할래) 거의 비슷하게 수행 하지만 3차원부터 좀 달라짐
print(a.T@b)
print(a.t()@b)#이렇게 2개로도 transpose해서 행렬곱 가능
A = torch.randn(4,3,6)
print(A.permute(0,2,1).shape)#(0번째, 2번째, 1번째 가져와) 그러면 (4,6,3)이 됨
print(A.transpose(1,2).shape)#(1번째,2번째) 자리 바꿔 이러면 (4,6,3)이 됨 자리 바꾸는 거여서 (2,1)해도 같은 결과가 나옴
x = torch.randn(2,3,4,5,5,6)
print(x[1,2,:,:,:].shape)#:,:,:은 뒤에 꺼 다 갖고와
print(x[1,2,...])#위에 코드랑 같은 의미 뒤에 꺼 다 갖고와
A = torch.ones(3,4)
B = torch.zeros(3,4)
C = torch.vstack([A,B])#v는 0번째 차원 세로로 쌓는다
D = torch.hstack([A,B])#h는 1번째 차원 가로로 쌓는다
E = torch.cat([A,B],dim=0)#vstack이랑 같은 의미 0번째 차원 세로로
F = torch.cat([A,B],dim=1)#hstack이랑 같음 1번째 차원 가로로
A = torch.randn(1,1,1,3,1,1,1,4,1,1,1)
print(A.squeeze().shape)#squeeze는 필요 없는 차원을 없애는 거 1개만 있는 차원은 필요없으니 사라져서 (3,4)가 나옴
print(A.squeeze(dim=(0,2,4,5)).shape)#0,2,4,5 번째에 해당하는 차원을 없애달라
A = torch.randn(3,4)
print(A.unsqueeze(dim=0).shape)#unsqueeze는 말 그대로 squeeze의 반대 가상의 차원을 생성 dim=n으로 원하는 곳에 생성가능
#reshape으로도 가능하지만 나머지 차원을 다 써줘야해서 unsqueeze가 좀 더 간단함
A = torch.ones(3,4)
B = torch.zeros(3,4)
C = torch.stack([A,B])
print(C)#unsqueeze 말고 stack으로도 가능한데 stack은 A와 B의 차원이 완벽하게 맞아야함 cat은 쌓을 차원 이외에 차원만 맞아도 가능한데 이건 안됨
A = torch.tensor([[1,2],[3,4]])
B = A
B[0,0] = 100
print(B)
print(A)#print(B)와 같은 결과가 나옴 주소 까지 복사함
#B = A.clone()이렇게 해주면 값은 같지만 주소는 달라서 B는 다른 텐서가 되서 수정해도 A에 영향이 안감
A = torch.randn(5,7)
B = torch.randn(7,10)
C = A@B
#이러면 5,10이 나옴
A = torch.randn(32,5,7)
B = torch.randn(32,7,10)
C = A@B
print(C.shape)
#3차원에 대해서 행렬 곱을 하면 5,7 7,10 각각의 대해서 모두 행렬 곱을 해서 32,5,10이 나옴 행렬 곱을 32번 하게 되는거임
A = torch.randn(32,5,7)
B = torch.randn(7,10)
C = A@B
print(C.shape)
#이렇게 되면 B를 32개를 복제해서 곱함 결과는 아까와 같은 32,5,10