# 비선형 회귀 관련

**URL:** <https://discuss.pytorch.kr/t/topic/111>\
**Category:** 묻고 답하기\
**Created:** [11월 23, 2021, 3:48오전 UTC](https://discuss.pytorch.kr/t/topic/111 "2021-11-23T03:48:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolooc](https://discuss.pytorch.kr/user_avatar/discuss.pytorch.kr/coolooc/32/84_2.png) [@coolooc](https://discuss.pytorch.kr/u/coolooc)\
**Post date:** [11월 23, 2021, 3:48오전 UTC](https://discuss.pytorch.kr/t/topic/111/1 "2021-11-23T03:48:05Z")

</div>

아무리 읽어도 비선형 회귀에 대해서 전혀 감이 잡히지 않습니다.  
클래스를 만들때 비선형 함수인 렐루나 시그모이드를 쓰면된다고 하는거 같은데 간단한 비선형데이터 조차 답에 근접하지.않아요. 어디가 잘못된걸까요?

import torch  
import torch.nn as nn  
import torch.optim as optim

x1=torch.randint(0,10, size=(300,1))  
x2=torch.randint(0,10, size=(300,1))  
x=torch.cat([x1, x2], dim=1)  
y=x1\*x2  
x=float()  
y=float()

class TEST(nn.Module):  
def **init** (self):  
self.ll=nn.Linear(2 , 8)  
self.ll2=nn.Linear(8 , 4)  
self.ll3=nn.Linear(4 , 1)  
self.ru=nn.ReLU()

def forward(self, x):  
x=self.ll(x)  
[x=self.ru](http://x=self.ru)(x)  
x=self.ll2(x)  
[x=self.ru](http://x=self.ru)(x)  
x=self.ll3(x)  
return x

model=TEST()  
optimizer=optim.SGD(model.parameters(), lr=0.0001)  
lossf = nn.MSELoss()  
epochs=50000

for e in range(epochs+1):  
out = model(x)  
optimizer.zero\_grad()  
loss = lossf(out, y)  
loss.backward()  
optimizer.step()

with torch.no\_grad():  
xx1=torch.randint(0,10, size=(300,1))  
xx2=torch.randint(0,10, size=(300,1))  
xx=torch.cat([xx1, xx2], dim=1)  
yy=xx1\*xx2  
xx=float()  
yy=float()

for i, val in enumerate(xx):  
out=model(val)  
print('val ={},{},{}'.format(val, float(yy[i]), float(out)))

여기까지가 제소스입니다  
모바일로 적어서 오타가 잇을수 잇으니 양해보탁드릴께요  
epochs를 변경하고 lr을 변경하고  
relu를 시그모이드 tanh로 변경하고 리니어를 추가해도 원하는 답에 근접하지 않네요 ㅠㅠ  
어떤것니 잘못된가요?  
답에 근접하게 하려면 클래스를 어떻게 설계해야되나요?

---

<div class="post-metadata">

**Author:** ![cisowas](https://discuss.pytorch.kr/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@cisowas](https://discuss.pytorch.kr/u/cisowas)\
**Post date:** [11월 27, 2021, 9:16오전 UTC](https://discuss.pytorch.kr/t/topic/111/2 "2021-11-27T09:16:40Z")

</div>

이거 안될텐데요. nn.Linear가 어떻게 계산되는지 아시면 곱이 안나올 겁니다.  
[x0, x1, x2] 이런형식이면  
x0\*w0 + x1\*w1 + x2\*w2 등 가중치 w가 각각 곱해져서 계산되는 것인데 x0\*x1등이 될 수가 없죠.

torch에 설명이 이렇게 나와있네요  
Applies a linear transformation to the incoming data: y = xA^T + b

---

<div class="post-metadata">

**Author:** ![cisowas](https://discuss.pytorch.kr/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@cisowas](https://discuss.pytorch.kr/u/cisowas)\
**Post date:** [11월 27, 2021, 9:25오전 UTC](https://discuss.pytorch.kr/t/topic/111/3 "2021-11-27T09:25:50Z")

</div>

`torch.nn.` `Bilinear` (_in1\_features_ , _in2\_features_ , _out\_features_ , _bias=True_ , _device=None_ , _dtype=None_ )  
이거 사용해야 될듯

---

<div class="post-metadata">

**Author:** ![cisowas](https://discuss.pytorch.kr/letter_avatar_proxy/v4/letter/c/ecd19e/32.png) [@cisowas](https://discuss.pytorch.kr/u/cisowas)\
**Post date:** [11월 27, 2021, 9:45오전 UTC](https://discuss.pytorch.kr/t/topic/111/4 "2021-11-27T09:45:40Z")

</div>

> [@coolooc](#):
>
> import torch  
> import torch.nn as nn  
> import torch.optim as optim
> 
> x1=torch.randint(0,10, size=(300,1))  
> x2=torch.randint(0,10, size=(300,1))  
> x=torch.cat([x1, x2], dim=1)  
> y=x1\*x2  
> x=float()  
> y=float()
> 
> class TEST(nn.Module):  
> def **init** (self):  
> self.ll=nn.Linear(2 , 8)  
> self.ll2=nn.Linear(8 , 4)  
> self.ll3=nn.Linear(4 , 1)  
> self.ru=nn.ReLU()
> 
> def forward(self, x):  
> x=self.ll(x)  
> [x=self.ru](http://x=self.ru)(x)  
> x=self.ll2(x)  
> [x=self.ru](http://x=self.ru)(x)  
> x=self.ll3(x)  
> return x
> 
> model=TEST()  
> optimizer=optim.SGD(model.parameters(), lr=0.0001)  
> lossf = nn.MSELoss()  
> epochs=50000
> 
> for e in range(epochs+1):  
> out = model(x)  
> optimizer.zero\_grad()  
> loss = lossf(out, y)  
> loss.backward()  
> optimizer.step()
> 
> with torch.no\_grad():  
> xx1=torch.randint(0,10, size=(300,1))  
> xx2=torch.randint(0,10, size=(300,1))  
> xx=torch.cat([xx1, xx2], dim=1)  
> yy=xx1\*xx2  
> xx=float()  
> yy=float()

구현 까지 해봤어요.

```auto
import torch
import torch.nn as nn
import torch.optim as optim

x1=torch.randint(0,10, size=(300,1)).type(torch.float)
x2=torch.randint(0,10, size=(300,1)).type(torch.float)
y1=x1*x2

class TEST(nn.Module):
  def __init__ (self):
    super(). __init__ ()
    self.bl=nn.Bilinear(1 , 1, 1)
  def forward(self, x1, x2):
    y=self.bl(x1, x2)
    return y

model=TEST()
optimizer=optim.Adam(model.parameters())
lossf = nn.MSELoss()
epochs=50000

model.train()
for e in range(epochs+1):
  out = model(x1, x2)
  print(out[2], y1[2])
  
  loss = lossf(out, y1)
  optimizer.zero_grad()
  loss.backward()
  optimizer.step()

```

근데 내가 올린건 언제 해결해 주는거지 나도 질문 있는데 ㅜㅜ

---

<div class="post-metadata">

**Author:** ![coolooc](https://discuss.pytorch.kr/user_avatar/discuss.pytorch.kr/coolooc/32/84_2.png) [@coolooc](https://discuss.pytorch.kr/u/coolooc)\
**Post date:** [11월 27, 2021, 1:10오후 UTC](https://discuss.pytorch.kr/t/topic/111/5 "2021-11-27T13:10:59Z")

</div>

우와 감사합니다. linear내부를 봐봐야 겟네요 ㅋㅋㅋ
