Quiz-11 (2026.04.03) // 범위: ~05wk

Author

최규빈

Published

April 11, 2026

1. 회귀 (설명변수 1개)

(1) 아래 데이터에 대하여 회귀모델 \(y_i \sim N(w_0^\ast + w_1^\ast x_i, \sigma^2)\)를 적합하려 한다. 코드의 ???를 채우시오.

\(x_i\) \(y_i\)
1 2.8
2 5.1
3 7.3
4 8.9
5 11.2
import torch
x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
y = torch.tensor([2.8, 5.1, 7.3, 8.9, 11.2])
X = ???  # (a)
Y = y.reshape(-1, 1)

What = ???  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = X @ What
    loss = torch.mean((Yhat - Y)**2)
    loss.backward()
    What.data = What.data - 0.05 * What.grad
    What.grad = None

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \\ y_5 \end{bmatrix} \approx \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ 1 & x_4 \\ 1 & x_5 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \end{bmatrix}\]

or

\[\begin{bmatrix} 2.8 \\ 5.1 \\ 7.3 \\ 8.9 \\ 11.2 \end{bmatrix} \approx \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \\ 1 & 5 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \end{bmatrix}\]

import torch
x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
y = torch.tensor([2.8, 5.1, 7.3, 8.9, 11.2])
X = torch.stack([torch.ones(5),x],axis=1)
Y = y.reshape(-1, 1)

What = torch.tensor([[0.0],[0.0]],requires_grad=True)

for epoch in range(300):
    Yhat = X @ What
    loss = torch.mean((Yhat - Y)**2)
    loss.backward()
    What.data = What.data - 0.05 * What.grad
    What.grad = None

(2) 위 코드를 실행하여 모델을 적합한 뒤, 아래 표의 빈칸을 채우시오.

\(x_i\) \(y_i\) 적합값 (\(\hat{y}_i\))
1 2.8 ???
2 5.1 ???
3 7.3 ???
4 8.9 ???
5 11.2 ???

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \end{bmatrix} = \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ 1 & x_4 \\ 1 & x_5 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \end{bmatrix}\]

or

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \\ 1 & 4 \\ 1 & 5 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \end{bmatrix}\]

X@What
tensor([[ 2.9388],
        [ 4.9992],
        [ 7.0597],
        [ 9.1202],
        [11.1807]], grad_fn=<MmBackward0>)

(3) 위 코드를 실행하여 모델을 적합한 뒤, 새로운 데이터 \(x = 7\)에 대한 예측값을 구하는 코드를 작성하시오.

(힌트) 아래의 수식을 관찰해볼 것.

\[\begin{aligned} \hat{y}_{\text{new}} &= \hat{w}_0 + \hat{w}_1 \cdot x_{\text{new}} \\ &= \hat{w}_0 + \hat{w}_1 \cdot 7 \\ &= \begin{bmatrix} 1 & 7 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \end{bmatrix} \end{aligned}\]

What
tensor([[0.8783],
        [2.0605]], requires_grad=True)
0.8783 + 2.0605 * 7 # 이렇게 풀어도 되고
15.3018
Xnew = torch.tensor([[1.0,7.0]])
Xnew @ What # 이렇게 풀어도된다.
tensor([[15.3016]], grad_fn=<MmBackward0>)

2. 회귀 (설명변수 2개)

(1) 아래 데이터에 대하여 회귀모델 \(y_i \sim N(w_0^\ast + w_1^\ast u_i + w_2^\ast v_i, \sigma^2)\)를 적합하려 한다. 코드의 ???를 채우시오.

\(u_i\) \(v_i\) \(y_i\)
1 2 8.1
2 1 7.0
3 3 15.2
4 2 13.8
5 4 22.1
import torch
u = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
v = torch.tensor([2.0, 1.0, 3.0, 2.0, 4.0])
y = torch.tensor([8.1, 7.0, 15.2, 13.8, 22.1])
X = ???  # (a)
Y = y.reshape(-1, 1)

What = ???  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = X @ What
    loss = torch.mean((Yhat - Y)**2)
    loss.backward()
    What.data = What.data - 0.05 * What.grad
    What.grad = None

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \\ y_5 \end{bmatrix} \approx \begin{bmatrix} 1 & u_1 & v_1 \\ 1 & u_2 & v_2 \\ 1 & u_3 & v_3 \\ 1 & u_4 & v_4 \\ 1 & u_5 & v_5 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \\ w_2^\ast \end{bmatrix}\]

