-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some error about model.sample_elbo #96
Comments
Hi. I'm gonna take a look at it and try to help you.
Em ter., 5 de out. de 2021 às 10:54, hpompom ***@***.***>
escreveu:
… When I want to use blitz, I find it may not to backpropagation and tells
me "forward() not find N". And I find it maybe error in "sample_elbo". I
hope you can give me some help
Error
------------------------------
TypeError Traceback (most recent call last)
in
----> 1 bsde_solver.train(batch_size, N, itr, log = T)
2 #x,y = bsde_solver.model(batch_size, N)
in train(self, batch_size, N, itr, log)
94 dataloader_train = torch.utils.data.DataLoader(ds_train,
batch_size=batch_size, shuffle=True)
95 for j, (datapoints, labels) in enumerate(dataloader_train):
---> 96 loss = self.model.sample_elbo(y, self.equation.g(x)
,criterion=criterion,
97 sample_nbr=10)
98
/opt/anaconda3/lib/python3.8/site-packages/blitz/utils/variational_estimator.py
in sample_elbo(self, inputs, labels, criterion, sample_nbr,
complexity_cost_weight)
63 loss = 0
64 for _ in range(sample_nbr):
---> 65 outputs = self(inputs)
66 loss += criterion(outputs, labels)
67 loss += self.nn_kl_divergence() * complexity_cost_weight
/opt/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in
_call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or
self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []
TypeError: forward() missing 1 required positional argument: 'N'
code
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class fbsde():
def __init__(self, x_0, b, sigma, f, g, T, dim_x,dim_y,dim_d):
self.x_0 = x_0.to(device)
self.b = b
self.sigma = sigma
self.f = f
self.g = g
self.T = T
self.dim_x = dim_x
self.dim_y = dim_y
self.dim_d = dim_d
@variational_estimator
class Model(nn.Module):
def __init__(self, equation, dim_h):
super(Model, self).__init__()
self.linear1 = BayesianLinear(equation.dim_x+1, dim_h)#x
self.linear2 = BayesianLinear(dim_h, dim_h)#y
self.linear3 = BayesianLinear(dim_h, equation.dim_y*equation.dim_d)#z
self.y_0 = nn.Parameter(torch.rand(equation.dim_y, device=device))#y_0
self.equation = equation
def forward(self,batch_size,N):
def phi(x):
x = F.relu(self.linear1(x))#
x = F.relu(self.linear2(x))#
return self.linear3(x).reshape(-1, self.equation.dim_y, self.equation.dim_d)
delta_t = self.equation.T / N
print(delta_t)
W = torch.randn(batch_size, self.equation.dim_d, N, device=device) * np.sqrt(delta_t)
x = self.equation.x_0+torch.zeros(W.size()[0],self.equation.dim_x,device=device)
#x
y = self.y_0+torch.zeros(W.size()[0],self.equation.dim_y,device=device)
#y
print(1)
print(N)
for i in range(N):
u = torch.cat((x, torch.ones(x.size()[0], 1,device=device)*delta_t*i), 1)
z = phi(u)#
w = W[:, :, i].reshape(-1, self.equation.dim_d, 1)
#pos = N[:, :, i].reshape(-1, self.equation.dim_d, 1)
x = x+self.equation.b(delta_t*i, x, y)*delta_t+torch.matmul( self.equation.sigma(delta_t*i, x), w).reshape(-1, self.equation.dim_x)
#x = x+self.equation.b(delta_t*i, x, y)*delta_t+torch.matmul( self.equation.sigma(delta_t*i, x), w).reshape(-1, self.equation.dim_x) + pos
y = y-self.equation.f(delta_t*i, x, y, z)*delta_t + torch.matmul(z, w).reshape(-1, self.equation.dim_y)
return x, y
print(12)
class BSDEsolver():
def __init__(self, equation, dim_h):
self.model = Model(equation,dim_h).to(device)
self.equation = equation
def train(self, batch_size, N,itr, log):
criterion = torch.nn.MSELoss().to(device)#
#loss = self.model.sample_elbo(criterion=criterion,
# sample_nbr=3)
#self.model.sample_elbo
optimizer = torch.optim.Adam(self.model.parameters())#
#weight_decay = 1e-2
loss_data, y0_data = [], []
print(2)
for i in range(itr):
print(3)
x, y = self.model(batch_size,N)
#loss = criterion(self.equation.g(x), y)
ds_train = torch.utils.data.TensorDataset(y, self.equation.g(x))
dataloader_train = torch.utils.data.DataLoader(ds_train, batch_size=500, shuffle=True)
for j, (datapoints, labels) in enumerate(dataloader_train):
loss = self.model.sample_elbo(y, self.equation.g(x) ,criterion=criterion,
sample_nbr=10)
loss_data.append(float(loss))
y0_data.append(float(self.model.y_0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
#if(i % 100 == 0):
#print("损失为{}".format(loss))
if log:
np.save('loss_data', loss_data)
np.save('y0_data', y0_data)
import torch
import numpy as np
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
dim_x, dim_y, dim_d, dim_h, N, itr, batch_size = 1, 1, 1, 11, 100, 3000, 1000
x_0, T = torch.ones(dim_x), 1
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def b(t, x, y):
return (1-x).reshape(batch_size, dim_x)
def sigma(t, x):
return torch.sqrt(torch.abs(x)).reshape(batch_size, dim_x, dim_d)
def f(t, x, y, z):
return (-y*x).reshape(batch_size, dim_y)
def g(x):
return torch.ones(batch_size, dim_y,device=device)
equation = fbsde(x_0, b, sigma, f, g, T,dim_x, dim_y, dim_d)
bsde_solver = BSDEsolver(equation, dim_h)
bsde_solver.train(batch_size, N, itr, log = T)
```
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#96>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALLYRXV72HRZASJV3ULACMTUFL7QRANCNFSM5FL2UZDA>
.
|
Thank you so much! Looking forward to you reply ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I want to use blitz, I find it may not to backpropagation and tells me "forward() not find N". And I find it maybe error in "sample_elbo". I hope you can give me some help
Error
TypeError Traceback (most recent call last)
in
----> 1 bsde_solver.train(batch_size, N, itr, log = T)
2 #x,y = bsde_solver.model(batch_size, N)
in train(self, batch_size, N, itr, log)
94 dataloader_train = torch.utils.data.DataLoader(ds_train, batch_size=batch_size, shuffle=True)
95 for j, (datapoints, labels) in enumerate(dataloader_train):
---> 96 loss = self.model.sample_elbo(y, self.equation.g(x) ,criterion=criterion,
97 sample_nbr=10)
98
/opt/anaconda3/lib/python3.8/site-packages/blitz/utils/variational_estimator.py in sample_elbo(self, inputs, labels, criterion, sample_nbr, complexity_cost_weight)
63 loss = 0
64 for _ in range(sample_nbr):
---> 65 outputs = self(inputs)
66 loss += criterion(outputs, labels)
67 loss += self.nn_kl_divergence() * complexity_cost_weight
/opt/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1049 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1050 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1051 return forward_call(*input, **kwargs)
1052 # Do not call functions when jit is used
1053 full_backward_hooks, non_full_backward_hooks = [], []
TypeError: forward() missing 1 required positional argument: 'N'
code
The text was updated successfully, but these errors were encountered: