<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>AI와 데이터 사이언스의 이론과 실전</title>
    <link>https://aibrain.tistory.com/</link>
    <description></description>
    <language>ko</language>
    <pubDate>Sat, 16 May 2026 05:44:54 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>큐트아리</managingEditor>
    <image>
      <title>AI와 데이터 사이언스의 이론과 실전</title>
      <url>https://tistory1.daumcdn.net/tistory/6636654/attach/d260c7e2666c415783da1e6a51c3b6d5</url>
      <link>https://aibrain.tistory.com</link>
    </image>
    <item>
      <title>CatBoost(Categorical Boosting)</title>
      <link>https://aibrain.tistory.com/184</link>
      <description>&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;CatBoost의 기본 개념&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;이름의 유래&lt;/b&gt;: &quot;CatBoost&quot;라는 이름은 'Categorical'과 'Boosting'의 합성어로, 범주형 데이터에 초점을 맞춘 부스팅 알고리즘이라는 의미를 담고 있습니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;개발자&lt;/b&gt;: CatBoost는 Yandex, 러시아의 주요 검색 엔진 기업에 의해 개발되었습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;CatBoost의 주요 특징&lt;/h3&gt;
&lt;ol style=&quot;list-style-type: decimal; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;decimal&quot;&gt;
&lt;li&gt;&lt;b&gt;범주형 데이터 처리&lt;/b&gt;: CatBoost는 별도의 전처리 없이 범주형 변수를 효과적으로 처리할 수 있도록 설계되었습니다. 대부분의 다른 기계 학습 모델과 달리, 범주형 데이터를 수치형으로 변환하는 과정이 필요 없습니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;자동 하이퍼파라미터 튜닝&lt;/b&gt;: CatBoost는 하이퍼파라미터 튜닝이 비교적 쉬운 편이며, 기본 설정만으로도 우수한 성능을 보여줍니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;결측치 처리&lt;/b&gt;: 자체적으로 결측치를 처리하는 메커니즘을 갖추고 있어, 사용자가 별도로 결측치 처리를 하지 않아도 됩니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;속도와 규모&lt;/b&gt;: CatBoost는 큰 데이터셋에서도 빠르게 작동하며, GPU 지원을 통해 더 빠른 학습 속도를 제공합니다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;CatBoost의 작동 원리&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;Symmetric Trees&lt;/b&gt;: CatBoost는 대칭 트리(Symmetric Trees)라는 특별한 유형의 트리를 사용합니다. 이는 모델의 성능과 속도 향상에 기여합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Ordered Boosting&lt;/b&gt;: CatBoost의 중요한 특징 중 하나는 'Ordered Boosting'입니다. 이는 훈련 데이터에 순서를 부여하고, 각 단계에서는 과거의 모든 데이터를 사용하지 않고 일부만을 사용하여 과적합을 방지합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Leaf-wise Growth Strategy&lt;/b&gt;: CatBoost는 트리를 구축할 때 레벨별로 확장하는 대신, 손실을 최대한 줄일 수 있는 리프에서 성장하는 전략을 사용합니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;CatBoost 사용 사례&lt;/h3&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;CatBoost는 다양한 분야에서 사용될 수 있습니다. 예를 들어, 금융 분야에서의 사기 탐지, 의료 분야에서의 질병 예측, 인터넷 검색 및 추천 시스템, 분류 문제 등에서 뛰어난 성능을 보입니다.&lt;/p&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;CatBoost와 다른 Gradient Boosting 모델과의 차이&lt;/h3&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;CatBoost는 XGBoost나 LightGBM 같은 다른 Gradient Boosting 알고리즘과 비교할 때, 범주형 데이터를 직접 처리할 수 있다는 점에서 큰 차이가 있습니다. 이 외에도 고유의 알고리즘 최적화로 인해 다른 모델 대비 과적합에 더 강하며, 안정적인 성능을 제공합니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;대칭 트리(Symmetric Trees)란?&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;정의&lt;/b&gt;: 대칭 트리는 CatBoost에서 사용되는 결정 트리의 일종으로, 트리의 각 레벨에서 분할 규칙이 모든 노드에 동일하게 적용되는 구조를 가지고 있습니다. 즉, 트리의 같은 레벨에 있는 모든 노드는 같은 기준으로 데이터를 분할합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;대칭성&lt;/b&gt;: 이러한 대칭성 때문에, 같은 레벨의 모든 노드는 구조적으로 동일한 결정 경로를 갖게 됩니다. 이는 트리를 더 단순하고 효율적으로 만들어주며, 예측 속도를 향상시키는 데 기여합니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;대칭 트리의 장점&lt;/h3&gt;
&lt;ol style=&quot;list-style-type: decimal; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;decimal&quot;&gt;
&lt;li&gt;&lt;b&gt;계산 효율성&lt;/b&gt;: 대칭 트리는 노드를 생성할 때 복잡한 계산을 줄여줍니다. 각 레벨에서 동일한 분할 기준을 사용하기 때문에, 불필요한 계산을 피할 수 있습니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;메모리 효율성&lt;/b&gt;: 대칭 구조는 트리의 저장과 처리에 있어 메모리 사용량을 최소화합니다. 또한, 이러한 구조는 GPU와 같은 병렬 컴퓨팅 환경에서도 효율적입니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;예측 속도 향상&lt;/b&gt;: 대칭 트리는 예측 시간을 단축시킵니다. 모든 레벨에서 같은 기준을 사용하기 때문에, 예측 과정이 빠르고 간결합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;과적합 방지&lt;/b&gt;: 대칭 트리는 과적합의 위험을 줄여줍니다. 복잡한 트리 구조보다 간단하게 유지되기 때문에, 모델이 훈련 데이터에 지나치게 최적화되는 것을 방지합니다.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;Leaf-wise Growth Strategy란?&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;정의&lt;/b&gt;: Leaf-wise Growth Strategy는 결정 트리를 구축할 때, 각 단계에서 가장 큰 손실 감소를 가져다 주는 리프(leaf) 노드를 분할하는 방식입니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;작동 원리&lt;/b&gt;: 이 방식에서는 전체 트리의 균형을 맞추는 대신, 가장 많은 이득을 줄 수 있는 특정 리프 노드에 집중하여 트리를 성장시킵니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;Leaf-wise 대 Level-wise&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;Level-wise Strategy&lt;/b&gt;: 일반적인 부스팅 알고리즘(예: XGBoost)에서 사용되는 Level-wise 전략은 트리의 모든 리프 노드를 동일하게 확장합니다. 이는 균형 잡힌 트리를 만들지만, 꼭 필요하지 않은 분할을 할 수도 있습니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Leaf-wise Strategy&lt;/b&gt;: 반면 Leaf-wise 전략은 가장 큰 손실을 감소시킬 수 있는 리프에 집중하여 트리를 확장합니다. 이는 불필요한 분할을 줄이고, 더 정확한 학습 결과를 도출할 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;Leaf-wise Growth Strategy의 장점&lt;/h3&gt;
&lt;ol style=&quot;list-style-type: decimal; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;decimal&quot;&gt;
&lt;li&gt;&lt;b&gt;정확도 향상&lt;/b&gt;: 손실 감소가 큰 리프에 집중함으로써, 더 정확한 예측 모델을 만들 수 있습니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;효율적인 학습&lt;/b&gt;: 불필요한 분할을 줄임으로써 학습 과정이 더 효율적이 됩니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;과적합 방지&lt;/b&gt;: CatBoost는 Leaf-wise 전략을 사용하면서도 과적합을 방지하기 위한 다양한 기술(예: 정규화, 랜덤화)을 적용합니다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;고려사항&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;과적합 위험&lt;/b&gt;: Leaf-wise 전략은 극단적인 경우 과적합으로 이어질 수 있습니다. 그러나 CatBoost는 이를 완화하기 위한 기법들을 적용합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;데이터 크기&lt;/b&gt;: 대규모 데이터셋에서 Leaf-wise 전략은 더욱 빛을 발합니다. 소규모 데이터셋에서는 이 전략이 과적합을 유발할 위험이 있을 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/184</guid>
      <comments>https://aibrain.tistory.com/184#entry184comment</comments>
      <pubDate>Wed, 10 Apr 2024 23:24:49 +0900</pubDate>
    </item>
    <item>
      <title>배깅과 부스팅의 원리와 주요 알고리즘</title>
      <link>https://aibrain.tistory.com/183</link>
      <description>&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;배깅(Bagging)&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;기본 원리&lt;/b&gt;: 배깅(Bagging, Bootstrap Aggregating의 줄임말)은 다수의 분류기가 훈련 데이터셋의 서로 다른 무작위 샘플에 대해 각각 학습을 수행하고, 이 분류기들의 예측을 평균화하여 최종 결과를 결정합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;목적&lt;/b&gt;: 배깅의 주 목적은 훈련 데이터의 무작위 샘플링을 통해 분류기의 분산을 감소시켜 과적합을 줄이는 것입니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;작동 방식&lt;/b&gt;: 주로 부트스트랩 샘플링(복원 추출)을 사용하여 원래 데이터셋의 크기와 동일한 여러 서브셋을 생성합니다. 각 서브셋으로 모델을 개별적으로 훈련시키고, 이 모델들의 결과를 결합합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;대표적 모델&lt;/b&gt;: 랜덤 포레스트(Random Forest)가 대표적인 배깅 기반 모델입니다. 랜덤 포레스트는 다수의 결정 트리들로 구성되며, 각 트리는 데이터셋의 서로 다른 샘플로 훈련됩니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;부스팅(Boosting)&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;기본 원리&lt;/b&gt;: 부스팅은 약한 학습기(Weak Learner)를 순차적으로 훈련시키면서, 잘못 예측된 데이터에 대해 가중치를 부여하여, 다음 학습기가 더 잘 예측할 수 있도록 하는 방식입니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;목적&lt;/b&gt;: 부스팅은 약한 학습기의 성능을 개선하여 강한 학습기를 만드는 것에 중점을 둡니다. 오류를 줄이기 위해 학습기 간의 연속적인 학습을 강조합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;작동 방식&lt;/b&gt;: 초기에는 모든 데이터에 동일한 가중치가 부여되지만, 각 반복 과정에서 잘못 분류된 데이터에 대한 가중치가 증가합니다. 이렇게 각기 다른 가중치가 적용된 데이터셋에 대해 새로운 학습기가 학습을 수행하고, 이 과정을 반복합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;대표적 모델&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;AdaBoost(Adaptive Boosting)&lt;/b&gt;: 오류 데이터에 가중치를 부여해가며 순차적으로 모델을 구축합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Gradient Boosting&lt;/b&gt;: 손실 함수의 기울기에 따라 모델을 업데이트합니다. XGBoost, LightGBM, CatBoost 등이 Gradient Boosting의 확장된 형태입니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;배깅과 부스팅의 차이점&lt;/h3&gt;
&lt;ol style=&quot;list-style-type: decimal; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;decimal&quot;&gt;
&lt;li&gt;&lt;b&gt;학습 방식&lt;/b&gt;: 배깅은 병렬로 각 모델이 독립적으로 학습되는 반면, 부스팅은 순차적으로 학습하여 이전 모델의 오류를 개선합니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;주 목적&lt;/b&gt;: 배깅은 분산 감소(과적합 방지)에, 부스팅은 편향 감소(성능 향상)에 초점을 맞춥니다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;샘플 가중치&lt;/b&gt;: 배깅에서는 각 모델이 동일한 확률로 데이터를 샘플링하지만, 부스팅에서는 오류를 더 많이 범한 데이터에 더 큰 가중치를 부여합니다.&lt;/li&gt;
&lt;/ol&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/183</guid>
      <comments>https://aibrain.tistory.com/183#entry183comment</comments>
      <pubDate>Wed, 10 Apr 2024 22:35:26 +0900</pubDate>
    </item>
    <item>
      <title>[Pytorch]파이토치로 구현한 선형회귀</title>
      <link>https://aibrain.tistory.com/179</link>
      <description>&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;1. 단항 선형 회귀 실습&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;한개의 입력이 들어가서 한 개의 출력이 나오는 구조&lt;/li&gt;
