I’m stuck on a problem for a while
Little background of question:
I pass few images into pretrained CNN to get features and then pass those extracted features into a LSTM to get a score for every image. But i’m stuck on training the LSTM using those features , i always get the error during backpropagating .
Error says that Trying to backward through the graph a second time, but the buffers have already been freed. Specify retain_graph=True when calling backward the first time.
I tried many solutions given on this forum and on stackoverflow as well of passing retain_graph=True , retain_variables=True etc. but none of them worked out.
Then i tried something else , i JUST REPLACED my CNN features output with a random initialised features like this
inps = [Variable(torch.randn(10, 1000) for _ in range(10)]
, this inps
is exactly the same as my deep features in dimension, and the below code works fine on this but NOT with frames_deep_features
Can somebody will please help me out in this question, i really need to get it done in 2/3 days
hidden_size = 32
input_size = 1000
M = 100
seq_len = 10
inps = [Variable(torch.randn(10, input_size)) for _ in range(10)]
num_layers = 2
sigma = 0.3
def my_loss(scores):
return (scores/M) - sigma
class tryLstm(nn.Module):
def __init__(self):
super(tryLstm, self).__init__()
self.rnn = nn.LSTM(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers)
self.linear = nn.Linear(hidden_size, 1)
self.sigmoid = nn.Sigmoid()
self.hidden = self.init_hidden()
def init_hidden(self):
return (Variable(torch.zeros(num_layers, 1, hidden_size)), Variable(torch.zeros(num_layers, 1, hidden_size)))
def forward(self, inp):
out, self.hidden = self.rnn(inp.view(seq_len, 1, -1), self.hidden)
score = self.linear(out)
score = self.sigmoid(score)
return score
model = tryLstm()
def print_grad(g):
print g
optimizer = optim.SGD(model.parameters(), lr=0.1)
for epoch in range(100):
for i in frames_deep_features:
model.zero_grad()
model.hidden = model.init_hidden()
scores = model(i)
scores = scores.sum()
# print scores
loss = my_loss(scores)
# print loss
if epoch % 10 == 0:
print "Loss for epoch {} is = {} ".format(epoch, loss.data[0])
# loss.register_hook(print_grad)
loss.backward(retain_graph=True)
# print loss.grad
optimizer.step()