模型创建相关代码

def create_model(bert_config, is_training, input_ids, input_mask, segment_ids,labels, num_labels, use_one_hot_embeddings, extras):"""Creates a classification model."""model = modeling.BertModel(config=bert_config,is_training=is_training,input_ids=input_ids,input_mask=input_mask,token_type_ids=segment_ids,use_one_hot_embeddings=use_one_hot_embeddings,extras=extras)output_layer = model.get_sequence_output()from_seq_length = output_layer.shape[1].valuehidden_size = output_layer.shape[2].value# B 10 F 768output_layer = tf.stack([output_layer] * FLAGS.max_num_relations, axis=1)# B 10 F 1e1_mas = tf.reshape(extras.e1_mas, [-1, FLAGS.max_num_relations, from_seq_length, 1])# B 10 F 768e1 = tf.multiply(output_layer, tf.to_float(e1_mas))# B 10 768e1 = tf.reduce_sum(e1, axis=-2) / tf.maximum(1.0, tf.reduce_sum(tf.to_float(e1_mas), axis=-2))# B*10 768e1 = tf.reshape(e1, [-1, hidden_size])# B 10 F 1e2_mas = tf.reshape(extras.e2_mas, [-1, FLAGS.max_num_relations, from_seq_length, 1])# B 10 F 768e2 = tf.multiply(output_layer, tf.to_float(e2_mas))# B 10 768e2 = tf.reduce_sum(e2, axis=-2) / tf.maximum(1.0, tf.reduce_sum(tf.to_float(e2_mas), axis=-2))# B*10 768e2 = tf.reshape(e2, [-1, hidden_size])# B*10 768*2output_layer = tf.concat([e1, e2], axis=-1)output_weights = tf.get_variable("cls/entity/output_weights", [num_labels, hidden_size*2],initializer=tf.truncated_normal_initializer(stddev=0.02))output_bias = tf.get_variable("cls/entity/output_bias", [num_labels], initializer=tf.zeros_initializer())with tf.variable_scope("loss"):if is_training:# I.e., 0.1 dropoutoutput_layer = tf.nn.dropout(output_layer, keep_prob=0.9)# B*10 num_labellogits = tf.matmul(output_layer, output_weights, transpose_b=True)# B*10 num_labellogits = tf.nn.bias_add(logits, output_bias)# B*10 num_labelprobabilities = tf.nn.softmax(logits, axis=-1)# B*10 num_labellog_probs = tf.nn.log_softmax(logits, axis=-1)# B*10labels = tf.reshape(labels, [-1])# B*10 num_labelone_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)# B*10per_example_loss = -tf.reduce_sum(one_hot_labels * log_probs, axis=-1)# B*10cls_mask = tf.reshape(tf.to_float(extras.cls_mask), [-1])# B*10per_example_loss = per_example_loss * cls_maskloss = tf.reduce_sum(per_example_loss) / tf.reduce_sum(cls_mask)return (loss, per_example_loss, logits, probabilities)

说明

  • 通过bert得到的输出output_layer的形状是[4,128,768](这里表是句子的表示),其中4是batchsize的大小,128是最大的句子长度,768是每一个字对应的维度大小。
  • 我们预先定义了一个最大的关系数量为12,我们将 output_layer变形为[4,12,128,768],这里的12是定义的最大的关系相数量。
  • 对于extras.e1_mas而言,它的维度是[4,1536],我们将他们重新调整为[4,12,128,1]
  • 接着将output_layer:[4,12,128,768]和e1_mas:[4,12,128,1]进行逐元素相乘,得到e1:[4,12,128,768],由于e1_mas是一个mask矩阵,相乘之后我们就将不是实体的字进行屏蔽了。
  • 对实体表示进行归一化后得到[4,12,768],在转换为[48,768]。
  • 对一个句子中的另一个实体进行同样的处理,得到e2,维度是[48,768]。
  • 将e1和e2进行拼接,得到最终的output_layer:[48,1536]
  • 经过一个全连接层,即:[48,1536]和[6,1536]作矩阵乘法,得到[48,6]
  • 最后就是计算一些相关的东西了,比如loss等。这里需要注意的是,我们需要将没有关系的地方忽略掉,让它们不参与计算。

参考代码:https://sourcegraph.com/github.com/helloeve/mre-in-one-pass/-/blob/run_classifier.py#L379

