Fastai v0.7.0 SIamese Network: Module does not support indexing

(Raj) #1

I’m trying to build a Siamese Network that uses ULMFit as the encoders. For this, I created a Module known as MySiameseNetwork:

class MySiameseClassifier(nn.Module):

def __init__(self, encoder):
    super().__init__()
    self.encoder = encoder



def forward_once(self, input):
    raw_outputs, outputs = self.encoder(input)  #what is the difference here?
    return outputs  #should this be outputs[-1]?

def forward(self, reg_inp, control_inp):  #Raj:  I think I should edit this
    u = self.forward_once(reg_inp)
    v = self.forward_once(control_inp)

    #u.reshape()
    print(np.shape(u))
    print(np.shape(v))
    dot_prod = torch.dot(u,v)
    out = F.sigmoid(dot_prod)  # do I need this or does loss function automatically apply this?  
    
    return out 
    
def reset(self):  #not sure about what exactly this does
    for c in self.children():
        if hasattr(c, 'reset'): c.reset()       

And here is how I build my model:

training_data = MySiameseDataLoader(sentence_pairs_train, pad_tok)
test_data = MySiameseDataLoader(sentence_pairs_test, pad_tok)

md = ModelData(token_files, training_data, test_data)
m = get_rnn_classifer(bptt, 20*70, c, vs, emb_sz=em_sz, n_hid=nh, n_layers=nl, pad_token=1,
          layers=[em_sz*3, 50, c], drops=[dps[4], 0.1],
          dropouti=dps[0], wdrop=dps[1], dropoute=dps[2], dropouth=dps[3])
		  
#will load weights for m 
siamese = MySiameseClassifier(m) 
learn = RNN_Learner(md, TextModel(to_gpu(siamese)), opt_fn=opt_fn, crit=nn.BCELoss())

However, when I try to freeze layers or use the fit function, I get the following error: TypeError: ‘MySiameseClassifier’ object does not support indexing

How do I get around this? Is there a better way to build my model?

0 Likes