728x90 반응형 Python286 [PyTorch] autograd 심화: grad_fn 과 custom operation 만들기 1. PyTorch Autograd 메커니즘 이해1-1. Tensor의 grad_fn과 연산 그래프 추적 방법PyTorch tensor의 grad_fn은 마지막 operation만 표시하는 attribute.전체 computation graph 확인을 위한 next_functions attribute의 활용.재귀적 접근을 통한 전체 operation history의 추적.def print_grad_graph(grad_fn, level=0): print(' ' * level, grad_fn) if hasattr(grad_fn, 'next_functions'): for next_func in grad_fn.next_functions: if next_func[0] i.. 2025. 3. 28. [Py] Bitwise Operator Operand로 int 와 bool , 또는 byte와 bytesarrays의 각 요소 (각 요소가 중요!!)만 사용할 수 있다.bytes 나 bytearrays는 직접 사용이 안되고 각 요소 단위로 꺼내서 처리해야 한다.참고로,Bitwise Operator는 Tensor 등에서 Element-wise로 동작하며 때문에 그 중요도가 커짐.그 외에선 많이 쓰이진 않음. Python의 Bitwise Operators의 우선순위와 동작은 다음과 같음(이해를 위해 8비트 기준으로 기재했으나 실제로는 Python의 int는 가변 길이 정수로 비트 수에 제한이 없다는 점을 명심) Bitwise Operator를 연산자 precedence 에 따라 기재함:1. ~ (비트 NOT, complement)각 bit를 반.. 2025. 3. 26. [matplotlib] plt.ion 과 plt.ioff 를 통한 대화형으로 그래프 그리기 이 문서는Python REPL(Read-Eval-Print Loop) 환경에서matplotlib을 대화형(interactive)으로 사용하는 예제임. plt.ion() 은 interactive on을 줄인 이름 그대로, matplotlib의 interactive mode를 활성화함.IPython shell이나 Python 기본 interactive shell 에서 유용하게 각 statement의 실행마다 화면이 갱신됨.plt.pause(0.1) 등과 같이 사용할 경우, animation 시뮬레이션이 가능.하지만, Jupyter Notebook이나 Colab 처럼 브라우저 기반인 경우엔 그 효과가 없음%matplotlib inline 이 기본이기 때문에 셀실행마다 static image로 출력됨.별도의 창.. 2025. 3. 24. [Py] 객체(object)에 대한 정보 확인하기 Python에서 object(객체)란?type, id, refcount, value 를 속성으로 가지고 있는 a chunk of data.variable과 object에 대한 보다 자세한 내용은 다음을 참고: https://dsaint31.tistory.com/517 [Python] Variable (and Object)Variable (and Object)1. 정의Python에서 Variable은 Memory에 할당된 Object를 참조하는 Name (=Reference, Identifier)에 불과하다.이 문서에서 Object는 Python에서의 Object로 type과 value, ID (CPython에서는 할당된 memory addrdsaint31.tistory.com프로그래밍에서 object에 .. 2025. 3. 19. [Ex] Numeric Datatype 다음의 연산들의 결과를 구해보고 그 이유를 설명해보라. Ex 01result1 - 5 까지의 값을 출력하는 코드를 작성하고, 그 결과값을 comment로 기재하라.a = 15b = 4result1 = a + bresult2 = a - bresult3 = a * bresult4 = a / bresult5 = a // bEx 02result1 - 3 까지의 값을 출력하는 코드를 작성하고, 그 결과값을 comment로 기재하라.a = 17b = 5result1 = a % bresult2 = a ** 2result3 = b ** 3Ex 03최종 x의 값은?x = 10x += 5x *= 2x //= 3x %= 4Ex 04result의 값은?result = 2 + 3 * 4 ** 2 - 8 / 4Ex 05result1-4 의 데이터 타입과 값을 출력하는 코드를 작성하고.. 2025. 3. 17. [PyTorch] dtype 단축메서드로 바꾸기 아래의 URL에서 간단히 다룬 단축 method들을 이용한 방식 (to나 type이 아닌)을 설명하는 문서임.2024.03.15 - [Python] - [DL] Tensor: dtype 변경(casting) 및 shape 변경. [DL] Tensor: dtype 변경(casting) 및 shape 변경.Tensor를 추상화하고 있는 class로는numpy.array: numpy의 ndarraytorch.tensortensorflow.constant: (or tensorflow.Variable)이 있음. 이들은 Python의 sequence types과 달리 일반적으로 다음과 같은 특징을 지님.데이터들이ds31x.tistory.com사용예제: 정수형import torchb = torch.rand((2,3).. 2025. 3. 14. 이전 1 ··· 17 18 19 20 21 22 23 ··· 48 다음 728x90 반응형