【关系抽取-mre-in-one-pass】模型的建立相关推荐

  1. 批量Excel操作——paddlenlp进行ner和主谓宾关系抽取二次处理详解-Taskflow模型基础上使用

    参考这篇讲的很清楚详细, 下面记录一些自己的二次处理. 批量读取Excel列数据进行ner并写入 import pandas as pd import read_excel df=read_excel ...

  2. nlp中的实体关系抽取方法总结

    点击上方,选择星标或置顶,每天给你送干货! 阅读大概需要35分钟 跟随小博主,每天进步一丢丢 来自:知乎 地址:https://zhuanlan.zhihu.com/p/77868938 作者:Jay ...

  3. 关系抽取论文总结(relation extraction)不断更新

    2000 1.Miller, Scott, et al. "A novel use of statistical parsing to extract information from te ...

  4. 知识图谱从哪儿来?实体关系抽取的现状和未来

    12月17日晚,2019年清华特奖获得者之一,清华大学自然语言处理实验室大四本科生高天宇,在智源论坛Live第1期,以<实体关系抽取的现状和未来>为主题,与150位观众进行了在线交流.本文 ...

  5. 【信息抽取】如何使用循环神经网络进行关系抽取

    事物.概念之间的关系是人类知识中非常重要的一个部分,但是他们通常隐藏在海量的非结构文本中.为了从文本中抽取这些关系事实,从早期的模式匹配到近年的神经网络,大量的研究在多年前就已经展开. 然而,随着互联 ...

  6. 技术动态 | 知识图谱从哪里来:实体关系抽取的现状与未来

    本文作者为:韩旭.高天宇.刘知远.转载自刘知远老师的知乎专栏,文章链接:https://zhuanlan.zhihu.com/p/91762831 最近几年深度学习引发的人工智能浪潮席卷全球,在互联网 ...

  7. 文献阅读课13-DSGAN: Generative Adversarial Training for (远程监督关系抽取,句子级去噪)

    文章目录 Abstract 1.Introduction 2.相关工作 3 Adversarial Learning for Distant Supervision 3.1 Pre-Training ...

  8. 【论文】Awesome Relation Extraction Paper(关系抽取)(PART V)

    写在前面 之前介绍的关系抽取系列文章主要是属于pipeline形式的,即第一步用实体识别将关系对抽取出来,第二步才是对关系对的分类,这样的做法会导致误差的传递.另外有一种解决方案是端到端的joint ...

  9. 信息抽取——关系抽取

    向AI转型的程序员都关注了这个号???????????? 机器学习AI算法工程   公众号:datayx 简介信息抽取(information extraction),即从自然语言文本中,抽取出特定的 ...

  10. 必读!信息抽取(Information Extraction)【关系抽取】

    来源: AINLPer 微信公众号(每日给你好看-) 编辑: ShuYini 校稿: ShuYini 时间: 2020-08-11 引言     信息抽取(information extraction ...

最新文章

  1. python实现洗牌算法_为什么渔民耶茨最有用的洗牌算法?
  2. Spring 如何在一个事务中开启另一个事务?
  3. MAC Pro 同时安装 Python2 和 Python3
  4. VirtualBox安装Kali
  5. 对计算机描述错误的是什么意思,下列对计算机特点的描述中错误的是:________。...
  6. Lex-BERT:超越FLAT的中文NER模型?
  7. java动态添加view
  8. redis启动后出现WARNING you have Transparent Huge Pages (THP) support enabled in your kernel问题...
  9. 李子柒被坑,大厂生气了!字节跳动火速对杭州微念启动撤资
  10. 现在大厂面试,也太太太太太难了吧!
  11. 【数据库】SQL语句大全
  12. 计算机网络(5.13)运输层- TCP的拥塞控制方法
  13. STM32小车篇之超声波测距
  14. Qt-android开发环境搭建及打包安装测试hello world
  15. T(n)=2T(n/2)+n=o(nlogn)
  16. Unity一般工程升级到HDRP
  17. 倡议书格式范文_写倡议书的格式及范文
  18. 尝试编写一个加密程序,加密方法将明文的字母变成其后的第4个字母,**字母表首尾相连。非字母符号** 不加密。 例如输入“China“, 输出密文“Glmre“, 输入 “ab123“, 输出 “ef1
  19. return false和return true
  20. OpenCV-python利用蒙版进行叠加(含alpha通道)

热门文章

  1. 制作左右声道音频用什么软件比较好?
  2. 将数字转换成中文数字
  3. 苏宁易购开放平台_发力“内循环”市场 苏宁易购“零售服务商”进阶显成效...
  4. C语言实现中文模糊查询
  5. 海康将摄像头传输过来的rtsp协议转换成rtmp
  6. IAM - 身份识别与访问管理 系统 - 学习/实践
  7. Linux限制磁盘与内存配额【超详细】
  8. 朝曦建筑建材租赁管理系统
  9. github打开ipynb文件显示Sorry, something went wrong. Reload?
  10. JAVA EE综合项目(二手书籍出售消息)