&lt;li&gt;y = Wx + b&lt;/li&gt;
&lt;li&gt;w는 weight, b는 잔차&lt;/li&gt;
&lt;li&gt;x가 1개임으로 단항 선형 회귀&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.optim &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; optim&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; plt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; pd&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; np&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;torch.manual_seed(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2024&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# manual_seed : 랜덤값 고정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이렇게 랜덤값을 고정하면 제 결과와 이 글을 보고 따라하시는 여러분들의 결과가 같아집니다&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;x_train = torch.FloatTensor([[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_train = torch.FloatTensor([[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(x_train, x_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(y_train, y_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;372&quot; data-origin-height=&quot;181&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/3GtWp/btsGdYOSC1u/FaL09NHpUa06Po2mTRj7hk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/3GtWp/btsGdYOSC1u/FaL09NHpUa06Po2mTRj7hk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/3GtWp/btsGdYOSC1u/FaL09NHpUa06Po2mTRj7hk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F3GtWp%2FbtsGdYOSC1u%2FFaL09NHpUa06Po2mTRj7hk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;296&quot; height=&quot;144&quot; data-origin-width=&quot;372&quot; data-origin-height=&quot;181&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.figure(figsize = (&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.scatter(x_train,y_train)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;816&quot; data-origin-height=&quot;579&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/MLWz4/btsGc5AXcAC/GwkLBTcvYQWSmLVPqZcKQK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/MLWz4/btsGc5AXcAC/GwkLBTcvYQWSmLVPqZcKQK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/MLWz4/btsGc5AXcAC/GwkLBTcvYQWSmLVPqZcKQK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FMLWz4%2FbtsGc5AXcAC%2FGwkLBTcvYQWSmLVPqZcKQK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;530&quot; height=&quot;376&quot; data-origin-width=&quot;816&quot; data-origin-height=&quot;579&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;딱봐도 한줄로 그을 수 있겠죠?&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이렇게 선으로 표현이 가능한 애들의 선 방정식을 구하는 것을 선형회귀라고 합니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 직선의 방정식으로 만들기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# y = Wx + b&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# y = 2x&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# nn.Linear(input의 수,output의 수,bias=bool)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# bias=False면 b(절편)을 안구하겠다는 의미 -&amp;gt; 정확도가 떨어질수있음&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;544&quot; data-origin-height=&quot;41&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cy1Nkd/btsGeguQA2W/P3B5s4keMtmfM6toEBPOU1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cy1Nkd/btsGeguQA2W/P3B5s4keMtmfM6toEBPOU1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cy1Nkd/btsGeguQA2W/P3B5s4keMtmfM6toEBPOU1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fcy1Nkd%2FbtsGeguQA2W%2FP3B5s4keMtmfM6toEBPOU1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;544&quot; height=&quot;41&quot; data-origin-width=&quot;544&quot; data-origin-height=&quot;41&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이렇게하면 1개의 변수를 입력으로 받아 1개의 아웃풋 값을 출력하고 절편을 계산하는 model이 완성됩니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 학습을 안한 상태에서 예측하라고하니 W와 b가 랜덤으로 정해져서 결과값이 엉망으로 나옴&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred = model(x_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;497&quot; data-origin-height=&quot;90&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bDOqjo/btsGeSmHq0i/JLbGtlohgCg7mh9bKhJs3k/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bDOqjo/btsGeSmHq0i/JLbGtlohgCg7mh9bKhJs3k/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bDOqjo/btsGeSmHq0i/JLbGtlohgCg7mh9bKhJs3k/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbDOqjo%2FbtsGeSmHq0i%2FJLbGtlohgCg7mh9bKhJs3k%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;497&quot; height=&quot;90&quot; data-origin-width=&quot;497&quot; data-origin-height=&quot;90&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model.parameters())) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# W : 0.0634 , b : 0.6625&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# y = 0.0634x + &amp;nbsp;0.6625&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;681&quot; data-origin-height=&quot;91&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/5FTf3/btsGfxP3hOj/3lL7yNEKHEJbwYkp2ui2V0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/5FTf3/btsGfxP3hOj/3lL7yNEKHEJbwYkp2ui2V0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/5FTf3/btsGfxP3hOj/3lL7yNEKHEJbwYkp2ui2V0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F5FTf3%2FbtsGfxP3hOj%2F3lL7yNEKHEJbwYkp2ui2V0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;681&quot; height=&quot;91&quot; data-origin-width=&quot;681&quot; data-origin-height=&quot;91&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이렇게 학습을 안한 상태의 모델은 이상한 결과값을 가져옵니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;당연히 그럴 수 밖에 없는게 모델을 사람으로 치자면 내가 뭔가를 1개 주면 너가 1개를 만들어서 돌려줘~하고 말한 상태에서 아무거나 준 상태입니다. 모델은 ??? 뭘 달라는건지 모르겠지만 일단 아무거나 줄게! 하고 준 상태인거죠.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이 때, mse를 구해보겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# mse 값 구하기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;((y_train - y_pred)**&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).mean()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;475&quot; data-origin-height=&quot;37&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/b612T4/btsGfMzzjMM/Dsgth0EqtX49OoHo5UPW0k/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/b612T4/btsGfMzzjMM/Dsgth0EqtX49OoHo5UPW0k/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/b612T4/btsGfMzzjMM/Dsgth0EqtX49OoHo5UPW0k/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fb612T4%2FbtsGfMzzjMM%2FDsgth0EqtX49OoHo5UPW0k%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;475&quot; height=&quot;37&quot; data-origin-width=&quot;475&quot; data-origin-height=&quot;37&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;mse는 단독으로는 크게 쓸모가 없습니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;지금이야 아웃풋값이 2,4,6인 정수인걸 알고 있으니까 오차의 제곱의 평균인 mse가 저정도면 크다. 실제값이 2,4,6인데 한 값당 오차의 제곱 평균이 12정도이면 루트값을 구했을때 3.xx 정도이니 오차가 어마어마하군. 이렇게 알 수 있지만 현업에서는 저정도로 계산하기가 어렵습니다. 실제값도 너무 많을 뿐더러 실제값의 단위도 중요하고 실제값이 저렇게 고르게 분포되어 있지도 않을테니까요. 그러니 지금 학습이 안된 상태의 모델의 mse와 학습한 모델의 mse를 비교해서 개선이 되었다!를 아는 것이 중요합니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;그러니 이 mse를 통해 손실함수를 사용하면 좋겠죠? 모델은 손실함수의 값을 최저로 하는 것을 목표로 학습을 진행하니까요.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'''&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;SGD(Stochastic Gradient Descent)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;- 기존의 그래디언트 디센트는 모든데이터를 하나하나씩 뽑아서 계산하며 최솟값찾았지만&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;- SGD는 랜덤하게 데이터를 하나씩 봅아서 loss를 만듦(랜덤이라 Stochastic)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;- 데이터를 뽑고 데이터를 다시 넣고 반복&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;- 빠르게 방향을 결정&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;학습률(learning rate)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;- 한번 움직이는 거리(incresent step)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;- 독립변수가 많아서 차원이 많은 데이터의 경우는 learning rate를 작게&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'''&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 뒤에 익명함수 구현&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;loss = nn.MSELoss()(y_pred,y_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;loss&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = optim.SGD(model.parameters(),lr=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.01&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# optimizer 사용&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 1. gradient를 초기화&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 원래는 optimizer가 반복문 안에서 사용됨&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 러닝레이트만큼 움직일때마다 오차값을 새로 계산하게 항상 초기화 시켜야함&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 역전파 : 비용함수를 미분해서 gradient(기울기)를 실제적으로 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 해당 값의 순간기울기를 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 기존의 기울기와 바이어스를 업데이트&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model.parameters())) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 0.2177x + 0.7267&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 반복 학습을 통해 틀린 W, b를 수정하면서 오차를 계속 줄여나감&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# epochs : 반복 학습 횟수(에포크)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(epochs + &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; y_pred = model(x_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss = nn.MSELoss()(y_pred, y_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch % &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; == &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Epoch: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epochs}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Loss: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{loss&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:.6f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;370&quot; data-origin-height=&quot;310&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/blTqp0/btsGd669qEC/491U2cc5bKH74FUpvJWuRK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/blTqp0/btsGd669qEC/491U2cc5bKH74FUpvJWuRK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/blTqp0/btsGd669qEC/491U2cc5bKH74FUpvJWuRK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FblTqp0%2FbtsGd669qEC%2F491U2cc5bKH74FUpvJWuRK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;282&quot; height=&quot;236&quot; data-origin-width=&quot;370&quot; data-origin-height=&quot;310&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model.parameters())) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# W:1.9499 , b: 0.1138&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;689&quot; data-origin-height=&quot;88&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bd9vOZ/btsGe6ZtgPZ/ohBlWYkYi7L6lJa2KjtU21/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bd9vOZ/btsGe6ZtgPZ/ohBlWYkYi7L6lJa2KjtU21/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bd9vOZ/btsGe6ZtgPZ/ohBlWYkYi7L6lJa2KjtU21/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fbd9vOZ%2FbtsGe6ZtgPZ%2FohBlWYkYi7L6lJa2KjtU21%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;540&quot; height=&quot;69&quot; data-origin-width=&quot;689&quot; data-origin-height=&quot;88&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;x_test = torch.FloatTensor([[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred = model(x_test)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(y_pred)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;515&quot; data-origin-height=&quot;40&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bQRs3T/btsGdthvqru/uZNOa3Hvo0E0O3DZvhs9P1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bQRs3T/btsGdthvqru/uZNOa3Hvo0E0O3DZvhs9P1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bQRs3T/btsGdthvqru/uZNOa3Hvo0E0O3DZvhs9P1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbQRs3T%2FbtsGdthvqru%2FuZNOa3Hvo0E0O3DZvhs9P1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;451&quot; height=&quot;35&quot; data-origin-width=&quot;515&quot; data-origin-height=&quot;40&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;학습되지 않은 값인 8을 넣었을때 잘 나오는 것을 알 수 있습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;2. 다중 선형 회귀&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;여러 개의 입력이 들어가서 한 개의 출력이 나오는 구조이것&lt;/li&gt;
&lt;li&gt;y = W &lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;⑴&lt;/span&gt; x &lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;⑴&lt;/span&gt; + W &lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;⑵x &lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;⑵&amp;nbsp; + ..... + b&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;&lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;x변수가 2개 이상인 회귀 분석&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;x_train = torch.FloatTensor([[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;73&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;80&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;75&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;93&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;88&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;93&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;89&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;91&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;96&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;98&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;73&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;66&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;70&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_train = torch.FloatTensor([[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;150&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], [&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;190&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], [&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;180&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], [&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], [&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;130&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(x_train, x_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(y_train, y_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;550&quot; data-origin-height=&quot;280&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/dA3MRg/btsGfeC8IK4/5l8Dt27dNKMNjXFGBNWL2k/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/dA3MRg/btsGfeC8IK4/5l8Dt27dNKMNjXFGBNWL2k/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/dA3MRg/btsGfeC8IK4/5l8Dt27dNKMNjXFGBNWL2k/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdA3MRg%2FbtsGfeC8IK4%2F5l8Dt27dNKMNjXFGBNWL2k%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;381&quot; height=&quot;194&quot; data-origin-width=&quot;550&quot; data-origin-height=&quot;280&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;다중 선형 회귀도 모델 쌓는것과 학습하는 코드는 똑같습니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;다만 이전에는 nn.Linear(1,1) 즉, 1개의 input에 1개의 output이였다면 이젠 nn.Linear(3,1) 즉, 3개의 input에 1개의 output으로 코드를 짜야합니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 모델 만들기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = optim.SGD(model.parameters(), lr=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.00001&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;150000&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(epochs + &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; y_pred = model(x_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss = nn.MSELoss()(y_pred, y_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch % &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; == &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Epoch: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epochs}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Loss: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{loss&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:.6f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;426&quot; data-origin-height=&quot;433&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bBYmrz/btsGeKvDGsB/EyvnD4O9m00R5pzspoxsKK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bBYmrz/btsGeKvDGsB/EyvnD4O9m00R5pzspoxsKK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bBYmrz/btsGeKvDGsB/EyvnD4O9m00R5pzspoxsKK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbBYmrz%2FbtsGeKvDGsB%2FEyvnD4O9m00R5pzspoxsKK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;279&quot; height=&quot;284&quot; data-origin-width=&quot;426&quot; data-origin-height=&quot;433&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model.parameters())) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# W:-1.4802, -0.2136, &amp;nbsp;3.6704 , b: 0.1138&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;891&quot; data-origin-height=&quot;83&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bMpFPp/btsGgaG80Rf/LVLLsr8nnYMVNZNtDkJaEk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bMpFPp/btsGgaG80Rf/LVLLsr8nnYMVNZNtDkJaEk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bMpFPp/btsGgaG80Rf/LVLLsr8nnYMVNZNtDkJaEk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbMpFPp%2FbtsGgaG80Rf%2FLVLLsr8nnYMVNZNtDkJaEk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;891&quot; height=&quot;83&quot; data-origin-width=&quot;891&quot; data-origin-height=&quot;83&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;x_test = torch.FloatTensor([[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;92&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;90&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;89&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred = model(x_test)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(y_pred)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;521&quot; data-origin-height=&quot;28&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/vcHl6/btsGezgI5ue/iLn72blJu88RhegLGwWJsk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/vcHl6/btsGezgI5ue/iLn72blJu88RhegLGwWJsk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/vcHl6/btsGezgI5ue/iLn72blJu88RhegLGwWJsk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FvcHl6%2FbtsGezgI5ue%2FiLn72blJu88RhegLGwWJsk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;465&quot; height=&quot;25&quot; data-origin-width=&quot;521&quot; data-origin-height=&quot;28&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;지금까진 직접 작성한 데이터셋을 사용했는데요 이번엔 csv파일을 불러와서 해보겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;3. temps 데이터셋&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;천안지역의 1년 날씨 데이터&lt;/li&gt;
&lt;li&gt;천안에 있는 다른 해의 날짜를 예측하는 것이 목표&lt;/li&gt;
&lt;li&gt;기온에 따른 지면온도 예측하기&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# df에 데이터프레임으로 불러오기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df = pd.read_csv(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'/content/drive/MyDrive/KDT/머신러닝과 딥러닝/data/temps.csv'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,encoding= &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'euc-kr'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# df의 null값 구분하기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df.isna().mean()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;295&quot; data-origin-height=&quot;171&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bwsrU2/btsGd7rqRAw/P1kKfurPQ4bpiAP5d8Qw61/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bwsrU2/btsGd7rqRAw/P1kKfurPQ4bpiAP5d8Qw61/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bwsrU2/btsGd7rqRAw/P1kKfurPQ4bpiAP5d8Qw61/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbwsrU2%2FbtsGd7rqRAw%2FP1kKfurPQ4bpiAP5d8Qw61%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;197&quot; height=&quot;114&quot; data-origin-width=&quot;295&quot; data-origin-height=&quot;171&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# df의 null 제거하기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df.dropna(inplace=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df.isna().mean()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;239&quot; data-origin-height=&quot;170&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/b6VNuP/btsGgz7Tw1N/fYrucLd46vkZJEKaKVxzm1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/b6VNuP/btsGgz7Tw1N/fYrucLd46vkZJEKaKVxzm1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/b6VNuP/btsGgz7Tw1N/fYrucLd46vkZJEKaKVxzm1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fb6VNuP%2FbtsGgz7Tw1N%2FfYrucLd46vkZJEKaKVxzm1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;155&quot; height=&quot;110&quot; data-origin-width=&quot;239&quot; data-origin-height=&quot;170&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# x_data에 기온 데이터를 저장&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;x_data = df[[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'기온(&amp;deg;C)'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터프레임을 텐서로 변경&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;x_data = torch.FloatTensor(x_data.values)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# x_data = x_data.reshape(-1, 1)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# y_data에 지면온도 데이터를 저장&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_data = df[[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'지면온도(&amp;deg;C)'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터프레임을 텐서로 변경 -&amp;gt; 이런 형식으로 하면 차원이 지켜지는듯?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_data = torch.FloatTensor(y_data.values)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# y_data = y_data.reshape(-1, 1)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(x_data.shape, y_data.shape)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;484&quot; data-origin-height=&quot;37&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/GBV9Z/btsGfwwTWbd/odX9KpbpgjzsR8YVV3zky1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/GBV9Z/btsGfwwTWbd/odX9KpbpgjzsR8YVV3zky1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/GBV9Z/btsGfwwTWbd/odX9KpbpgjzsR8YVV3zky1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FGBV9Z%2FbtsGfwwTWbd%2FodX9KpbpgjzsR8YVV3zky1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;366&quot; height=&quot;28&quot; data-origin-width=&quot;484&quot; data-origin-height=&quot;37&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# scatter로 x_data와 y_data를 출력&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.figure(figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.scatter(x_data, y_data)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.xlabel(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'기온(&amp;deg;C)'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.ylabel(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'지면온도(&amp;deg;C)'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.title(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Scatter Plot'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;987&quot; data-origin-height=&quot;951&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/baXytZ/btsGe6ZtvWi/X0dQpXwwt8OvLGWGkPdXTK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/baXytZ/btsGe6ZtvWi/X0dQpXwwt8OvLGWGkPdXTK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/baXytZ/btsGe6ZtvWi/X0dQpXwwt8OvLGWGkPdXTK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbaXytZ%2FbtsGe6ZtvWi%2FX0dQpXwwt8OvLGWGkPdXTK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;351&quot; height=&quot;338&quot; data-origin-width=&quot;987&quot; data-origin-height=&quot;951&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;저는 코랩을 써서 한글이 깨지지만 이것도 해결할 수 있습니다. 해결 방법은 추후에 별도로 글을 작성해서 올리겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 입력과 출력이 1개인 선형모델 생성&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# SGD를 통해 옵티마이저 객체를 생성&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = torch.optim.SGD(model.parameters(), lr=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.001&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model.parameters()))&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;626&quot; data-origin-height=&quot;74&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/SS7YC/btsGejd6Kr8/uJAcZ26mr2Z8oTsEcS41uK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/SS7YC/btsGejd6Kr8/uJAcZ26mr2Z8oTsEcS41uK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/SS7YC/btsGejd6Kr8/uJAcZ26mr2Z8oTsEcS41uK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FSS7YC%2FbtsGejd6Kr8%2FuJAcZ26mr2Z8oTsEcS41uK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;550&quot; height=&quot;65&quot; data-origin-width=&quot;626&quot; data-origin-height=&quot;74&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 모델을 학습&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 오차함수는 MSELoss()를 사용&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(epochs + &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; y_pred = model(x_data)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss = nn.MSELoss()(y_pred, y_data)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch % &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; == &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Epoch: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epochs}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Loss : &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{loss : &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;.6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;f}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;359&quot; data-origin-height=&quot;266&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/caD0cB/btsGfJbODKi/sVQTIyW250KQqBk9MKFeiK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/caD0cB/btsGfJbODKi/sVQTIyW250KQqBk9MKFeiK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/caD0cB/btsGfJbODKi/sVQTIyW250KQqBk9MKFeiK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcaD0cB%2FbtsGfJbODKi%2FsVQTIyW250KQqBk9MKFeiK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;232&quot; height=&quot;172&quot; data-origin-width=&quot;359&quot; data-origin-height=&quot;266&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 학습된 파라미터 확인&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model.parameters())) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# W:1.1182 , b: 0.1443&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;619&quot; data-origin-height=&quot;75&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/dldlu8/btsGfgAYWn6/jXz62eXRRB5zYY3PcWPiik/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/dldlu8/btsGfgAYWn6/jXz62eXRRB5zYY3PcWPiik/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/dldlu8/btsGfgAYWn6/jXz62eXRRB5zYY3PcWPiik/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fdldlu8%2FbtsGfgAYWn6%2FjXz62eXRRB5zYY3PcWPiik%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;479&quot; height=&quot;58&quot; data-origin-width=&quot;619&quot; data-origin-height=&quot;75&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 예측된 y값 구하기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# detach() : 텐서에서 다른 자료구조로 변환&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred = model(x_data).detach().numpy()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# scatter로 x_data와 y_data를 출력 후 예측된 선을 출력&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# scatter로 x_data와 y_data를 출력&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 666&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.figure(figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.scatter(x_data, y_data)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.scatter(x_data,y_pred)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.xlabel(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'기온(&amp;deg;C)'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.ylabel(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'지면온도(&amp;deg;C)'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.title(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Scatter Plot'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;872&quot; data-origin-height=&quot;871&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bC45pv/btsGdZG6DIB/rV5OHKDIk8Ks8ZxfUiRp90/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bC45pv/btsGdZG6DIB/rV5OHKDIk8Ks8ZxfUiRp90/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bC45pv/btsGdZG6DIB/rV5OHKDIk8Ks8ZxfUiRp90/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbC45pv%2FbtsGdZG6DIB%2FrV5OHKDIk8Ks8ZxfUiRp90%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;349&quot; height=&quot;349&quot; data-origin-width=&quot;872&quot; data-origin-height=&quot;871&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;선을 보면 완벽하게 데이터를 설명하진 못하지만 어느정도 기울기를 반영한 선형식이 완성된 것을 알 수 있습니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;사실 지금과 같이 딥러닝 기반 선형회귀와는 달리 통계적인 의미의 선형회귀는 조건이 굉장히 까다롭습니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;등분산성, 선형성, 독립성, 정규성, 다중공선성과 같은 조건들이 만족해야 선형회귀를 돌릴 때 의미있는 직선을 구할 수가 있습니다. 하지만 우리는 모델 학습의 기초에 대해서 쉽게 알기위해 선형회귀로 배워본 것임으로 예측 정확도가 낮은것에 집중하지말고 데이터의 형태를 확인하는 법과 학습코드를 작성하는 법에 집중해서 보면 도움이 될 것 입니다.&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/179</guid>
      <comments>https://aibrain.tistory.com/179#entry179comment</comments>
      <pubDate>Sun, 31 Mar 2024 20:40:09 +0900</pubDate>
    </item>
    <item>
      <title>목적함수와 최적화 알고리즘, 경사하강법, 학습률과 배치사이즈</title>
      <link>https://aibrain.tistory.com/178</link>
      <description>&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;흔히 말하는 모델을 학습시킬때, 모델을 무엇으로 평가를 하고 어떻게 학습해나가는 걸까요?&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;오늘은 모델의 평가기준인 목적함수와 성능개선 방법인 최적화 알고리즘에 대해서 알아보겠습니다.&lt;/p&gt;
&lt;h2 style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size26&quot;&gt;최적화 알고리즘의 기본 개념&lt;/h2&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;최적화 알고리즘은 주어진 문제에 대해 최적의 해결책을 찾는 과정입니다. 이 과정에서는 목표 함수(objective function)를 최대화하거나 최소화하는 변수의 값을 결정합니다. 최적화 문제는 다양한 제약 조건을 포함할 수 있으며, 이러한 제약 조건 하에서 최적의 해를 찾는 것이 목표입니다.&lt;/p&gt;
&lt;h2 style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size26&quot;&gt;주요 최적화 알고리즘 종류&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;경사하강법(Gradient Descent)&lt;/b&gt;: 함수의 기울기(경사)를 사용하여 최소점을 찾는 방법입니다. 이 방법은 머신러닝 모델을 학습시킬 때 널리 사용됩니다.&lt;/li&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;유전 알고리즘(Genetic Algorithm)&lt;/b&gt;: 자연 선택과 유전자 변이 과정을 모방하여 최적의 해를 찾는 방법입니다. 복잡한 최적화 문제에 효과적으로 사용될 수 있습니다.&lt;/li&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;심화 학습(Deep Learning)&lt;/b&gt;: 대규모 데이터셋에서 복잡한 패턴을 학습하여 최적화 문제를 해결하는 데 사용됩니다. 이 방법은 이미지 인식, 자연어 처리 등 다양한 분야에서 활용됩니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 data-ke-size=&quot;size20&quot;&gt;이제, 가장 많이 쓰이는 경사하강법에 대해서 알아보겠습니다.&lt;/h4&gt;
&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h2 style=&quot;color: #000000;&quot; data-ke-size=&quot;size26&quot;&gt;경사하강법(Gradient Descent)&lt;/h2&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;비용함수(mse와 같은 현재값과 예측값사이의 차이를 구하는 함수)의 값을 최소로 하는 W와 b를 찾는 알고리즘을 옵티마이저(최적화) 알고리즘이라고 함&lt;/li&gt;
&lt;li&gt;옵티마이저 알고리즘 중 가장 기본적인 기술이 경사하강법이다&lt;/li&gt;
&lt;li&gt;옵티마이저 알고리즘을 통해 W와 b를 찾아내는 과정을 &quot;학습&quot;이라고 부름&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;조금씩 기울기를 바꿔보면서 오차가 가장 작아지는 W값을 찾는 기법&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;860&quot; data-origin-height=&quot;483&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bAvkDP/btsGgcSpKM4/43gezOYCIFJKfktKipo8h0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bAvkDP/btsGgcSpKM4/43gezOYCIFJKfktKipo8h0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bAvkDP/btsGgcSpKM4/43gezOYCIFJKfktKipo8h0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbAvkDP%2FbtsGgcSpKM4%2F43gezOYCIFJKfktKipo8h0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;600&quot; height=&quot;483&quot; data-origin-width=&quot;860&quot; data-origin-height=&quot;483&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;그런데 이런 경사하강법에도 종류가 있습니다.&lt;/p&gt;
&lt;p style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;너무 복잡하죠? 하지만 대분류를 아시면 헷갈리지 않으실겁니다. 제가 아래에 요약을 해놓았으니 확인해보세요.&lt;/p&gt;
&lt;h2 style=&quot;color: #000000;&quot; data-ke-size=&quot;size26&quot;&gt;경사하강법(Gradient Descent)의 종류&lt;/h2&gt;
&lt;ol style=&quot;list-style-type: decimal; background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-list-type=&quot;decimal&quot;&gt;
&lt;li&gt;&lt;b&gt;배치 경사 하강법 (Batch Gradient Descent)&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;모든 훈련 데이터를 한 번에 사용하여 기울기를 계산하고 파라미터를 업데이트합니다.&lt;/li&gt;
&lt;li&gt;전체 데이터셋이 크면 메모리 부담이 크고, 업데이트가 느릴 수 있지만 안정적으로 최적값에 수렴할 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;확률적 경사 하강법 (Stochastic Gradient Descent, SGD)&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;각각의 훈련 데이터마다 기울기를 계산하고 파라미터를 업데이트합니다.&lt;/li&gt;
&lt;li&gt;각 단계에서의 업데이트는 불안정할 수 있지만, 빠르게 수렴할 수 있고 대규모 데이터셋에 유용합니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;미니 배치 경사 하강법 (Mini-batch Gradient Descent)&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;전체 데이터셋을 작은 미니 배치로 나누고 각 미니 배치에 대해 기울기를 계산하고 파라미터를 업데이트합니다.&lt;/li&gt;
&lt;li&gt;배치 경사 하강법과 SGD의 절충안으로, 업데이트가 빠르면서도 안정적으로 수렴할 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;모멘텀 최적화 (Momentum Optimization)&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;기존의 경사 하강법에 관성을 추가하여, 이전 업데이트의 효과를 고려합니다.&lt;/li&gt;
&lt;li&gt;이전 그래디언트들의 가중 평균을 사용하여 진행 방향을 결정하므로 지역 최소값에 빠지지 않고 빠르게 수렴할 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;아다그라드 (Adagrad)&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;각각의 파라미터에 다른 학습률을 적용하여 많이 변화하지 않은 파라미터는 더 큰 학습률을 가지고, 자주 변화하는 파라미터는 작은 학습률을 가집니다.&lt;/li&gt;
&lt;li&gt;주어진 문제에 따라 적응적으로 학습률을 조절하여 수렴 속도를 향상시킬 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;아담 옵티마이저 (Adam Optimizer)&lt;/b&gt;:
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;모멘텀 최적화와 RMSProp을 합친 알고리즘으로, 그래디언트의 지수 가중 평균 및 제곱의 지수 가중 평균을 계산하여 업데이트합니다.&lt;/li&gt;
&lt;li&gt;학습률의 보정과 그래디언트의 이동 평균을 사용하여 효과적으로 수렴할 수 있습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&lt;b&gt;목적함수(Objective Function)에 대한 이해&lt;/b&gt;&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;목적함수는 최적화 문제에서 최대화하거나 최소화하고자 하는 대상을 정의하는 함수입니다. 머신러닝, 인공지능, 경영과학 등 다양한 분야에서 중요한 역할을 합니다. 목적함수는 손실 함수(Loss Function), 비용 함수(Cost Function) 등으로도 불리며, 이는 문제의 성격에 따라 달라질 수 있습니다.  &lt;/p&gt;
&lt;hr data-ke-style=&quot;style1&quot; /&gt;
&lt;h2 style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size26&quot;&gt;목적함수의 기본 개념&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;목적함수란?&lt;/b&gt;: 머신러닝 모델의 성능을 측정하거나, 경영과학에서 이익을 최대화하거나 비용을 최소화하는 등의 목적을 달성하기 위해 최적화하는 함수입니다. 이 함수의 값을 최소화하거나 최대화함으로써 최적의 해를 찾습니다.&lt;span&gt;&amp;nbsp;&lt;/span&gt;1&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size26&quot;&gt;목적함수의 종류&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;손실 함수(Loss Function)&lt;/b&gt;: 모델의 예측값과 실제 값 사이의 차이를 측정하는 함수입니다. 손실 함수의 값이 작을수록 모델의 성능이 좋다고 평가됩니다.&lt;/li&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;비용 함수(Cost Function)&lt;/b&gt;: 손실 함수의 값들을 데이터셋 전체에 대해 평균낸 값입니다. 모델의 전체적인 성능을 평가하는 데 사용됩니다.&lt;/li&gt;
&lt;li style=&quot;color: #1a1918;&quot;&gt;&lt;b&gt;목적 함수(Objective Function)&lt;/b&gt;: 최적화하고자 하는 주요 목표를 정의하는 함수로, 손실 함수나 비용 함수를 포함할 수 있습니다.&lt;span&gt;&amp;nbsp;&lt;/span&gt;2&lt;span&gt;&amp;nbsp;&lt;/span&gt;3&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;목적함수의 중요성&lt;/h3&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;목적함수는 최적화 문제에서 해결책의 방향성을 제시합니다. 머신러닝에서는 모델의 학습 방향을 결정하고, 경영과학에서는 의사 결정의 기준이 됩니다. 목적함수를 통해 문제의 해결책을 수학적으로 정의하고, 최적화 알고리즘을 사용하여 최적의 해를 찾을 수 있습니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;목적함수는 문제 해결의 핵심입니다. 목적에 맞는 적절한 함수를 선택하고 최적화하는 과정을 통해, 더 나은 결정을 내릴 수 있습니다. 목적함수를 이해하고 적용하는 것은 다양한 분야에서 중요한 역할을 합니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #1a1918; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3 style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size23&quot;&gt;&lt;b&gt;요약&lt;/b&gt;&lt;/h3&gt;
&lt;p style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size18&quot;&gt;&lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;&lt;b&gt;모델의 목적&lt;/b&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;: 목적함수값 최대 혹은 최소로 만들기&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size18&quot;&gt;&lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;&lt;b&gt;최적화 알고리즘&lt;/b&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;: 목적함수 값을 최대 최소로 만드는 방법&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;color: #333333; text-align: start;&quot; data-ke-size=&quot;size18&quot;&gt;&lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: left;&quot;&gt;&lt;b&gt;경사하강법&lt;/b&gt;&lt;span&gt;&amp;nbsp;&lt;/span&gt;: 최적화 알고리즘의 한 종류,&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: left;&quot;&gt;시작점에서 기울기(경사)를 따라 목적 함수 값을 최적화하기 위해 조금씩 이동하는 방식&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;color: #000000; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&lt;b&gt;SGD, ADAM&amp;nbsp;&lt;/b&gt;: 경사하강법의 종류&lt;/p&gt;
&lt;p style=&quot;color: #000000; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1648&quot; data-origin-height=&quot;867&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cq3rYs/btsGdQKcS9y/f6WlwhT4sCM1ToeTLCO5k0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cq3rYs/btsGdQKcS9y/f6WlwhT4sCM1ToeTLCO5k0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cq3rYs/btsGdQKcS9y/f6WlwhT4sCM1ToeTLCO5k0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fcq3rYs%2FbtsGdQKcS9y%2Ff6WlwhT4sCM1ToeTLCO5k0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1648&quot; height=&quot;867&quot; data-origin-width=&quot;1648&quot; data-origin-height=&quot;867&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;span style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: left;&quot;&gt;이렇게 그림으로 보면 이해가 편하죠? 최적화 알고리즘과 목적함수를 나눠놨지만 결국은 모델 안에서 연관성이 있습니다. 그저 이해하기 쉽게 나눠놓았습니다.&lt;/span&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;&lt;b&gt;학습률 (Learning Rate) 개념&lt;/b&gt;&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;학습률은 각 반복(에폭)에서 가중치를 업데이트할 때 얼마나 '크게' 업데이트할지를 결정합니다.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot;&gt;신경망 모델을 훈련할 때 가중치를 업데이트하는 속도를 결정하는 하이퍼파라미터입니다.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;너무 작은 학습률은 수렴하는 데 시간이 오래 걸릴 수 있으며, 학습이 지역 최소점(local minima)에서 멈출 수 있습니다.&lt;/li&gt;
&lt;li&gt;반대로, 너무 큰 학습률은 발산(divergence)하거나 안정성이 없는 훈련을 초래할 수 있습니다.&lt;/li&gt;
&lt;li&gt;일반적으로는 작은 학습률부터 시작하여 점진적으로 크기를 키워가며 모델의 성능을 모니터링하면서 최적값을 찾습니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;&lt;b&gt;배치 사이즈 (Batch Size) 개념&lt;/b&gt;&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;배치 사이즈는 각 반복에서 신경망에 전달되는 데이터 포인트의 수를 결정합니다.&lt;/li&gt;
&lt;li&gt;작은 배치 사이즈는 더 많은 업데이트를 제공하지만, 계산 비용이 높을 수 있습니다.&lt;/li&gt;
&lt;li&gt;큰 배치 사이즈는 더 안정적인 그레이디언트(gradient)를 제공할 수 있지만, 메모리 요구량이 늘어날 수 있습니다.&lt;/li&gt;
&lt;li&gt;일반적으로는 GPU 또는 TPU와 같은 하드웨어의 메모리 용량에 맞게 배치 사이즈를 선택합니다.&lt;/li&gt;
&lt;li&gt;또한 배치 사이즈가 작을수록 노이즈가 많이 발생하므로 학습이 더 많은 에폭을 거쳐야 할 수 있습니다.&lt;/li&gt;
&lt;li&gt;신경망이 한번의 업데이트를 수행하기 위해 몇개의 데이터를 쓸 것인지를 의미합니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;모델 학습에는 어떤 목적함수를 쓸 것인지, 어떤 최적화 알고리즘을 쓸것인지, 학습률을 몇으로 할것인지, 배치사이즈를 몇으로 할것인지 등을 통해 천차만별의 성능을 자기는 모델이 탄생합니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;때문에 이런 개념을 확실히 잡고 모델을 학습시킬 때 다양하게 시도하고 고려한다면 여러분들도 좋은 모델을 만들 수 있을것입니다.&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/178</guid>
      <comments>https://aibrain.tistory.com/178#entry178comment</comments>
      <pubDate>Sun, 31 Mar 2024 19:41:29 +0900</pubDate>
    </item>
    <item>
      <title>LSTM으로 주식예측</title>
      <link>https://aibrain.tistory.com/176</link>
      <description>&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision.transforms &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; transforms&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision.datasets&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; Parameter&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; Tensor&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn.functional &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; F&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.utils.data &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; DataLoader&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; plt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; datetime&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;LSTM 모델 생성&lt;/span&gt;&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;LSTM&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;nn&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;Module&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;__init__&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;num_classes&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;input_size&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;hidden_size&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;num_layers&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;seq_length&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; super(LSTM, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;__init__&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_classes = num_classes &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 클래스&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_layers = num_layers &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# LSTM 계층 개수&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.input_size = input_size &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 입력 크기(컬럼 개수)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size = hidden_size &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 은닉층의 뉴런 개수&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.seq_length = seq_length &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 시퀀스 길이&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.lstm = nn.LSTM(input_size=input_size, hidden_size=hidden_size,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; num_layers=num_layers, batch_first=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# lstm 계층&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc_1 = &amp;nbsp;nn.Linear(hidden_size, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 완전연결층&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc = nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, num_classes) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 출력층&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.relu = nn.ReLU()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;forward&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; h_0 = torch.zeros(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_layers, x.size(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 은닉상태 초기화&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c_0 = torch.zeros(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_layers, x.size(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 셀 상태 초기화&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output, (hn, cn) = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.lstm(x, (h_0, c_0)) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# lstm 계층에 은닉 상태와 셀 상태 적용&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; hn = hn.view(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;-1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 완전연결층 적용을 위해 데이터 형태를 1차원으로 조정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.relu(hn)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc_1(out) &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.relu(out) &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc(out)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; out&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.style.use(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'seaborn-whitegrid'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;device = torch.device(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cuda:0'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cpu'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;cuda = &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;Tensor = torch.cuda.FloatTensor &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; cuda &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.FloatTensor&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 난수 생성기의 시드(seed)를 설정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;torch.manual_seed(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;125&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; torch.cuda.manual_seed_all(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;125&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; yfinance &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; yf&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df = yf.download(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'005930.KS'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;start=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'2022-01-01'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'2022-05-31'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 005930 : 삼성전자 주가&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df.head()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;750&quot; data-origin-height=&quot;342&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bpfLZq/btsF6hOaxF8/IFv7Ye91eMR1yMDWlubUM0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bpfLZq/btsF6hOaxF8/IFv7Ye91eMR1yMDWlubUM0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bpfLZq/btsF6hOaxF8/IFv7Ye91eMR1yMDWlubUM0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbpfLZq%2FbtsF6hOaxF8%2FIFv7Ye91eMR1yMDWlubUM0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;750&quot; height=&quot;342&quot; data-origin-width=&quot;750&quot; data-origin-height=&quot;342&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data check&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig = df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].plot()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;772&quot; data-origin-height=&quot;556&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/6Cqwa/btsF7TMNt5q/WrIqGcDomfb36dEl3i9tFK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/6Cqwa/btsF7TMNt5q/WrIqGcDomfb36dEl3i9tFK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/6Cqwa/btsF7TMNt5q/WrIqGcDomfb36dEl3i9tFK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F6Cqwa%2FbtsF7TMNt5q%2FWrIqGcDomfb36dEl3i9tFK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;772&quot; height=&quot;556&quot; data-origin-width=&quot;772&quot; data-origin-height=&quot;556&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X = df.drop(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, axis=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# X, y 분리&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y = df[[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]]&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; sklearn.preprocessing &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; StandardScaler, MinMaxScaler&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;ms = MinMaxScaler() &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 0 ~ 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;ss = StandardScaler() &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 평균 0, 분산 1&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_ss = ss.fit_transform(X)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_ms = ms.fit_transform(y)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_train = X_ss[:&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, :]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_test = X_ss[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:, :]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_train = y_ms[:&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, :]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_test = y_ms[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:, :]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Training Shape :'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_train.shape, y_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Testing Shape :'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_test.shape, y_test.shape)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;337&quot; data-origin-height=&quot;61&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/QrVzZ/btsF77xeBQF/501WBXFfBYtWIzvIyhmFUk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/QrVzZ/btsF77xeBQF/501WBXFfBYtWIzvIyhmFUk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/QrVzZ/btsF77xeBQF/501WBXFfBYtWIzvIyhmFUk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FQrVzZ%2FbtsF77xeBQF%2F501WBXFfBYtWIzvIyhmFUk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;337&quot; height=&quot;61&quot; data-origin-width=&quot;337&quot; data-origin-height=&quot;61&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;573&quot; data-origin-height=&quot;221&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bw17Mm/btsF6Hsu8DE/vfkB5bOCwk1CiPSNVBOK1k/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bw17Mm/btsF6Hsu8DE/vfkB5bOCwk1CiPSNVBOK1k/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bw17Mm/btsF6Hsu8DE/vfkB5bOCwk1CiPSNVBOK1k/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fbw17Mm%2FbtsF6Hsu8DE%2FvfkB5bOCwk1CiPSNVBOK1k%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;573&quot; height=&quot;221&quot; data-origin-width=&quot;573&quot; data-origin-height=&quot;221&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;num_epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;lr = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.0001&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;input_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 칼럼 개수&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;hidden_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 은닉층 유닛 개수&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;num_layers=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# LSTM 계층 개수&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;num_classes=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 클래스 개수&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = LSTM(num_classes, input_size, hidden_size, num_layers, X_train_tensors_f.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;criterion = torch.nn.MSELoss()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = torch.optim.Adam(model.parameters(), lr=lr)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(num_epochs) :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; outputs = model.forward(X_train_tensors_f) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 전방향 학습&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss = criterion(outputs, y_train_tensors) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 오차 계싼&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss.backward() &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 기울기 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.step() &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 오차 업데이트&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch % &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; == &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Epoch : &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;, loss : &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{loss.item()&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:1.5f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;291&quot; data-origin-height=&quot;249&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/AaOMX/btsF61YC3N1/gk0s8rDQMoJCwsP2ZGNvUK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/AaOMX/btsF61YC3N1/gk0s8rDQMoJCwsP2ZGNvUK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/AaOMX/btsF61YC3N1/gk0s8rDQMoJCwsP2ZGNvUK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FAaOMX%2FbtsF61YC3N1%2Fgk0s8rDQMoJCwsP2ZGNvUK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;183&quot; height=&quot;157&quot; data-origin-width=&quot;291&quot; data-origin-height=&quot;249&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_x_ss = ss.transform(X)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_y_ms = ms.transform(y)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_x_ss = torch.Tensor(df_x_ss)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_y_ms = torch.Tensor(df_y_ms)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_x_ss = torch.reshape(df_x_ss, (df_x_ss.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, df_x_ss.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; sklearn.metrics &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; mean_squared_error, mean_absolute_error&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; np&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 예측값과 실제값 추출&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = model(df_x_ss).data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;actual = df_y_ms.data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# inverse_transform으로 정규화된 값을 다시 변환&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = ms.inverse_transform(predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;actual = ms.inverse_transform(actual)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# MSE 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;mse = mean_squared_error(actual, predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Mean Squared Error (MSE):&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, mse)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# MAE 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;mae = mean_absolute_error(actual, predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Mean Absolute Error (MAE):&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, mae)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# RMSE 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;rmse = np.sqrt(mse)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Root Mean Squared Error (RMSE):&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, rmse)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;424&quot; data-origin-height=&quot;85&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/BFMyN/btsF5Dc95hF/NNuyzs2KbiKNcDm2XeDJZ0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/BFMyN/btsF5Dc95hF/NNuyzs2KbiKNcDm2XeDJZ0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/BFMyN/btsF5Dc95hF/NNuyzs2KbiKNcDm2XeDJZ0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FBFMyN%2FbtsF5Dc95hF%2FNNuyzs2KbiKNcDm2XeDJZ0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;424&quot; height=&quot;85&quot; data-origin-width=&quot;424&quot; data-origin-height=&quot;85&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;train_predict = model(df_x_ss)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = train_predict.data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;label_y = df_y_ms.data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = ms.inverse_transform(predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;label_y = ms.inverse_transform(label_y)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.figure(figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.axvline(x=datetime(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2022&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), c=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'r'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, linestyle=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'--'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'pred'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] = predicted&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.plot(df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], label=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Actual Data'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.plot(df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'pred'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], label=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Predicted Data'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.title(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Time-series Prediction'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.legend()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1175&quot; data-origin-height=&quot;727&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cBDfN2/btsF7Y1nQvs/1U5RzeTNI1sniFniGkKjAk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cBDfN2/btsF7Y1nQvs/1U5RzeTNI1sniFniGkKjAk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cBDfN2/btsF7Y1nQvs/1U5RzeTNI1sniFniGkKjAk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcBDfN2%2FbtsF7Y1nQvs%2F1U5RzeTNI1sniFniGkKjAk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1175&quot; height=&quot;727&quot; data-origin-width=&quot;1175&quot; data-origin-height=&quot;727&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/176</guid>
      <comments>https://aibrain.tistory.com/176#entry176comment</comments>
      <pubDate>Wed, 27 Mar 2024 10:20:27 +0900</pubDate>
    </item>
    <item>
      <title>GRU로 주식예측</title>
      <link>https://aibrain.tistory.com/175</link>
      <description>&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision.transforms &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; transforms&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision.datasets&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; Parameter&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; Tensor&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn.functional &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; F&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.utils.data &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; DataLoader&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; plt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; datetime&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;GRU&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;nn&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;Module&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;__init__&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;num_classes&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;input_size&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;hidden_size&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;num_layers&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;seq_length&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; super(GRU, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;__init__&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_classes = num_classes&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_layers = num_layers&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.input_size = input_size&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size = hidden_size&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.seq_length = seq_length&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.gru = nn.GRU(input_size=input_size,hidden_size=hidden_size,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;num_layers=num_layers,batch_first=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc_1 = nn.Linear(hidden_size, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc = nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, num_classes)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.relu = nn.ReLU()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;forward&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; h_0 = torch.zeros(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.num_layers, x.size(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output, (hn) = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.gru(x, (h_0))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; hn = hn.view(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;-1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.hidden_size)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.relu(hn)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc_1(out)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.relu(out)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; out = &lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.fc(out)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; out&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.style.use(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'seaborn-whitegrid'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;device = torch.device(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cuda:0'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cpu'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;cuda = &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;Tensor = torch.cuda.FloatTensor &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; cuda &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.FloatTensor&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 난수 생성기의 시드(seed)를 설정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;torch.manual_seed(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;125&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; torch.cuda.manual_seed_all(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;125&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; yfinance &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; yf&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df = yf.download(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'005930.KS'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;start=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'2022-01-01'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;end=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'2022-05-31'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 005930 : 삼성전자 주가&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df.head()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;756&quot; data-origin-height=&quot;339&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/dZGmLX/btsF5vfjAap/K1I8aMyBgDpKmpPXGOusTk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/dZGmLX/btsF5vfjAap/K1I8aMyBgDpKmpPXGOusTk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/dZGmLX/btsF5vfjAap/K1I8aMyBgDpKmpPXGOusTk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdZGmLX%2FbtsF5vfjAap%2FK1I8aMyBgDpKmpPXGOusTk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;756&quot; height=&quot;339&quot; data-origin-width=&quot;756&quot; data-origin-height=&quot;339&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data check&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig = df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].plot()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;762&quot; data-origin-height=&quot;549&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/c7hl2h/btsF5ugneMp/jkU1lfk9YqKBjrSHLiFGuK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/c7hl2h/btsF5ugneMp/jkU1lfk9YqKBjrSHLiFGuK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/c7hl2h/btsF5ugneMp/jkU1lfk9YqKBjrSHLiFGuK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fc7hl2h%2FbtsF5ugneMp%2FjkU1lfk9YqKBjrSHLiFGuK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;762&quot; height=&quot;549&quot; data-origin-width=&quot;762&quot; data-origin-height=&quot;549&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X = df.drop(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, axis=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# X, y 분리&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y = df[[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]]&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; sklearn.preprocessing &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; StandardScaler, MinMaxScaler&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;ms = MinMaxScaler() &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 0 ~ 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;ss = StandardScaler() &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 평균 0, 분산 1&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_ss = ss.fit_transform(X)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_ms = ms.fit_transform(y)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_train = X_ss[:&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, :]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_test = X_ss[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:, :]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_train = y_ms[:&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, :]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_test = y_ms[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;79&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:, :]&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Training Shape :'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_train.shape, y_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Testing Shape :'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_test.shape, y_test.shape)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;333&quot; data-origin-height=&quot;55&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/b6cNUW/btsF6JjxHQy/my6JhHbjcp54sKGwlsXwmk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/b6cNUW/btsF6JjxHQy/my6JhHbjcp54sKGwlsXwmk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/b6cNUW/btsF6JjxHQy/my6JhHbjcp54sKGwlsXwmk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fb6cNUW%2FbtsF6JjxHQy%2Fmy6JhHbjcp54sKGwlsXwmk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;315&quot; height=&quot;52&quot; data-origin-width=&quot;333&quot; data-origin-height=&quot;55&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터셋 형태 및 크기 조정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_train_tensors = torch.Tensor(X_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_test_tensors = torch.Tensor(X_test)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_train_tensors = torch.Tensor(y_train)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_test_tensors = torch.Tensor(y_test)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_train_tensors_f = torch.reshape(X_train_tensors, (X_train_tensors.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_train_tensors.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;X_test_tensors_f = torch.reshape(X_test_tensors, (X_test_tensors.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_test_tensors.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Training Shape :'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, X_train.shape, y_train.shape)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;345&quot; data-origin-height=&quot;36&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/2Ryhb/btsF7umaJuf/zectx6ayyTdUZubQQ6j3Rk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/2Ryhb/btsF7umaJuf/zectx6ayyTdUZubQQ6j3Rk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/2Ryhb/btsF7umaJuf/zectx6ayyTdUZubQQ6j3Rk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2F2Ryhb%2FbtsF7umaJuf%2Fzectx6ayyTdUZubQQ6j3Rk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;297&quot; height=&quot;31&quot; data-origin-width=&quot;345&quot; data-origin-height=&quot;36&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;아래와 같이 X_train_tensors_f를 만들어준 이유는 &lt;span style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot;&gt;(데이터 개수, 시간 단계, 특성 개수)로 3차원 데이터로 만들어주기 위함입니다.&lt;/span&gt;&lt;span style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;584&quot; data-origin-height=&quot;226&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bURFFX/btsF56ziOYd/XgcELCVTFkkPghKr9EuCm1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bURFFX/btsF56ziOYd/XgcELCVTFkkPghKr9EuCm1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bURFFX/btsF56ziOYd/XgcELCVTFkkPghKr9EuCm1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbURFFX%2FbtsF56ziOYd%2FXgcELCVTFkkPghKr9EuCm1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;460&quot; height=&quot;178&quot; data-origin-width=&quot;584&quot; data-origin-height=&quot;226&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;num_epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;learning_rate = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.0001&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;input_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;5&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;hidden_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;num_layers=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;num_classes=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model=GRU(num_classes,input_size,hidden_size,num_layers,X_train_tensors_f.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;criterion = torch.nn.MSELoss()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(num_epochs) :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; outputs = model.forward(X_train_tensors_f)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss = criterion(outputs, y_train_tensors)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch % &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; == &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; :&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Epoch : &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;, loss : &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{loss.item()&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:1.5f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;310&quot; data-origin-height=&quot;250&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/Liise/btsF8t06A8A/ql80hTdsU1ocm8BxUGBuYk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/Liise/btsF8t06A8A/ql80hTdsU1ocm8BxUGBuYk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/Liise/btsF8t06A8A/ql80hTdsU1ocm8BxUGBuYk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FLiise%2FbtsF8t06A8A%2Fql80hTdsU1ocm8BxUGBuYk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;205&quot; height=&quot;165&quot; data-origin-width=&quot;310&quot; data-origin-height=&quot;250&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_x_ss = ss.transform(X)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_y_ms = ms.transform(y)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_x_ss = torch.Tensor(df_x_ss)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_y_ms = torch.Tensor(df_y_ms)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df_x_ss = torch.reshape(df_x_ss, (df_x_ss.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, df_x_ss.shape[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; sklearn.metrics &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; mean_squared_error, mean_absolute_error&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; np&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 예측값과 실제값 추출&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = model(df_x_ss).data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;actual = df_y_ms.data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# inverse_transform으로 정규화된 값을 다시 변환&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = ms.inverse_transform(predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;actual = ms.inverse_transform(actual)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# MSE 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;mse = mean_squared_error(actual, predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Mean Squared Error (MSE):&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, mse)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# MAE 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;mae = mean_absolute_error(actual, predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Mean Absolute Error (MAE):&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, mae)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# RMSE 계산&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;rmse = np.sqrt(mse)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;Root Mean Squared Error (RMSE):&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, rmse)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;432&quot; data-origin-height=&quot;89&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/m9Ms6/btsF5H0Qdnh/w7SO84DkhUeD7xBuZ9zARK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/m9Ms6/btsF5H0Qdnh/w7SO84DkhUeD7xBuZ9zARK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/m9Ms6/btsF5H0Qdnh/w7SO84DkhUeD7xBuZ9zARK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fm9Ms6%2FbtsF5H0Qdnh%2Fw7SO84DkhUeD7xBuZ9zARK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;359&quot; height=&quot;74&quot; data-origin-width=&quot;432&quot; data-origin-height=&quot;89&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;train_predict = model(df_x_ss)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = train_predict.data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;label_y = df_y_ms.data.numpy()&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;predicted = ms.inverse_transform(predicted)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;label_y = ms.inverse_transform(label_y)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.figure(figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.axvline(x=datetime(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2022&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), c=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'r'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, linestyle=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'--'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'pred'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] = predicted&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.plot(df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Close'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], label=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Actual Data'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.plot(df[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'pred'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], label=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Predicted Data'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.title(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'Time-series Prediction'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.legend()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1184&quot; data-origin-height=&quot;710&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/toIgy/btsF5UlnKcy/34OkH5KKAyUcbwQeEnURg0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/toIgy/btsF5UlnKcy/34OkH5KKAyUcbwQeEnURg0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/toIgy/btsF5UlnKcy/34OkH5KKAyUcbwQeEnURg0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FtoIgy%2FbtsF5UlnKcy%2F34OkH5KKAyUcbwQeEnURg0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1184&quot; height=&quot;710&quot; data-origin-width=&quot;1184&quot; data-origin-height=&quot;710&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/175</guid>
      <comments>https://aibrain.tistory.com/175#entry175comment</comments>
      <pubDate>Wed, 27 Mar 2024 10:09:29 +0900</pubDate>
    </item>
    <item>
      <title>Pytorch - GRU를 사용한 숫자형 예측 코드</title>
      <link>https://aibrain.tistory.com/174</link>
      <description>&lt;pre id=&quot;code_1711465274362&quot; class=&quot;python&quot; data-ke-language=&quot;python&quot; data-ke-type=&quot;codeblock&quot;&gt;&lt;code&gt;import torch
import torch.nn
import torchvision.transforms as transforms
import torchvision.datasets
from torch.nn import Parameter
import torch.nn as nn
from torch import Tensor

import torch.nn.functional as F
from torch.utils.data import DataLoader
import math

import matplotlib.pyplot as plt
from datetime import datetime
import seaborn as sns

class GRU(nn.Module) :
    def __init__(self, num_classes, input_size, hidden_size, num_layers, seq_length) :
        super(GRU, self).__init__()
        self.num_classes = num_classes
        self.num_layers = num_layers
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.seq_length = seq_length

        self.gru = nn.GRU(input_size=input_size,hidden_size=hidden_size,
                         num_layers=num_layers,batch_first=True)
        self.fc_1 = nn.Linear(hidden_size, 128)
        self.fc = nn.Linear(128, num_classes)
        self.relu = nn.ReLU()

    def forward(self, x) :
        h_0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size)
        output, (hn) = self.gru(x, (h_0))
        hn = hn.view(-1, self.hidden_size)
        out = self.relu(hn)
        out = self.fc_1(out)
        out = self.relu(out)
        out = self.fc(out)
        return out
        
        plt.style.use('seaborn-whitegrid')

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

cuda = True if torch.cuda.is_available() else False
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

# 난수 생성기의 시드(seed)를 설정
torch.manual_seed(125)

if torch.cuda.is_available() :
    torch.cuda.manual_seed_all(125)
import yfinance as yf
df = yf.download('005930.KS',
                     start='2022-01-01',
                     end='2022-05-31') # 005930 : 삼성전자 주가

df.head()

# data check
fig = df['Close'].plot()

X = df.drop('Close', axis=1) # X, y 분리
y = df[['Close']]

from sklearn.preprocessing import StandardScaler, MinMaxScaler

ms = MinMaxScaler() # 0 ~ 1
ss = StandardScaler() # 평균 0, 분산 1

X_ss = ss.fit_transform(X)
y_ms = ms.fit_transform(y)

X_train = X_ss[:79, :]
X_test = X_ss[79:, :]

y_train = y_ms[:79, :]
y_test = y_ms[79:, :]

print('Training Shape :', X_train.shape, y_train.shape)
print('Testing Shape :', X_test.shape, y_test.shape)

# 데이터셋 형태 및 크기 조정
X_train_tensors = torch.Tensor(X_train)
X_test_tensors = torch.Tensor(X_test)

y_train_tensors = torch.Tensor(y_train)
y_test_tensors = torch.Tensor(y_test)

X_train_tensors_f = torch.reshape(X_train_tensors, (X_train_tensors.shape[0], 1, X_train_tensors.shape[1]))

X_test_tensors_f = torch.reshape(X_test_tensors, (X_test_tensors.shape[0], 1, X_test_tensors.shape[1]))

print('Training Shape :', X_train.shape, y_train.shape)

X_train_tensors[0]
#tensor([ 1.9638,  2.0233,  2.0394,  2.0632, -0.0441])

X_train_tensors_f[0]

num_epochs = 1000
learning_rate = 0.0001

input_size=5
hidden_size=2
num_layers=1

num_classes=1

model=GRU(num_classes,input_size,hidden_size,num_layers,X_train_tensors_f.shape[1])

criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# tensor([[ 1.9638,  2.0233,  2.0394,  2.0632, -0.0441]])

for epoch in range(num_epochs) :
    outputs = model.forward(X_train_tensors_f)
    optimizer.zero_grad()
    loss = criterion(outputs, y_train_tensors)
    loss.backward()

    optimizer.step()
    if epoch % 100 == 0 :
        print(f'Epoch : {epoch}, loss : {loss.item():1.5f}')
        
df_x_ss = ss.transform(X)
df_y_ms = ms.transform(y)

df_x_ss = torch.Tensor(df_x_ss)
df_y_ms = torch.Tensor(df_y_ms)
df_x_ss = torch.reshape(df_x_ss, (df_x_ss.shape[0], 1, df_x_ss.shape[1]))

from sklearn.metrics import mean_squared_error, mean_absolute_error
import numpy as np

# 예측값과 실제값 추출
predicted = model(df_x_ss).data.numpy()
actual = df_y_ms.data.numpy()

# inverse_transform으로 정규화된 값을 다시 변환
predicted = ms.inverse_transform(predicted)
actual = ms.inverse_transform(actual)

# MSE 계산
mse = mean_squared_error(actual, predicted)
print(&quot;Mean Squared Error (MSE):&quot;, mse)

# MAE 계산
mae = mean_absolute_error(actual, predicted)
print(&quot;Mean Absolute Error (MAE):&quot;, mae)

# RMSE 계산
rmse = np.sqrt(mse)
print(&quot;Root Mean Squared Error (RMSE):&quot;, rmse)

# Mean Squared Error (MSE): 2250454.0
# Mean Absolute Error (MAE): 1058.7194
# Root Mean Squared Error (RMSE): 1500.1514

train_predict = model(df_x_ss)
predicted = train_predict.data.numpy()

label_y = df_y_ms.data.numpy()

predicted = ms.inverse_transform(predicted)
label_y = ms.inverse_transform(label_y)
plt.figure(figsize=(10, 6))
plt.axvline(x=datetime(2022,5,1), c='r', linestyle='--')

df['pred'] = predicted
plt.plot(df['Close'], label='Actual Data')
plt.plot(df['pred'], label='Predicted Data')

plt.title('Time-series Prediction')
plt.legend()
plt.show()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1160&quot; data-origin-height=&quot;701&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/dsVONK/btsF8l9OhSc/DcytPxuP3d6XU884Da9OH0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/dsVONK/btsF8l9OhSc/DcytPxuP3d6XU884Da9OH0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/dsVONK/btsF8l9OhSc/DcytPxuP3d6XU884Da9OH0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdsVONK%2FbtsF8l9OhSc%2FDcytPxuP3d6XU884Da9OH0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1160&quot; height=&quot;701&quot; data-origin-width=&quot;1160&quot; data-origin-height=&quot;701&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/174</guid>
      <comments>https://aibrain.tistory.com/174#entry174comment</comments>
      <pubDate>Wed, 27 Mar 2024 00:03:23 +0900</pubDate>
    </item>
    <item>
      <title>EfficientNet-B4를 사용한 포켓몬 분류</title>
      <link>https://aibrain.tistory.com/172</link>
      <description>&lt;h2 style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size26&quot;&gt;1. &lt;span style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot;&gt;EfficientNet이란?&lt;/span&gt;&lt;/h2&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;EfficientNet은 Neural Architecture Search (NAS)를 통해 찾아진 효율적이고 강력한 컨볼루션 신경망 구조입니다. EfficientNet은 모델의 성능과 효율성을 극대화하기 위해 네트워크의 깊이, 너비 및 해상도를 동시에 조정하는 Compound Scaling을 적용합니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;EfficientNet 모델은 네트워크의 깊이, 너비 및 해상도를 조절하는 세 가지 스케일 요소를 사용하여 조정됩니다. 각 스케일 요소는 계수를 사용하여 특정 크기의 모델을 조정하고, 이러한 스케일 요소들은 서로 독립적으로 변환될 수 있습니다. 이것은 네트워크의 파라미터 수, 연산 비용 및 메모리 요구 사항을 최소화하면서 성능을 향상시키는 효과적인 방법을 제공합니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;EfficientNet은 기본적으로 Depthwise Separable Convolution과 Swish 활성화 함수를 사용하여 경량화 및 성능 향상을 달성합니다. 이러한 구성 요소들은 모델의 파라미터 수를 줄이고 연산량을 감소시키면서도 효율적인 특징 추출을 가능하게 합니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;EfficientNet의 여러 가지 버전들(EfficientNet-B0부터 B7까지)이 있으며, 각 버전은 네트워크의 크기를 조정하여 다양한 용도와 환경에 적합하도록 설계되었습니다. EfficientNet-B4는 그 중 하나로, 중간 크기의 모델로써 효율성과 성능 사이의 균형을 제공합니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;요약하자면, EfficientNet은 파라미터와 연산량을 줄이면서도 높은 성능을 제공하는 효율적인 신경망 아키텍처입니다. EfficientNet-B4는 그 중 하나로 중간 크기의 모델이며, 다양한 컴퓨터 비전 작업에 유용하게 사용될 수 있습니다.&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #0d0d0d; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;2. 포켓몬 분류&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;Train :&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://www.kaggle.com/datasets/thedagger/pokemon-generation-one&quot;&gt;https://www.kaggle.com/datasets/thedagger/pokemon-generation-one&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Validation :&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://www.kaggle.com/hlrhegemony/pokemon-image-dataset&quot;&gt;https://www.kaggle.com/hlrhegemony/pokemon-image-dataset&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;두 파일의 포켓몬 종류가 서로 다름으로 나중에 맞춰줄 예정&lt;/p&gt;
&lt;p style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; os&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;os.environ[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'KAGGLE_USERNAME'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'아이디'&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;os.environ[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'KAGGLE_KEY'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'본인APIKEY'&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;kaggle datasets download -d thedagger/pokemon-generation-one&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;kaggle datasets download -d hlrhegemony/pokemon-image-dataset&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;unzip -q pokemon-generation-one.zip&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;unzip -q pokemon-image-dataset.zip&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# train 폴더 생성해서 dataset안의 파일을 train으로 이동&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# !mv 현재폴더 이동폴더&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;mv dataset train&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;rm -rf train/dataset &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터가 중복되어 있음&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# validation 폴더 생성해서 images안의 파일을 validation으로 이동&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;mv images validation&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;train_labels = os.listdir(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(train_labels))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 149&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;폴더가 각각의 포켓몬 이름으로 구성되어 있음으로 폴더의 수는 포켓몬 수입니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;즉, train에는 149종류의 포켓몬의 사진이 들어있습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;val_labels = os.listdir(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(val_labels))&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;#&lt;span&gt; 898&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;validation에는 898종류의 포켓몬 사진이 들어있습니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;둘이 맞지 않네요. train으로 학습시키지 않은 포켓몬을 validation에 넣어봤자겠죠.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# train : 149&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# validation : 898&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# validation에서 train에 있는 디렉토리를 확인하여 없는 디렉토리를 모두 제거&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; shutil&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# train에 있는데 validation에 없는 디렉토리 제거&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; val_label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; val_labels:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; val_label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;not&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; train_labels:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; shutil.rmtree(os.path.join(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,val_label))&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;train_labels = os.listdir(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(train_labels))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;validation_labels = os.listdir(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(validation_labels))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 149&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 147&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;여전히 맞지 않네요. train에 있는 애들중에 validation에 없는 애들이 있다는 뜻입니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;뭔지 확인하고 그 폴더를 생성해주겠습니다. 그리고 저 포켓몬에 맞는 사진을 몇개 인터넷에서 구해와서 직접 추가해주었습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; train_label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; train_labels:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; train_label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;not&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; val_labels:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(train_label)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; os.makedirs(os.path.join(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, train_label), exist_ok=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# MrMime&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# Farfetchd&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;데이터 전처리&lt;/h3&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 필요한 모듈 준비&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.optim &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; optim&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; plt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; datasets, models, transforms&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.utils.data &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; DataLoader&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# GPU 사용 확인&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;device = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cuda'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cpu'&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(device)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이미지 증강 기법&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data_transforms&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# train / validation&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;data_transforms = {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# Compose : 데이터에 처리하고자하는 함수들을 한번에 처리해주는 기능&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : transforms.Compose([&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.Resize((&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# RandomAffine() : 데이터를 랜덤하게 뽑아서 변환시켜주겠다는 의미&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 각도 : 0(변환x), shear(찌그러트림) , acale : 변환된 이미지의 크기는 원본 이미지 크기의 80%에서 120% 사이에서 무작위로 선택&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.RandomAffine(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, shear =&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, scale=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1.2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# RandomHorizontalFlip 이미지를 수평으로 뒤집기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.RandomHorizontalFlip(),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.ToTensor()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ]),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : transforms.Compose([&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.Resize((&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.ToTensor()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터셋 객체 생성&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# Image_datasets&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# train / validation&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;target_transforms&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.LongTensor([target])&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터셋 객체 만들어서 가져와줌&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;image_datasets = {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : datasets.ImageFolder(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 해당 위치의 파일에서&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data_transforms의 train을 가져와서&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; target_transform = target_transforms &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이 함수 써줌(콜백함수 개념)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : datasets.ImageFolder(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 해당 위치의 파일에서&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data_transforms의 train을 가져와서&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; target_transform = target_transforms &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이 함수 써줌(콜백함수 개념)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; )&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;989&quot; data-origin-height=&quot;268&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/L4boN/btsF4WjCsDJ/YfXFxsMEkTKx40VkCiUU41/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/L4boN/btsF4WjCsDJ/YfXFxsMEkTKx40VkCiUU41/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/L4boN/btsF4WjCsDJ/YfXFxsMEkTKx40VkCiUU41/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FL4boN%2FbtsF4WjCsDJ%2FYfXFxsMEkTKx40VkCiUU41%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;989&quot; height=&quot;268&quot; data-origin-width=&quot;989&quot; data-origin-height=&quot;268&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터로더 생성&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# dataloaders&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# batch_size = 32&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# shuffle = True&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;dataloaders = {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : DataLoader(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; batch_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; shuffle=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;: DataLoader(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; batch_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; shuffle=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; )&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]),&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 10657 661&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이미지 4*8로 출력&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;imgs, labels = &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;iter&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig, axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; ax, img, label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;zip&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(axes.flatten(), imgs, labels):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 찍으려면 가로, 세로, 컬러 순서여야해서 permute 해줌&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.imshow(img.permute(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.set_title(label)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1984&quot; data-origin-height=&quot;1011&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cjdSal/btsF4YPbX5Q/4kxRP41KrRkAqXfDE2c791/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cjdSal/btsF4YPbX5Q/4kxRP41KrRkAqXfDE2c791/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cjdSal/btsF4YPbX5Q/4kxRP41KrRkAqXfDE2c791/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcjdSal%2FbtsF4YPbX5Q%2F4kxRP41KrRkAqXfDE2c791%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1984&quot; height=&quot;1011&quot; data-origin-width=&quot;1984&quot; data-origin-height=&quot;1011&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;클래스 확인하는 법&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;102번 사진의 클래스 이름&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].classes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;101&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;# &lt;span style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;Pikachu&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;imgs, labels = &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;iter&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig, axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; ax, img, label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;zip&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(axes.flatten(), imgs, labels):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 찍으려면 가로, 세로, 컬러 순서여야해서 permute 해줌&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.imshow(img.permute(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.set_title(label)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1977&quot; data-origin-height=&quot;753&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/CMqCd/btsF8nzLfgF/kay2JkVLBa7i5QyIkGboRK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/CMqCd/btsF8nzLfgF/kay2JkVLBa7i5QyIkGboRK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/CMqCd/btsF8nzLfgF/kay2JkVLBa7i5QyIkGboRK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FCMqCd%2FbtsF8nzLfgF%2Fkay2JkVLBa7i5QyIkGboRK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1977&quot; height=&quot;753&quot; data-origin-width=&quot;1977&quot; data-origin-height=&quot;753&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision.models &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; efficientnet_b4, EfficientNet_B4_Weights&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision.models._api &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; WeightsEnum&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.hub &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; load_state_dict_from_url&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# WeightsEnum.get_state_dict를 재정의&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;get_state_dict&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, *&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;args&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, **&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;kwargs&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; kwargs.pop(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;check_hash&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; load_state_dict_from_url(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.url, *args, **kwargs)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;WeightsEnum.get_state_dict = get_state_dict&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;efficientnet_b4(weights=EfficientNet_B4_Weights.IMAGENET1K_V1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = efficientnet_b4(weights=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;DEFAULT&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).to(device)&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# FX Layer 수정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; param &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; model.parameters():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; param.requires_grad = &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 가져온 파라미터 (W, b)를 업데이트하지 않음&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model.classifier = nn.Sequential(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1792&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.ReLU(),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(val_labels)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).to(device)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 학습&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = optim.Adam(model.classifier.parameters(), lr=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.001&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(epochs):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이렇게 되면 학습이 끝날때 모델이 자동으로 model.eval()가 된 상태로 나옴&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; phase &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; phase == &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; model.train()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; model.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;eval&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_losses = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_accs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; x_batch, y_batch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; dataloaders[phase]:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x_batch, y_batch = x_batch.to(device), y_batch.to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_pred = model(x_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #008000; text-align: start;&quot;&gt;&lt;span&gt; 보통 이런 다중분류의 출력값은 2차원임으로 1차원으로 줄여주고 정수형으로 변환&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_batch = torch.flatten(y_batch).long()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;#&lt;span&gt; 다중분류문제임으로 CrossEntropyLoss&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;&lt;span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; # CrossEntropyLoss는 Softmax를 이미 포함하고 있음으로 무조건 Sigmoid해주던 이진분류와는 조금 다름&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; loss = nn.CrossEntropyLoss()(y_pred, y_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; phase == &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_losses = sum_losses + loss&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_prob = nn.Softmax(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)(y_pred)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_pred_index = torch.argmax(y_prob, axis=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; acc = (y_batch == y_pred_index).&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;() / &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(y_batch) * &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_accs = sum_accs + acc&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; avg_loss = sum_losses / &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[phase])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; avg_acc = sum_accs / &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[phase])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{phase&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:10s&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;: Epoch &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch+&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1:4d&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epochs}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Loss: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{avg_loss&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:.4f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Accuracy: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{avg_acc: &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;.2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;f}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;%'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 모델 저장&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;torch.save(model.state_dict(),&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'model.pth'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# model.h5도 가능&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;모델 불러오기&lt;/h2&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 학습이 안된 날것의 모델 불러오기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = models.efficientnet_b4().to(device)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# FX Layer 수정&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; param &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; model.parameters():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; param.requires_grad = &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 가져온 파라미터 (W, b)를 업데이트하지 않음&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model.classifier = nn.Sequential(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1792&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.ReLU(),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(val_labels)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).to(device)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 모델 불러오기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model.load_state_dict(torch.load(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'model.pth'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;eval&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;모델평가&lt;/h3&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; PIL &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; Image&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img1 = Image.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'./validation/Ditto/0.jpg'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img2 = Image.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'./validation/Charmander/0.jpg'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig,axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 틀 안나오게&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img2)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 틀 안나오게&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1290&quot; data-origin-height=&quot;669&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bbSE6g/btsF8mt48QO/ymEo8WkiAWtow9UQigtQK1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bbSE6g/btsF8mt48QO/ymEo8WkiAWtow9UQigtQK1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bbSE6g/btsF8mt48QO/ymEo8WkiAWtow9UQigtQK1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbbSE6g%2FbtsF8mt48QO%2FymEo8WkiAWtow9UQigtQK1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1290&quot; height=&quot;669&quot; data-origin-width=&quot;1290&quot; data-origin-height=&quot;669&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이 귀여운 메타몽과 파이리를 모델에 넣어 어떤 결과가 나오는지 보겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img1_input = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;](img1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img2_input = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;](img2)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(img1_input.shape,img2_input.shape)&lt;/span&gt;&lt;/span&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# torch.Size([3, 224, 224]) torch.Size([3, 224, 224])&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;test_batch = torch.stack([img1_input,img2_input])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;test_batch = test_batch.to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;test_batch.shape&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# torch.Size([2, 3, 224, 224])&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred = model(test_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_prob = nn.Softmax(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)(y_pred)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred_index = torch.argmax(y_prob, axis=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 상위 3개 값을 얻기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;topk_values, topk_indices = torch.topk(y_prob, k=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, dim=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;title_list = []&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; title = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;''&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; j &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; title += &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].classes[topk_indices[i][j]]}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{topk_values[i][j] * &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100:.2f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;% &quot;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; title_list.append(title)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig,axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 틀 안나오게&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].set_title(title_list[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img2)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 틀 안나오게&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].set_title(title_list[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# topk_indices&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1333&quot; data-origin-height=&quot;688&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/b08f1p/btsF40M3TLn/o40vLPY09NfntKweDt1LX0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/b08f1p/btsF40M3TLn/o40vLPY09NfntKweDt1LX0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/b08f1p/btsF40M3TLn/o40vLPY09NfntKweDt1LX0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fb08f1p%2FbtsF40M3TLn%2Fo40vLPY09NfntKweDt1LX0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1333&quot; height=&quot;688&quot; data-origin-width=&quot;1333&quot; data-origin-height=&quot;688&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;아주 잘나오네요.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이번엔 저희집 고양이 아리를 넣어보겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;my_img = Image.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'/content/KakaoTalk_20240112_102619252.jpg'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;my_img_input = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;](my_img)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;my_batch = torch.stack([my_img_input])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;my_batch = my_batch.to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;my_pred = model(my_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;my_prob = nn.Softmax(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)(my_pred)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;val, idx = torch.topk(my_prob, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig, axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(my_img)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].set_title(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'{:2f}% {}, {:2f}% {}, {:2f}% {}'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; val[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] * &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].classes[idx[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; val[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] * &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].classes[idx[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; val[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] * &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].classes[idx[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;src = image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].classes[idx[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;i = Image.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{src}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;/0.jpg'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(i)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1605&quot; data-origin-height=&quot;692&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/IBK6B/btsF7UkuiLD/jFXbdl9RVdmXPBumliFo5K/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/IBK6B/btsF7UkuiLD/jFXbdl9RVdmXPBumliFo5K/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/IBK6B/btsF7UkuiLD/jFXbdl9RVdmXPBumliFo5K/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FIBK6B%2FbtsF7UkuiLD%2FjFXbdl9RVdmXPBumliFo5K%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1605&quot; height=&quot;692&quot; data-origin-width=&quot;1605&quot; data-origin-height=&quot;692&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;오.... 컬러도 그렇고 좀 닮은것같네요 귀엽네요...&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이렇게 EfficientNet-B4를 사용해서 이미지 분류를 해보았습니다.&amp;nbsp;&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/172</guid>
      <comments>https://aibrain.tistory.com/172#entry172comment</comments>
      <pubDate>Tue, 26 Mar 2024 21:37:18 +0900</pubDate>
    </item>
    <item>
      <title>ResNet50를 사용하여 이미지 분류</title>
      <link>https://aibrain.tistory.com/171</link>
      <description>&lt;p data-ke-size=&quot;size16&quot;&gt;우선, 케글에서 데이터셋을 받아옵니다.&lt;/p&gt;
&lt;div id=&quot;cell-glIRHgI0K0Hr&quot; style=&quot;color: #212121; text-align: start;&quot;&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #000000;&quot;&gt;
&lt;div&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;에일리언 vs 프레데터 데이터셋&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; color: #000000;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://www.kaggle.com/datasets/pmigdal/alien-vs-predator-images&quot;&gt;https://www.kaggle.com/datasets/pmigdal/alien-vs-predator-images&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;cell-UM4iWXjfLU-6&quot; style=&quot;color: #212121; text-align: start;&quot;&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #000000;&quot;&gt;
&lt;div&gt;&lt;i&gt;&lt;/i&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;케글 데이터를 바로 받아오는 법&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; color: #000000;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;케글 로그인 _&amp;gt; 우측 상단 계정 클릭 -&amp;gt; Your Profile -&amp;gt; API New Create Token -&amp;gt; kaggle.json다운&lt;/li&gt;
&lt;li&gt;{&quot;username&quot;:&quot;본인의 아이디&quot;,&quot;key&quot;:&quot;본인api키&quot;}&lt;/li&gt;
&lt;li&gt;원하는 데이터의 COPY API COMMAND 한걸 !달아서 실행&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; os&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;os.environ[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'KAGGLE_USERNAME'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'아이디'&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;os.environ[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'KAGGLE_KEY'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;] = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'API키'&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;kaggle datasets download -d pmigdal/alien-vs-predator-images&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이렇게 되면 zip파일로 받아와지는데 zip파일을 압축 해제하는 것도 코드로 가능합니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;unzip -q alien-vs-predator-images.zip&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이제 모델을 돌리기 위해 데이터 정제를 하겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.nn &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; nn&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.optim &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; optim&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; plt&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torchvision &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; datasets, models, transforms&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.utils.data &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; DataLoader&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;device = &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cuda'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.cuda.is_available() &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'cpu'&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(device)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;이미지 증강 기법(Image Augmentation)&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;원본 이미지 데이터를 조작하려 원본과는 다른 변화를 가진 이미지를 생성&lt;/li&gt;
&lt;li&gt;일반적으로 모델 성능이 좋아짐&lt;/li&gt;
&lt;li&gt;오버피팅을 방지&lt;/li&gt;
&lt;/ul&gt;
&lt;p style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-size=&quot;size16&quot;&gt;&lt;a href=&quot;https://pytorch.org/vision/master/transforms.html&quot;&gt;https://pytorch.org/vision/master/transforms.html&lt;/a&gt;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이때, train의 데이터만 증강을 거치고 test는 증강을 하지 않음으로 이 둘을 나눠서 작성해줍니다&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;data_transforms = {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# Compose : 데이터에 처리하고자하는 함수들을 한번에 처리해주는 기능&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : transforms.Compose([&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.Resize((&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# RandomAffine() : 데이터를 랜덤하게 뽑아서 변환시켜주겠다는 의미&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 각도 : 0(변환x), shear(찌그러트림) , acale : 변환된 이미지의 크기는 원본 이미지 크기의 80%에서 120% 사이에서 무작위로 선택&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.RandomAffine(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, shear =&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, scale=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1.2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# RandomHorizontalFlip 이미지를 수평으로 뒤집기&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.RandomHorizontalFlip(),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.ToTensor()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ]),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : transforms.Compose([&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.Resize((&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;224&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transforms.ToTensor()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;보통 분류문제에서는 LongTensor를 사용하나 이진분류의 경우 손실함수를 Binary Cross Entropy Loss로 사용합니다. 때문에, LongTensor로 Y값을 정할 경우 에러가 나게 됨으로 이진분류시 target은 FloatTensor로 설정하면 됩니다&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;target_transforms&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #001080;&quot;&gt;target&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; torch.FloatTensor([target])&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;이터셋 객체 만들어서 가져와줌&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이렇게 할 경우, 폴더에서 파일을 불러오면서 transform을 통해 증강 및 resize와 같은 전처리를 할 수 있음&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;image_datasets = {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : datasets.ImageFolder(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; root=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'data/train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 데이터셋의 루트 디렉토리&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transform = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이미지에 적용할 변환&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; target_transform=target_transforms &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 레이블에 적용할 변환&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : datasets.ImageFolder(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'data/validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 해당 위치의 파일에서&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; transform = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data_transforms의 train을 가져와서&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; target_transform = target_transforms &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이 함수 써줌(콜백함수 개념)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; )&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;# 데이터 불러오는 것이 끝났으니 DataLoader로 데이터를 배치별로 묶어줍니다&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;dataloaders = {&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; : DataLoader(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; batch_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; shuffle=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;True&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;: DataLoader(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; batch_size=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;32&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; shuffle=&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; )&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]),&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(image_datasets[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 694 200&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;imgs, labels = &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;next&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;iter&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig, axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;16&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; ax, img, label &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;zip&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(axes.flatten(), imgs, labels):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 찍으려면 가로, 세로, 컬러 순서여야해서 permute 해줌&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.imshow(img.permute(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.set_title(label.item())&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; ax.axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignLeft&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1754&quot; data-origin-height=&quot;906&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/c6RbYW/btsF71wRRl6/woMP5azMrHQUPl1aRNWzU0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/c6RbYW/btsF71wRRl6/woMP5azMrHQUPl1aRNWzU0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/c6RbYW/btsF71wRRl6/woMP5azMrHQUPl1aRNWzU0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fc6RbYW%2FbtsF71wRRl6%2FwoMP5azMrHQUPl1aRNWzU0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1754&quot; height=&quot;906&quot; data-origin-width=&quot;1754&quot; data-origin-height=&quot;906&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;사실 전 영화를 잘 안봐서 predator가 뭔지 잘 모릅니다. 위의 사진을 보고도 구분하기 어렵네요. 하지만 데이터가 있으니 학습을 시켜 저 대신에 모델이 구분할 수 있도록 만들어보겠습니다.&lt;/p&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;ResNet50 불러오기&lt;/h3&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model = models.resnet50(weights=&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'IMAGENET1K_V1'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; param &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; model.parameters():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; param.requires_grad = &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 가져온 파라미터 (W, b)를 업데이트하지 않음&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model.fc = nn.Sequential(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2048&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.ReLU(),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이진분류니까 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Sigmoid()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).to(device)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이렇게 모델을 프린트하면 엄청나게 복잡하고 층층이 쌓인 모델 구조를 볼 수 있는데요. 이 모델은 다른 데이터로 이미 학습된 전이학습 모델이기에 그냥 써서는 우리가 원하는 분류를 할 수 없습니다.&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;967&quot; data-origin-height=&quot;319&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bC8WjX/btsF560chs7/72TNsSKFykyiemOwMfJFP0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bC8WjX/btsF560chs7/72TNsSKFykyiemOwMfJFP0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bC8WjX/btsF560chs7/72TNsSKFykyiemOwMfJFP0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbC8WjX%2FbtsF560chs7%2F72TNsSKFykyiemOwMfJFP0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;967&quot; height=&quot;319&quot; data-origin-width=&quot;967&quot; data-origin-height=&quot;319&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;모델 구조 맨 아래를 보시면 fc라고 보이지죠 거기가 저희가 입맛대로, 데이터대로 고쳐야하는 곳입니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;지금은 2048이 들어와 1000개의 아웃풋으로 나오는데 2048은 모델의 피처맵수이고 1000은 이 모델이 최종적으로 1000개의 데이터를 구분할 수 있다는 뜻입니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;하지만 우리는 이제 외계인과 수호자를 구분하니 저 곳의 아웃풋을 1로 만들어줘야 합니다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;2048개의 특징을 가지고 바로 이진분류를 하면 오류가 많이 생길 수 있고 성능이 잘 안나올 수 있으니 한줄한줄 줄여가면서 만들어보겠습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;모델 수정&lt;/h1&gt;
&lt;/div&gt;
&lt;h1 style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;5. Freeze Layers&lt;/h1&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;특징을 Qhqdksosms CNN의 앞쪽 컨볼루션 레이어들은 학습을하지 않도록 설정&lt;/li&gt;
&lt;li&gt;출력부분의 레이어(fc)를 다시 설정하며 분류에 맞게 변경&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; param &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; model.parameters():&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; param.requires_grad = &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;False&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 가져온 파라미터 (W, b)를 업데이트하지 않음&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;model.fc = nn.Sequential(&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2048&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.ReLU(),&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Linear(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이진분류니까 1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;&amp;nbsp; &amp;nbsp; # 이진분류니까 Sigmoid 사용 사실 여기가 아니라 모델 밖에서 사용해도됨&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; nn.Sigmoid()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).to(device)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(model)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;958&quot; data-origin-height=&quot;432&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/dPWQDJ/btsF3m34smf/FAaWDenIZRmUTeJcKXzGb0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/dPWQDJ/btsF3m34smf/FAaWDenIZRmUTeJcKXzGb0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/dPWQDJ/btsF3m34smf/FAaWDenIZRmUTeJcKXzGb0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FdPWQDJ%2FbtsF3m34smf%2FFAaWDenIZRmUTeJcKXzGb0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;958&quot; height=&quot;432&quot; data-origin-width=&quot;958&quot; data-origin-height=&quot;432&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이제 모델이 저희 데이터에 맞는 출력값을 뱉어낼 수 있게 되었습니다.&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 학습&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;optimizer = optim.Adam(model.fc.parameters(), lr=&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.001&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;epochs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;10&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; epoch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;range&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(epochs):&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 이렇게 되면 학습이 끝날때 모델이 자동으로 model.eval()가 된 상태로 나옴&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; phase &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;]:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; phase == &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; model.train()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; model.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;eval&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_losses = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_accs = &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; x_batch, y_batch &lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; dataloaders[phase]:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x_batch = x_batch.to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_batch = y_batch.to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_pred = model(x_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; loss = nn.BCELoss()(y_pred, y_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; phase == &lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'train'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; optimizer.zero_grad()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; loss.backward()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; optimizer.step()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_losses = sum_losses + loss&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y_bool = (y_pred &amp;gt;= &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0.5&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;).&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; acc = (y_batch == y_bool).&lt;/span&gt;&lt;span style=&quot;color: #257693;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;sum&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;() / &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(y_batch) * &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sum_accs = sum_accs + acc&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; avg_loss = sum_losses / &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[phase])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; avg_acc = sum_accs / &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(dataloaders[phase])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{phase&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:10s&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;: Epoch &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epoch+&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1:4d&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{epochs}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Loss: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{avg_loss&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;:.4f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt; Accuracy: &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{avg_acc: &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;.2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;f}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;%'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;※참고 : 이미지데이터 열기&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; PIL &lt;/span&gt;&lt;span style=&quot;color: #af00db;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt; Image&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img1 = Image.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'./data/validation/alien/19.jpg'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img2 = Image.&lt;/span&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;open&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'./data/validation/predator/20.jpg'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig, axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img2)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1230&quot; data-origin-height=&quot;656&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/CNd35/btsF7TZ7aTX/K3Ak99hQ4Qo7TORKwHEgLK/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/CNd35/btsF7TZ7aTX/K3Ak99hQ4Qo7TORKwHEgLK/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/CNd35/btsF7TZ7aTX/K3Ak99hQ4Qo7TORKwHEgLK/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FCNd35%2FbtsF7TZ7aTX%2FK3Ak99hQ4Qo7TORKwHEgLK%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1230&quot; height=&quot;656&quot; data-origin-width=&quot;1230&quot; data-origin-height=&quot;656&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data_transforms는 내가 위에서 설정한 애로 사이즈 224,224로 재설정하고 텐서형태로 변환되어있음&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 그럼으로 새로 불러온 img1과 img2 둘다 같은 전처리를 거쳐야함&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# data_transforms에는 Compose클래스가 가진 __call__이라는 스페셜 메서드가 있음으로 함수로 정하지 않았지만 ()를 붙여서 이미지를 넣으면 함수처럼 이미지가 &lt;span style=&quot;background-color: #f7f7f7; color: #008000; text-align: start;&quot;&gt;data_transforms안에 설정된 것들이 적용됨&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img1_input = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;](img1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;img2_input = data_transforms[&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'validation'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;](img2)&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;color: #795e26;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;(img1_input.shape,img2_input.shape)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# torch.Size([3, 224, 224]) torch.Size([3, 224, 224])&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 배치로 묶어서 모델에 연결&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# 2,3,224,224 차원으로 img1_input와 img2_input를 쌓음&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #008000;&quot;&gt;# stack : 차원을 쌓아주는 함수&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;test_batch = torch.stack([img1_input,img2_input])&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;test_batch = test_batch.to(device)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;test_batch.shape&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;
&lt;pre class=&quot;angelscript&quot; style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;&lt;code&gt;torch.Size([2, 3, 224, 224])&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred = model(test_batch)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;y_pred&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;pre class=&quot;lua&quot; style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;&lt;code&gt;tensor([[0.1115],
        [0.9516]], device='cuda:0', grad_fn=&amp;lt;SigmoidBackward0&amp;gt;)&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: #f7f7f7; color: #000000;&quot;&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;fig, axes = plt.subplots(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, figsize=(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;))&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].set_title(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;-y_pred[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])*&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100:.2f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;% Alien, &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{(y_pred[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])*&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100:.2f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;% Predator'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img1)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].set_title(&lt;/span&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{(&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;-y_pred[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])*&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100:.2f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;% Alien, &lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;{(y_pred[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;])*&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;100:.2f&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;% Predator'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].imshow(img2)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;axes[&lt;/span&gt;&lt;span style=&quot;color: #116644;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;].axis(&lt;/span&gt;&lt;span style=&quot;color: #a31515;&quot;&gt;'off'&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span style=&quot;color: #000000;&quot;&gt;plt.show()&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1237&quot; data-origin-height=&quot;677&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/RrcMu/btsF5tO9Bil/5N2iBK8USBEcXuKcYKZg1k/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/RrcMu/btsF5tO9Bil/5N2iBK8USBEcXuKcYKZg1k/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/RrcMu/btsF5tO9Bil/5N2iBK8USBEcXuKcYKZg1k/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FRrcMu%2FbtsF5tO9Bil%2F5N2iBK8USBEcXuKcYKZg1k%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1237&quot; height=&quot;677&quot; data-origin-width=&quot;1237&quot; data-origin-height=&quot;677&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/171</guid>
      <comments>https://aibrain.tistory.com/171#entry171comment</comments>
      <pubDate>Tue, 26 Mar 2024 19:24:11 +0900</pubDate>
    </item>
    <item>
      <title>전이 학습</title>
      <link>https://aibrain.tistory.com/170</link>
      <description>&lt;h1 style=&quot;background-color: #ffffff; color: #212121; text-align: start;&quot;&gt;전이 학습(Transfer Learning)&lt;/h1&gt;
&lt;ul style=&quot;list-style-type: disc; background-color: #ffffff; color: #212121; text-align: start;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;하나의 작업을 위해 훈련된 모델을 유사 작업 수행 모델의 시작점으로 활용하는 딥러닝 접근법&lt;/li&gt;
&lt;li&gt;신경망은 처음부터 새로 학습하는 것보다 전이 학습을 통해 업데이트하고 재학습하는 편이 더 빠르고 간편함&lt;/li&gt;
&lt;li&gt;전이 학습 방법은 여러 응용분야(검출, 영상인식, 음성인식, 검색 등)에서 많이 사용&lt;/li&gt;
&lt;/ul&gt;
&lt;div id=&quot;cell-c_7_ePiHqGgc&quot; style=&quot;color: #212121; text-align: start;&quot;&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #000000;&quot;&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;919&quot; data-origin-height=&quot;495&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cDZVRx/btsF57rcXEL/XNn4G6ODsMcobzbS6yDY30/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cDZVRx/btsF57rcXEL/XNn4G6ODsMcobzbS6yDY30/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cDZVRx/btsF57rcXEL/XNn4G6ODsMcobzbS6yDY30/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcDZVRx%2FbtsF57rcXEL%2FXNn4G6ODsMcobzbS6yDY30%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;919&quot; height=&quot;495&quot; data-origin-width=&quot;919&quot; data-origin-height=&quot;495&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;

&lt;h3 style=&quot;color: #000000;&quot; data-ke-size=&quot;size23&quot;&gt;1. 전이 학습의 고려할 점&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; color: #000000;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;크기 : 모델 크기는 배포할 위치와 방법에 따라 달라질 수 있음&lt;/li&gt;
&lt;li&gt;속도 및 정확도 : 하드웨어, 배치크기와 같은 요소를 고려해야한다&lt;/li&gt;
&lt;li&gt;정확도와 GPU 사용량등을 고려하여 적절히 사용&lt;/li&gt;
&lt;li&gt;동그라미 크기는 파라미터 개수, 덩치가 큰 애들은 CPU, GPU가 많이 쓰이지만 학습량이 많음&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1000&quot; data-origin-height=&quot;562&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/KdBsu/btsF7K3a4Bg/4qmpAuXKKHkCGQ5zoIKXO0/img.jpg&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/KdBsu/btsF7K3a4Bg/4qmpAuXKKHkCGQ5zoIKXO0/img.jpg&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/KdBsu/btsF7K3a4Bg/4qmpAuXKKHkCGQ5zoIKXO0/img.jpg&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FKdBsu%2FbtsF7K3a4Bg%2F4qmpAuXKKHkCGQ5zoIKXO0%2Fimg.jpg&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1000&quot; height=&quot;562&quot; data-origin-width=&quot;1000&quot; data-origin-height=&quot;562&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 style=&quot;color: #000000;&quot; data-ke-size=&quot;size23&quot;&gt;2. 사전 학습된 ResNet50 모델 사용하기&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc; color: #000000;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;a style=&quot;color: #000000;&quot; href=&quot;https://colab.research.google.com/corgiredirector?site=https%3A%2F%2Fpytorch.org%2Fvision%2Fstable%2Fmodels.html&quot;&gt;파이토치에서 제공하는 사전학습 모델들&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1280&quot; data-origin-height=&quot;700&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/EtSAn/btsF8a8lBVo/Bh8q4RwPkQVERKB5Mz65kk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/EtSAn/btsF8a8lBVo/Bh8q4RwPkQVERKB5Mz65kk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/EtSAn/btsF8a8lBVo/Bh8q4RwPkQVERKB5Mz65kk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FEtSAn%2FbtsF8a8lBVo%2FBh8q4RwPkQVERKB5Mz65kk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1280&quot; height=&quot;700&quot; data-origin-width=&quot;1280&quot; data-origin-height=&quot;700&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;cell-mfGnX_8Or4S_&quot; style=&quot;color: #212121; text-align: start;&quot;&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #000000;&quot;&gt;
&lt;div&gt;&lt;i&gt;&lt;/i&gt;
&lt;h1 style=&quot;color: #000000;&quot;&gt;3. 이미지넷(ImageNet)&lt;/h1&gt;
&lt;/div&gt;
&lt;ul style=&quot;list-style-type: disc; color: #000000;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;이미지 데이터 베이스&lt;/li&gt;
&lt;li&gt;1000개의 클래스로 동물과 사물 이미지를 포함&lt;span&gt;&amp;nbsp;&lt;/span&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-origin-width=&quot;1205&quot; data-origin-height=&quot;413&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/MGD1w/btsF3otZstl/ajQ1HORO8YYpUKU8LoI4A0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/MGD1w/btsF3otZstl/ajQ1HORO8YYpUKU8LoI4A0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/MGD1w/btsF3otZstl/ajQ1HORO8YYpUKU8LoI4A0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FMGD1w%2FbtsF3otZstl%2FajQ1HORO8YYpUKU8LoI4A0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;1205&quot; height=&quot;413&quot; data-origin-width=&quot;1205&quot; data-origin-height=&quot;413&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
      <author>큐트아리</author>
      <guid isPermaLink="true">https://aibrain.tistory.com/170</guid>
      <comments>https://aibrain.tistory.com/170#entry170comment</comments>
      <pubDate>Tue, 26 Mar 2024 18:26:35 +0900</pubDate>
    </item>
  </channel>
</rss>