or

\[\begin{bmatrix} 8.1 \\ 7.0 \\ 15.2 \\ 13.8 \\ 22.1 \end{bmatrix} \approx \begin{bmatrix} 1 & 1 & 2 \\ 1 & 2 & 1 \\ 1 & 3 & 3 \\ 1 & 4 & 2 \\ 1 & 5 & 4 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \\ w_2^\ast \end{bmatrix}\]

import torch
u = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])
v = torch.tensor([2.0, 1.0, 3.0, 2.0, 4.0])
y = torch.tensor([8.1, 7.0, 15.2, 13.8, 22.1])
X = torch.stack([torch.ones(5),u,v],axis=1)
Y = y.reshape(-1, 1)

What = torch.tensor([[0.0],[0.0],[0.0]],requires_grad=True)  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = X @ What
    loss = torch.mean((Yhat - Y)**2)
    loss.backward()
    What.data = What.data - 0.05 * What.grad
    What.grad = None

(2) 위 코드를 실행하여 모델을 적합한 뒤, 아래 표의 빈칸을 채우시오.

\(u_i\) \(v_i\) \(y_i\) 적합값 (\(\hat{y}_i\))
1 2 8.1 ???
2 1 7.0 ???
3 3 15.2 ???
4 2 13.8 ???
5 4 22.1 ???

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \end{bmatrix} = \begin{bmatrix} 1 & u_1 & v_1 \\ 1 & u_2 & v_2 \\ 1 & u_3 & v_3 \\ 1 & u_4 & v_4 \\ 1 & u_5 & v_5 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \\ \hat{w}_2 \end{bmatrix}\]

or

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \end{bmatrix} = \begin{bmatrix} 1 & 1 & 2 \\ 1 & 2 & 1 \\ 1 & 3 & 3 \\ 1 & 4 & 2 \\ 1 & 5 & 4 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \\ \hat{w}_2 \end{bmatrix}\]

X@What
tensor([[ 8.1798],
        [ 6.9304],
        [15.1327],
        [13.8832],
        [22.0856]], grad_fn=<MmBackward0>)

(3) 위 코드를 실행하여 모델을 적합한 뒤, 새로운 데이터 \(u = 3\), \(v = 4\)에 대한 예측값을 구하는 코드를 작성하시오.

(힌트) 아래의 수식을 관찰해볼 것.

\[\begin{aligned} \hat{y}_{\text{new}} &= \hat{w}_0 + \hat{w}_1 \cdot u_{\text{new}} + \hat{w}_2 \cdot v_{\text{new}} \\ &= \hat{w}_0 + \hat{w}_1 \cdot 3 + \hat{w}_2 \cdot 4 \\ &= \begin{bmatrix} 1 & 3 & 4 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \\ \hat{w}_2 \end{bmatrix} \end{aligned}\]

What
tensor([[-0.0225],
        [ 1.9011],
        [ 3.1506]], requires_grad=True)
-0.0225 + 3*1.9011 + 4*3.1506
18.2832
Xnew = torch.tensor([[1,3,4]]).float()
Xnew@What
tensor([[18.2833]], grad_fn=<MmBackward0>)

3. 로지스틱 (설명변수 1개)

(1) 아래 데이터에 대하여 로지스틱 모델 \(y_i \sim \text{Bernoulli}(\text{sig}(w_0^\ast + w_1^\ast x_i))\)를 적합하려 한다. 코드의 ???를 채우시오.

\(x_i\) \(y_i\)
-2 0
-1 0
0 0
1 0
2 1
3 1
import torch
def sig(x):
    return torch.exp(x) / (torch.exp(x) + 1)

x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0])
y = torch.tensor([0.0, 0.0, 0.0, 0.0, 1.0, 1.0])
X = ???  # (a)
Y = y.reshape(-1, 1)

What = ???  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = sig(X @ What)
    loss = -torch.mean(Y*torch.log(Yhat) + (1-Y)*torch.log(1-Yhat))
    loss.backward()
    What.data = What.data - 0.1 * What.grad
    What.grad = None

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \\ y_5 \\ y_6 \end{bmatrix} \approx \text{sig}\left(\begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ 1 & x_4 \\ 1 & x_5 \\ 1 & x_6 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \end{bmatrix}\right)\]

