piEsposito / blitz-bayesian-deep-learning

A simple and extensible library to create Bayesian Neural Network layers on PyTorch.
GNU General Public License v3.0
931 stars 106 forks source link

Bayesian Siamese Network #20

Closed ana-tamais closed 4 years ago

ana-tamais commented 4 years ago

Hi! I'm trying to create a Siamese Network using Bayesian Layers. But I'm having the following issue:

/pytorch/torch/csrc/autograd/python_anomaly_mode.cpp:57: UserWarning: Traceback of forward call that caused the error:
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/traitlets/config/application.py", line 664, in launch_instance
    app.start()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 583, in start
    self.io_loop.start()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 149, in start
    self.asyncio_loop.run_forever()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/asyncio/base_events.py", line 442, in run_forever
    self._run_once()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/asyncio/base_events.py", line 1462, in _run_once
    handle._run()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/ioloop.py", line 690, in <lambda>
    lambda f: self._run_callback(functools.partial(callback, future))
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/ioloop.py", line 743, in _run_callback
    ret = callback()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/gen.py", line 787, in inner
    self.run()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/gen.py", line 748, in run
    yielded = self.gen.send(value)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 361, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 268, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 541, in execute_request
    user_expressions, allow_stdin,
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/tornado/gen.py", line 209, in wrapper
    yielded = next(result)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 300, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2858, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2886, in _run_cell
    return runner(coro)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/IPython/core/async_helpers.py", line 68, in _pseudo_sync_runner
    coro.send(None)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3063, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3254, in run_ast_nodes
    if (await self.run_code(code, result,  async_=asy)):
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-78-4c5bd945864c>", line 12, in <module>
    pred = sn(data1.to(torch.int64), data2.to(torch.int64))
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "<ipython-input-72-7e84bf9dfb73>", line 24, in forward
    input_1 = self.e3(input_1)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/blitz/modules/linear_bayesian_layer.py", line 72, in forward
    b = self.bias_sampler.sample()
  File "/home/anatamais-t480/anaconda3/envs/torch-ds/lib/python3.6/site-packages/blitz/modules/weight_sampler.py", line 33, in sample
    self.w = self.mu + self.sigma * self.eps_w

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-78-4c5bd945864c> in <module>
     17 
     18         # Backpropagation
---> 19         loss.backward()
     20 
     21         optimizer.step()

~/anaconda3/envs/torch-ds/lib/python3.6/site-packages/torch/tensor.py in backward(self, gradient, retain_graph, create_graph)
    164                 products. Defaults to ``False``.
    165         """
--> 166         torch.autograd.backward(self, gradient, retain_graph, create_graph)
    167 
    168     def register_hook(self, hook):

~/anaconda3/envs/torch-ds/lib/python3.6/site-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     97     Variable._execution_engine.run_backward(
     98         tensors, grad_tensors, retain_graph, create_graph,
---> 99         allow_unreachable=True)  # allow_unreachable flag
    100 
    101 

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [8]] is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

That's my code:

@variational_estimator
class SiameseNet(nn.Module):

    def __init__(self, maximo, vocab1, vocab2):
        super(SiameseNet, self).__init__()

        self.embedding1= nn.Embedding(len(vocab1.keys()),maximo)
        self.gru1= BayesianLSTM(maximo, maximo)
        self.e1 = BayesianLinear(maximo,16)
        self.e2 = BayesianLinear(16, 8)
        self.e3 = BayesianLinear(8, 8)

    def euclidean_distance(self,input_1, input_2):
        input_1, input_2 = input_1[:, -1, :], input_2[:, -1, :]
        dist = ((input_1-input_2)**2).sum(dim=1)
        return dist

    def forward(self,input_1, input_2):
        input_1 = self.embedding1(input_1)
        input_1, hidden1= self.gru1(input_1)
        input_1 = self.e1(input_1)
        input_1 = self.e2(input_1)
        input_1 = self.e3(input_1)

        input_2 = self.embedding1(input_2)
        input_2, hidden2= self.gru1(input_2)
        input_2 = self.e1(input_2)
        input_2 = self.e2(input_2)
        input_2 = self.e3(input_2)

        output = self.euclidean_distance(input_1, input_2)
        return torch.sigmoid(output)
ana-tamais commented 4 years ago

21 solves the problem