Git Product home page Git Product logo

amnre's Introduction

AMNRE

Introduction

Source code and dataset for COLING2018 paper "Adversarial Multi-lingual Neural Relation Extraction".

Requirements

  • pytorch==0.3.1
  • scikit-learn==0.19.1
  • numpy==1.14.1
  • matplotlib==2.1.2

Data

We use the same dataset and pre-trained word embedding as the MNRE. You can download the raw data in this page. You need to download it to the Data/ path and use init.py in CNN/src/ to preprocess it.

We also provide the preprocessed .npy format data in this page. Download it to the Data/ path and unpack it, then you can run the code.

Run

Run python train.py in corresponding directory to train the model. It will output the average precision on test set to AUC.txt and the prediction result as .npy every epoch.

Run python draw.py <label file's name> <prediction result file's name> <output image's name> to get the precision-recall curve for one specific prediction result.

If you want to tune the hyper parameters, see the src/constant.py and change the parameters defined in the file.

Cite

If the codes help you, please cite the following paper:

Xiaozhi Wang, Xu Han, Yankai Lin, Zhiyuan Liu, Maosong Sun. Adversarial Multi-lingual Neural Relation Extraction (COlING 2018)

amnre's People

Contributors

bakser avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

amnre's Issues

关于测试数据和attention

CNN/train.py中调用 test(testSet,"Test",e)函数时,报Expected object of type torch.cuda.LongTensor but found type torch.LongTensor for argument #3 'index' 错误。根据我的分析,在训练数据和测试时,re_mask的维度是不一致的。
我一直有一个疑问,在使用 language-individual attention时,网络训练时,关系r是已知的,但是到测试时关系r是未知的,在调用

class MonoRE(nn.Module):
    def __init__(self):
        super(MonoRE,self).__init__()
        self.relation_emb=nn.Embedding(dimR,Encodered_dim)
        self.dropout=nn.Dropout(p=Att_dropout)
        #self.softmax=nn.Softmax()
        #self.logsoftmax=nn.LogSoftmax()
        self.M=nn.Linear(Encodered_dim,dimR)
    def forward(self,inp,r,l,re_mask):
        NumRe=r.size(0)
        NumIn=l.size(0)
        print(r.size())
        relation=self.relation_emb(r)
        attn=torch.sum(relation*inp,2)
        p=Variable(torch.cuda.FloatTensor(NumIn,NumRe).fill_(0.0))
        L=0
        R_vec=Variable(torch.cuda.FloatTensor(NumIn,NumRe,Encodered_dim).fill_(0.0))
        S=Variable(torch.cuda.FloatTensor(NumIn,NumRe,Encodered_dim).fill_(0.0))
        for i in range(0,NumIn):
            R=L+l[i].item()          
            if R>L:
                Att=F.softmax(attn[:,L:R],1)
                S[i]=self.dropout(torch.matmul(Att,inp[L:R]))
                R_vec[i]=relation[:,L,:]
            L=R
        p_n=F.log_softmax((self.M(S)+torch.sum(R_vec*S,2).view(NumIn,NumRe,1)),2).view(NumIn,NumRe,dimR)
        return p_n[re_mask].view(NumIn,NumRe)

时,这个r的含义是什么?
谢谢解释。

Bug in function Orth_con in models.py

In models.py, the code from line 193 to 196 is as follows:

share_en,share_zh=self.share_encoder(wordsEn,pos1En,pos2En,wordsZh,pos1Zh,pos2Zh)
mono_en,mono_zh=self.monoRE.encoder(wordsEn,pos1En,pos2En,wordsZh,pos1Zh,pos2Zh)
share=torch.cat((share_en,share_zh),0)
mono=torch.cat((share_en,share_zh),0)

where Tensor mono_en and mono_zh are not concatenated to Tensor mono. I think there is a bug where line 196 should be:
mono=torch.cat((mono_en,mono_zh),0)

The code from line 197 to 200:

share-=torch.mean(share,0)
mono-=torch.mean(share,0)
share=F.normalize(share,2,1)
mono=F.normalize(share,2,1)

obviously it is also a typo where the input of line 198 and 200 should be Tensor mono instead of Tensor share.

关于预测结果

