2. 현재 값이 Stack의 Top보다 크거나 같다면 Stack을 Pop하고 다시 1번부터 진행.
3. 현재 Stack이 비어있다면, 현재 값의 NGE 값은 -1이 됩니다.
4. 1,2,3번이 실행되고나면 Stack에 Push 해줍니다.
ex)
#include<iostream>
#include<stack>
using namespace std;
int a[1000000];
int l[1000000];
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
stack<int> s;
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> l[i];
for (int i = n - 1; i >= 0; i--) {
while ( !s.empty() && s.top() <= l[i])
s.pop();
if (s.empty())
a[i] = -1;
else
a[i] = s.top();
s.push(l[i]);
}
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
}
● torch.randn() : 평균 0, 분산 1인 정규 분포를 따라서 tensor 배열을 생성한다.
● torch.add(x,y) : tensor 배열 x, y를 더해준다.
● torch.add(x, y, out = result) : tensor 배열 x, y를 더해 result tensor 배열에 저장한다.
● y.add(x) : tensor 배열 y 에 x를 더한 값을 y에 in_place 하게 저장한다.
● x + y : numpy 배열처럼 tensor 배열 x, y를 더한다.
● x - y : numpy 배열처럼 tensor 배열 x, y를 뺀다.
● x * y : numpy 배열처럼 tensor 배열 x, y를 행렬 곱을 한다.
● .size() : tensor 배열의 크기를 알려준다.
import numpy as np
import torch
# 1번 코드
x = torch.Tensor(5,3) # 5 x 3 tensor 배열을 만든다.
print(x)
# 2번 코드
y = np.random.rand(5,3) # 5 x 3 numpy 배열을 만든다.
print(y)
# 3번 코드 중요!
z = torch.Tensor(y) # numpy 배열 y를 tensor 배열로 바꿔 저장한다.
print(z)
print(type(z))
먼저 numpy 와 torch library를 import 하겠습니다. 1번 코드를 보시면, numpy 배열과 비슷하게 tensor 배열을 만들 수 있습니다. 위에서 tensor 타입은 numpy와 비슷하다고 했는데, 3번 코드를 보시면 numpy 배열인
import torch
x = torch.rand(5,3)
print(x)
y = torch.rand(5,3)
print(y)
print(x+y)
print(torch.add(x,y))
result = torch.Tensor(5,3) # 결과를 담아줄 배열을 만든다.
torch.add(x,y, out = result)
print(result)
다음으로는 여러 가지 덧셈 문법에 대해서 알아보도록 하겠습니다.
numpy 배열처럼 직접 x + y를 할 수도 있고, torch.add 함수를 이용할 수도 있습니다.