# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="2QIZ9bWL1r9Q" executionInfo={"status": "ok", "timestamp": 1636866811369, "user_tz": -540, "elapsed": 396, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

# + id="LDBLMrMk2vQC" executionInfo={"status": "ok", "timestamp": 1636866723590, "user_tz": -540, "elapsed": 8, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
def sigmoid(x):
  return 1 / (1 + np.exp(-x))


# + colab={"base_uri": "https://localhost:8080/", "height": 284} id="yhjrlBv_2227" executionInfo={"status": "ok", "timestamp": 1636866787670, "user_tz": -540, "elapsed": 451, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="b060a1a1-aab2-4480-da03-bef4d21daf57"
x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)
plt.plot(x, y, 'b')

# + [markdown] id="dkqwgGIE4K6e"
# # 함수의 모양(W)과 위치(b)를 결정하는 계수

# + [markdown] id="EVbue5y34QLc"
# # W값에 따른 그래프 모양의 변화

# + colab={"base_uri": "https://localhost:8080/", "height": 284} id="-lUHSaVR3Kk9" executionInfo={"status": "ok", "timestamp": 1636866962505, "user_tz": -540, "elapsed": 434, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="047f0863-fc72-4380-8270-f67e9cef6743"
y2 = sigmoid(2 * x)
plt.plot(x, y2, 'r')

# + colab={"base_uri": "https://localhost:8080/", "height": 284} id="iBMN5ksx38_Y" executionInfo={"status": "ok", "timestamp": 1636867103086, "user_tz": -540, "elapsed": 457, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="b67e5ff9-7693-4b86-8082-4f01f224fb91"
y3 = sigmoid(0.5 * x)
plt.plot(x, y3, 'r')

# + [markdown] id="4egkXILD4jb0"
# # b값에 따른 그래프 위치의 변화

# + id="T51r_4oZ4meO" executionInfo={"status": "ok", "timestamp": 1636867189445, "user_tz": -540, "elapsed": 438, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
x = np.arange(-5.0, 5.0, 0.1)
y1 = sigmoid(x + 0.5)
y2 = sigmoid(x + 1)
y3 = sigmoid(x + 1.5)

# + colab={"base_uri": "https://localhost:8080/", "height": 281} id="pDGRMiXE4ywV" executionInfo={"status": "ok", "timestamp": 1636867276572, "user_tz": -540, "elapsed": 604, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="57b07400-9582-46ee-a63b-8c10fe7358a4"
plt.plot(x, y1, 'r', linestyle = '--')  # x + 0.5
plt.plot(x, y2, 'g')  # x + 1
plt.plot(x, y3, 'b', linestyle = '--')  # x + 1.5
plt.plot([0, 0], [1.0, 0.0], ':')  # 가운데 점선 추가
plt.title('Sigmoid Function')
plt.show()

# + [markdown] id="6MlQYExn7dK1"
# # 로지스틱 회귀 구현해보기

# + id="LoNKmy7T7PnO" executionInfo={"status": "ok", "timestamp": 1636868669981, "user_tz": -540, "elapsed": 424, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from tqdm import tqdm

# + colab={"base_uri": "https://localhost:8080/"} id="ylM3wZ927Zwz" executionInfo={"status": "ok", "timestamp": 1636867884640, "user_tz": -540, "elapsed": 529, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="6bf75c25-83f2-43ea-fa85-e6ae6a3fa1a3"
torch.manual_seed(1)

# + colab={"base_uri": "https://localhost:8080/"} id="ufvtLqPs7pGI" executionInfo={"status": "ok", "timestamp": 1636868033046, "user_tz": -540, "elapsed": 421, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="57495f71-0518-4193-d45e-76c7b3204bb4"
x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]]
y_data = [[0], [0], [0], [1], [1], [1]]
x_train = torch.FloatTensor(x_data)
y_train = torch.FloatTensor(y_data)
print(x_train.shape)
print(y_train.shape)

# + id="l2_JgOV68BjT" executionInfo={"status": "ok", "timestamp": 1636868117488, "user_tz": -540, "elapsed": 409, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
W = torch.zeros((2, 1), requires_grad = True)  # 크기는 2 X 1, Wx를 계산한 결과물이 1차원이 되도록
b = torch.zeros(1, requires_grad = True)

# + id="PXHBqQnc8Vy1" executionInfo={"status": "ok", "timestamp": 1636868157902, "user_tz": -540, "elapsed": 408, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
h = 1 / (1 + torch.exp(-(x_train.matmul(W) + b)))

# + colab={"base_uri": "https://localhost:8080/"} id="2CV3KAB68fjj" executionInfo={"status": "ok", "timestamp": 1636868198000, "user_tz": -540, "elapsed": 410, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="ae79a988-d238-4671-e928-6bfededba9d8"
print(h)

# + colab={"base_uri": "https://localhost:8080/"} id="99O6djoW8uIp" executionInfo={"status": "ok", "timestamp": 1636868321332, "user_tz": -540, "elapsed": 382, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="fefd7661-0ad6-4301-8d45-74b527de3d5f"
loss = -(y_train * torch.log(h) + (1 - y_train) * torch.log(1 - h))
print(loss)
cost = loss.mean()
print(cost)

# + id="1GKCM_gf8-gK" executionInfo={"status": "ok", "timestamp": 1636868351937, "user_tz": -540, "elapsed": 516, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}}
cost = F.binary_cross_entropy(h, y_train)

# + [markdown] id="vcAEJjXW9RZl"
# # 학습

# + colab={"base_uri": "https://localhost:8080/"} id="vJYwxCVR9TwH" executionInfo={"status": "ok", "timestamp": 1636868851951, "user_tz": -540, "elapsed": 411, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="5037aca6-e0a4-4834-8ee5-7436cf6b7b4c"
W = torch.zeros((2, 1), requires_grad = True)
b = torch.zeros(1, requires_grad = True)
optimizer = optim.SGD([W, b], lr = 1)
num_epochs = 1000
for epoch in tqdm(range(num_epochs)):
  # cost
  h = torch.sigmoid(x_train.matmul(W) + b)
  cost = F.binary_cross_entropy(h, y_train)

  # backpropagation
  optimizer.zero_grad()
  cost.backward()
  optimizer.step()

  # 로그 출력
  if epoch % 100 == 0:
    print('Epoch {:4d} / {} cost: {:.6f}'.format(epoch, num_epochs, cost.item()))

# + colab={"base_uri": "https://localhost:8080/"} id="YLE4GH4p--Ni" executionInfo={"status": "ok", "timestamp": 1636868931575, "user_tz": -540, "elapsed": 800, "user": {"displayName": "\uae40\uc138\uc900\uc804\uc790\uacf5\ud559\uc804\uacf5", "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64", "userId": "13702907769980422999"}} outputId="70906e03-e648-4007-e739-d80923f00503"
print(torch.sigmoid(x_train.matmul(W) + b))
hypo = torch.sigmoid(x_train.matmul(W) + b)
prediction = hypo >= torch.FloatTensor([0.5])
print(prediction)

StarcoderdataJupyterScriptsDedupFiltered