Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将pytorch模型转换为fastNLP可用的模型 #286

Closed
jwc19890114 opened this issue Apr 21, 2020 · 1 comment
Closed

将pytorch模型转换为fastNLP可用的模型 #286

jwc19890114 opened this issue Apr 21, 2020 · 1 comment

Comments

@jwc19890114
Copy link

你好,我在尝试使用fastNLP构建自己的模型的时候遇到了问题。
pytorch教程[https://pytorch.apachecn.org/]中有一个字符级的文本生成模型

class RNN(nn.Module):
    def __init__(self,input_size,hidden_size,output_size):
        super().__init__()
        self.input_size=input_size
        self.hidden_size=hidden_size
        self.output_size=output_size
        
        self.i2h=nn.Linear(input_size+hidden_size,hidden_size)
        self.i2o=nn.Linear(input_size+hidden_size,output_size)
        self.softmax=nn.LogSoftmax()
        
    def forward(self,input ,hidden):
        combined=torch.cat((input,hidden),1)
        hidden=self.i2h(combined)
        output=self.i2o(combined)
        output=self.softmax(output)
        return output,hidden
    def init_hidden(self):
        return V(torch.zeros(1,self.hidden_size))

我想用fastNLP重写一次,但是在写的时候遇到一个问题,fastNLP要求返回数据为一个字典类型{'pred':outputs},那对于这个模型应该如何改写呢?
谢谢~

@yhcc
Copy link
Member

yhcc commented Apr 22, 2020

首先确认对应的DataSet中有input, hidden这两个field,且被设置为了input。可以通过dataset.rename_field()将dataset中的field改为这两个名字。另外就是把forward中的返回值改成字典,比如下面所示

    def forward(self,input ,hidden):
        combined=torch.cat((input,hidden),1)
        hidden=self.i2h(combined)
        output=self.i2o(combined)
        output=self.softmax(output)
        return {'pred':output, 'hidden':hidden}

@yhcc yhcc closed this as completed Apr 23, 2020
@linzehui linzehui mentioned this issue May 3, 2020
5 tasks
@pptt168 pptt168 mentioned this issue Oct 21, 2020
4 tasks
@baiziru baiziru mentioned this issue Jun 12, 2021
5 tasks
This was referenced Oct 29, 2021
Closed
Closed
@loni415 loni415 mentioned this issue Aug 31, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants