Hello everyone,
I am trying to replace GRU with Bert. But I am getting error “TypeError: from_pretrained() takes 2 positional arguments but 3 were given”
The code where GRU is already implemented is as follow:
class SeqEncoder(nn.Module):
“”"
the sequence encoder.
“”"
def __init__(self, din, dhid, slots, dropout=None):
	super(SeqEncoder, self).__init__()
	self.slots = slots        
	self.dropout = dropout or {}
	self.rnn_encoder = nn.GRU(din, dhid, bidirectional=True, batch_first=True)
	self.slot_att = nn.ModuleDict({s: SlotAttention(dhid*2, self.dropout.get('att_in', 0.2)) for s in self.slots})

def forward(self, x, x_len, slot):
	rnn_encoding = run_rnn(self.rnn_encoder, x, x_len)
	attended = self.slot_att[slot](rnn_encoding, x_len)
	attended = F.dropout(attended, self.dropout.get('att_out', 0.2), self.training)
	return rnn_encoding, attended

After changing GRU with Bert the code is:
class SeqEncoder(nn.Module):
“”"
the sequence encoder.
“”"
def __init__(self, din, dhid, slots, dropout=None):
    super(SeqEncoder, self).__init__()
    self.slots = slots
    self.dropout = dropout or {}
    self.rnn_encoder_gru = BertModel.from_pretrained('bert-base-uncased', dhid, din
                                # Whether the model returns all hidden-states.
                              )
    #self.rnn_encoder_gru = nn.GRU(din, dhid, bidirectional=True, batch_first=True)
    self.slot_att_gru = nn.ModuleDict({s: SlotAttention(dhid * 2, self.dropout.get('att_in', 0.2)) for s in self.slots})
    self.rnn_encoder_lstm = nn.LSTM(din, dhid, bidirectional=True, batch_first=True)
    self.slot_att_lstm = nn.ModuleDict({s: SlotAttention(dhid * 2, self.dropout.get('att_in', 0.2)) for s in self.slots})

def forward(self, x, x_len, slot):
    rnn_encoding = run_rnn(self.rnn_encoder_gru, self.rnn_encoder_lstm, x, x_len)
    attended = self.slot_att_gru[slot](rnn_encoding, x_len)
    attended = self.slot_att_lstm[slot](rnn_encoding, x_len)
    attended = F.dropout(attended, self.dropout.get('att_out', 0.2), self.training)
    return rnn_encoding, attended

But I am getting the following error:
Traceback (most recent call last):
File “/home/asifdadra/Experiments/DST/ten/train.py”, line 81, in 
run(args)
File “/home/asifdadra/Experiments/DST/ten/train.py”, line 40, in run
model = load_model(args[‘model’], args, ontology, vocab)
File “/home/asifdadra/Experiments/DST/ten/utils.py”, line 41, in load_model
model = Model(*args, **kwargs)
File “/home/asifdadra/Experiments/DST/ten/models/dst.py”, line 178, in init
self.utt_encoder = SeqEncoder(args[‘demb’], args[‘dhid’], self.slots, dropout=args[‘dropout’])
File “/home/asifdadra/Experiments/DST/ten/models/dst.py”, line 123, in init
self.rnn_encoder_gru = BertModel.from_pretrained(‘bert-base-uncased’, dhid, din
File “/home/asifdadra/Downloads/enter/envs/ttp37/lib/python3.7/site-packages/transformers/modeling_utils.py”, line 988, in from_pretrained
**kwargs,
TypeError: from_pretrained() takes 2 positional arguments but 4 were given
Process finished with exit code 1