비선형 회귀 관련

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

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(x)
x=self.ll2(x)
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로 변경하고 리니어를 추가해도 원하는 답에 근접하지 않네요 ㅠㅠ
어떤것니 잘못된가요?
답에 근접하게 하려면 클래스를 어떻게 설계해야되나요?

이거 안될텐데요. 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

torch.nn. Bilinear (in1_features , in2_features , out_features , bias=True , device=None , dtype=None )
이거 사용해야 될듯

구현 까지 해봤어요.

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()

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

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