or

\[\begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \\ 1 \\ 1 \end{bmatrix} \approx \text{sig}\left(\begin{bmatrix} 1 & -2 \\ 1 & -1 \\ 1 & 0 \\ 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \end{bmatrix}\right)\]

import torch
def sig(x):
    return torch.exp(x) / (torch.exp(x) + 1)

x = torch.tensor([-2.0, -1.0, 0.0, 1.0, 2.0, 3.0])
y = torch.tensor([0.0, 0.0, 0.0, 0.0, 1.0, 1.0])
X = torch.stack([torch.ones(6),x],axis=1)  # (a)
Y = y.reshape(-1, 1)

What = torch.tensor([[0.0],[0.0]],requires_grad=True)  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = sig(X @ What)
    loss = -torch.mean(Y*torch.log(Yhat) + (1-Y)*torch.log(1-Yhat))
    loss.backward()
    What.data = What.data - 0.1 * What.grad
    What.grad = None

(2) 위 코드를 실행하여 모델을 적합한 뒤, 아래 표의 빈칸을 채우시오.

\(x_i\) \(y_i\) 적합값 (\(\hat{y}_i\))
-2 0 ???
-1 0 ???
0 0 ???
1 0 ???
2 1 ???
3 1 ???

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \\ \hat{y}_6 \end{bmatrix} = \text{sig}\left(\begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ 1 & x_4 \\ 1 & x_5 \\ 1 & x_6 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \end{bmatrix}\right)\]

or

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \\ \hat{y}_6 \end{bmatrix} = \text{sig}\left(\begin{bmatrix} 1 & -2 \\ 1 & -1 \\ 1 & 0 \\ 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \end{bmatrix}\right)\]

sig(X@What)
tensor([[0.0020],
        [0.0130],
        [0.0819],
        [0.3757],
        [0.8024],
        [0.9648]], grad_fn=<DivBackward0>)

(3) 위 코드를 실행하여 모델을 적합한 뒤, 새로운 데이터 \(x = 2.2\)에 대한 예측값을 구하는 코드를 작성하시오.

(힌트) 아래의 수식을 관찰해볼 것.

\[\begin{aligned} \hat{y}_{\text{new}} &= \text{sig}(\hat{w}_0 + \hat{w}_1 \cdot x_{\text{new}}) \\ &= \text{sig}(\hat{w}_0 + \hat{w}_1 \cdot 2.2) \\ &= \text{sig}\left(\begin{bmatrix} 1 & 2.2 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \end{bmatrix}\right) \end{aligned}\]

What
tensor([[-2.4175],
        [ 1.9094]], requires_grad=True)
sig(torch.tensor(-2.4175 + 2.2*1.9094))
tensor(0.8561)
Xnew = torch.tensor([[1,2.2]])
sig(Xnew @ What)
tensor([[0.8561]], grad_fn=<DivBackward0>)

4. 로지스틱 (설명변수 2개)

(1) 아래 데이터에 대하여 로지스틱 모델 \(y_i \sim \text{Bernoulli}(\text{sig}(w_0^\ast + w_1^\ast u_i + w_2^\ast v_i))\)를 적합하려 한다. 코드의 ???를 채우시오.

\(u_i\) \(v_i\) \(y_i\)
1 1 1
2 1 1
1 2 1
-1 -1 0
-2 -1 0
-1 -2 0
import torch
def sig(x):
    return torch.exp(x) / (torch.exp(x) + 1)

u = torch.tensor([1.0, 2.0, 1.0, -1.0, -2.0, -1.0])
v = torch.tensor([1.0, 1.0, 2.0, -1.0, -1.0, -2.0])
y = torch.tensor([1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
X = ???  # (a)
Y = y.reshape(-1, 1)

What = ???  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = sig(X @ What)
    loss = -torch.mean(Y*torch.log(Yhat) + (1-Y)*torch.log(1-Yhat))
    loss.backward()
    What.data = What.data - 0.1 * What.grad
    What.grad = None

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} y_1 \\ y_2 \\ y_3 \\ y_4 \\ y_5 \\ y_6 \end{bmatrix} \approx \text{sig}\left(\begin{bmatrix} 1 & u_1 & v_1 \\ 1 & u_2 & v_2 \\ 1 & u_3 & v_3 \\ 1 & u_4 & v_4 \\ 1 & u_5 & v_5 \\ 1 & u_6 & v_6 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \\ w_2^\ast \end{bmatrix}\right)\]

