Consider the following code
# Choose number of layers
n_input = 1
n_hidden = 10
n_output = 2
n_bias = 1
def NN_s(q,w1,w2s,b):
sigma = torch.nn.functional.softplus(q * w1 + b)
outputs = torch.matmul(w2s,sigma)
def target_func_s(q):
return q**0*np.exp(-1.5**2*q**2/2)
def K_s(w1, w2, b1):
x = torch.linspace(0, float('inf'), steps=1000)
#numerator part
f = x**2 * NN_s(x, w1, w2, b) * target_func_s(x)
num = torch.trapz(f, x)
# denominator part 1
g = x**2 * NN_s(x, w1, w2, b)**2
denom1= torch.trapz(g, x)
# denominator part 2
h = x**2 * target_func_s(x)**2
denom2 = torch.trapz(h, x)
return (num**2) / (denom1*denom2)
#### cost function
def cost(w1, w2, b):
w2s = w2[0,:]
c_s = (K_s(w1, w2s, b)-1)**2
return c_s
When I run the following code,
epochs = 10**5
beta = 0.9
learning_rate = 0.02
costs = []
epsilon = 1e-6
w1 = torch.distributions.uniform.Uniform(-1,0).sample([n_hidden,n_input]).requires_grad_(True)
w2 = torch.distributions.uniform.Uniform(0,1).sample([n_output, n_hidden]).requires_grad_(True)
b = torch.distributions.uniform.Uniform(-1,1).sample([n_hidden, 1]).requires_grad_(True)
optimizer = optim.RMSprop([w1, w2, b], lr=learning_rate)
for epoch in range(epochs):
optimizer.zero_grad()
loss = cost(w1, w2, b)
loss.backward()
optimizer.step()
costs.append(loss.item())
print("costs = {costs}");
I have problem in my function K_s(w1, w2, b1)
when using
x = torch.linspace(0, float('inf'), steps=1000)
Is there a way to handle \int_0^{\inf}
in torch? Any suggestions would help. Thank you !!!