因为在CNN/train.py中调用 test(testSet,"Test",e)函数时,报 错误,无法对测试数据进行验证,我修改了
CNN/src/trainer.py文件中train_RE函数,输出预测结果,发现pred都是负值
pred
tensor([[-15.4812],
[-15.5336],
[-15.7833],
[-15.1775],
[-15.8235],
[-15.9747],
[-15.4162],
[-15.6784],
[-15.5847],
[-15.0997],
[-15.4924],,
这个预测结果好像不对。同时计算loss时,为什么 loss=-torch.sum(pred.view(lEn.size(0)))要计算负值?

具体修改代码如下:
def train_RE(self,wordsEn,pos1En,pos2En,rEn,lEn,wordsZh,pos1Zh,pos2Zh,rZh,lZh,re_mask):
self.model.train()
pred=self.model(wordsEn,pos1En,pos2En,rEn,lEn,wordsZh,pos1Zh,pos2Zh,rZh,lZh,re_mask)
print(pred,re_mask)

    loss=-torch.sum(pred.view(lEn.size(0)))+Orth_Coef*self.model.Orth_con(wordsEn,pos1En,pos2En,wordsZh,pos1Zh,pos2Zh)
    if (loss!=loss).data.any():
        print("NaN Loss (training RE)")
        exit()
    self.optim.zero_grad()
    loss.backward()
    self.optim.step()
    return loss.item()

模型中的正交性约束代码问题

作者您好,在解析您的代码时,模型中对句子的两种编码表示进行正交性约束时,我发现下面代码的share_en share_zh和mono_en mono_zh引用的方法都是CNNEncoder,且传入的参数也一样,得到的两种编码结果也是一样的,这是否存在什么问题呢?两种编码为什么代码中会是一样的呢?

    def Orth_con(self, wordsEn, pos1En, pos2En, wordsZh, pos1Zh, pos2Zh):
        share_en, share_zh = self.share_encoder(
            wordsEn, pos1En, pos2En, wordsZh, pos1Zh, pos2Zh)
        mono_en, mono_zh = self.monoRE.encoder(
            wordsEn, pos1En, pos2En, wordsZh, pos1Zh, pos2Zh)
        share = torch.cat((share_en, share_zh), 0)
        mono = torch.cat((share_en, share_zh), 0)
        share -= torch.mean(share, 0)
        mono -= torch.mean(share, 0)
        share = F.normalize(share, 2, 1)
        mono = F.normalize(share, 2, 1)
        correlation_mat = torch.matmul(share.transpose(0, 1), mono) 
        cost = torch.mean(correlation_mat * correlation_mat)  
        return cost

数据处理文件 init.py 无法正常使用

作者,您好,感谢你对代码的开源!
我在数据预处理的时候,并没有找到 文件 vec_en.out
也没有看到将数据 vec_en.bin 转化成 vec_en.out

# 源文件的代码
def load_init():
    with open(dataPath+"entity2id.txt","r") as e2I:
        for x in e2I:
            x=x.split()
            entityId[x[0]]=int(x[1])
    with open(dataPath+"relation2id.txt","r") as r2I:
        for x in r2I:
            x=x.split()
            relationId[x[0]]=int(x[1])
    load_vocab(dataPath+"vec_en.out", 0) #没有文件 vec_en.out
    load_vocab(dataPath+"vec_zh.out", 1)

数据初始化问题

您好,我在调试代码时,使用CNN/src/init.py 初始化数据,发现无法运行。
1、constant.py 文件中变量 SenLen=[70,85] #Max Sentence Length for Position Embedding 是否正确
2、 CNN/src/init.py 文件中 load_vo2()无法执行,缺少数据文件
3、 CNN/src/init.py 文件中
函数
def augment(self):
L=len(self.instances)
for i in range(0,L):
if self.instances[i].r!=0:
for t in range(0,AugTimes):
self.instances.append(self.instances[i])
AugTimes没有定义

数据预处理 init.py

作者,您好,感谢你对代码的开源!
我在数据预处理的时候,并没有找到 文件 vec_en.out
也没有看到将数据 vec_en.bin 转化成 vec_en.out

# 源文件的代码
def load_init():
    with open(dataPath+"entity2id.txt","r") as e2I:
        for x in e2I:
            x=x.split()
            entityId[x[0]]=int(x[1])
    with open(dataPath+"relation2id.txt","r") as r2I:
        for x in r2I:
            x=x.split()
            relationId[x[0]]=int(x[1])
    load_vocab(dataPath+"vec_en.out", 0) #没有文件 vec_en.out
    load_vocab(dataPath+"vec_zh.out", 1)

Format of files for building the dataset

Thank you authors for their work. Would it be possible for the authors to share the format of the text files which are processed by the init.py code in CNN/RNN folders? Baidu is not available in some countries.

It would be very helpful if the authors could just provide an example of an instance from all of the files used to build the dataset.

try to reproduce the results

Hey, I try to run CNN/train.py and finish the training. How can I get the prediction results and run the then draw.py.

数据集失效

作者您好,虽然这篇工作已经发表了许久,但是至今仍给我了很多启发!在复现工作的过程中,我发现redeme中附的处理好的数据的链接已经失效,并且我自己执行处理程序,总是会卡在 np.save(dataPath+Tag+"epEn.npy",np.array(ep_en)) 代码行。我想请问一下作者,目前还能重新上传一版已处理好的数据吗?

任何可能的帮助都将万分感谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.