or

\[\begin{bmatrix} 1 \\ 1 \\ 1 \\ 0 \\ 0 \\ 0 \end{bmatrix} \approx \text{sig}\left(\begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \\ 1 & -1 & -1 \\ 1 & -2 & -1 \\ 1 & -1 & -2 \end{bmatrix} \begin{bmatrix} w_0^\ast \\ w_1^\ast \\ w_2^\ast \end{bmatrix}\right)\]

import torch
def sig(x):
    return torch.exp(x) / (torch.exp(x) + 1)

u = torch.tensor([1.0, 2.0, 1.0, -1.0, -2.0, -1.0])
v = torch.tensor([1.0, 1.0, 2.0, -1.0, -1.0, -2.0])
y = torch.tensor([1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
X = torch.stack([torch.ones(6),u,v],axis=1)  # (a)
Y = y.reshape(-1, 1)

What = torch.tensor([[0.0],[0.0],[0.0]],requires_grad=True)  # (b) 모든 파라메터의 초기값은 0으로 설정

for epoch in range(300):
    Yhat = sig(X @ What)
    loss = -torch.mean(Y*torch.log(Yhat) + (1-Y)*torch.log(1-Yhat))
    loss.backward()
    What.data = What.data - 0.1 * What.grad
    What.grad = None

(2) 위 코드를 실행하여 모델을 적합한 뒤, 아래 표의 빈칸을 채우시오.

\(u_i\) \(v_i\) \(y_i\) 적합값 (\(\hat{y}_i\))
1 1 1 ???
2 1 1 ???
1 2 1 ???
-1 -1 0 ???
-2 -1 0 ???
-1 -2 0 ???

(힌트) 아래의 행렬 표현을 관찰해볼 것.

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \\ \hat{y}_6 \end{bmatrix} = \text{sig}\left(\begin{bmatrix} 1 & u_1 & v_1 \\ 1 & u_2 & v_2 \\ 1 & u_3 & v_3 \\ 1 & u_4 & v_4 \\ 1 & u_5 & v_5 \\ 1 & u_6 & v_6 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \\ \hat{w}_2 \end{bmatrix}\right)\]

or

\[\begin{bmatrix} \hat{y}_1 \\ \hat{y}_2 \\ \hat{y}_3 \\ \hat{y}_4 \\ \hat{y}_5 \\ \hat{y}_6 \end{bmatrix} = \text{sig}\left(\begin{bmatrix} 1 & 1 & 1 \\ 1 & 2 & 1 \\ 1 & 1 & 2 \\ 1 & -1 & -1 \\ 1 & -2 & -1 \\ 1 & -1 & -2 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \\ \hat{w}_2 \end{bmatrix}\right)\]

sig(X@What)
tensor([[0.9723],
        [0.9952],
        [0.9952],
        [0.0277],
        [0.0048],
        [0.0048]], grad_fn=<DivBackward0>)

(3) 위 코드를 실행하여 모델을 적합한 뒤, 새로운 데이터 \(u = 1\), \(v = -1\)에 대한 예측값을 구하는 코드를 작성하시오.

(힌트) 아래의 수식을 관찰해볼 것.

\[\begin{aligned} \hat{y}_{\text{new}} &= \text{sig}(\hat{w}_0 + \hat{w}_1 \cdot u_{\text{new}} + \hat{w}_2 \cdot v_{\text{new}}) \\ &= \text{sig}(\hat{w}_0 + \hat{w}_1 \cdot 1 + \hat{w}_2 \cdot (-1)) \\ &= \text{sig}\left(\begin{bmatrix} 1 & 1 & -1 \end{bmatrix} \begin{bmatrix} \hat{w}_0 \\ \hat{w}_1 \\ \hat{w}_2 \end{bmatrix}\right) \end{aligned}\]

What
tensor([[-4.8184e-08],
        [ 1.7790e+00],
        [ 1.7790e+00]], requires_grad=True)
sig(torch.tensor(-4.8184e-08 + 1.7790e+00*1 + 1.7790e+00*(-1)))
tensor(0.5000)
Xnew = torch.tensor([[1,1,-1]]).float()
sig(Xnew@What)
tensor([[0.5000]], grad_fn=<DivBackward0>)