import tensorflow as tf
from tensorflow.contrib import slim
if __name__ == "__main__":
from BiLSTM import BiLSTM
from SelfAttention import SelfAttention
from Dataset import Dataset
else:
from .BiLSTM import BiLSTM
from .SelfAttention import SelfAttention
from .Dataset import Dataset
class SentencePresentation(object):
def __init__(self, wv, lstm_size=128, layers=2, wv_dim=100, dim_a=200, dim_r=50,
alpha=1.0, lr=0.001, norm=5.0, drop_out=0.5, classes=2):
x, lens, vocab, dropout, output = BiLSTM(lstm_size=lstm_size, layers=layers, wv_dim=wv_dim).io_nodes()
self._x = x
self._lens = lens
self._vocab = vocab
self._dropout = dropout
self._output = output
self._attention = SelfAttention(output, 2*lstm_size, dim_a=dim_a, dim_r=dim_r).get_attention()
self._dim_r = dim_r
self._lstm_size = lstm_size
self._alpha = alpha
self._lr = lr
self._norm = norm
self._dr = drop_out
self._classes = classes
self._wv = wv
self._build_graph()
def _build_graph(self):
self._y = tf.placeholder(dtype=tf.int64, shape=[None], name='input-y')
self._mask = tf.sequence_mask(self._lens, dtype=tf.float64)
with tf.name_scope('sentence-embedding'):
self._M = tf.matmul(self._attention, self._output)
with tf.name_scope('fully-connected-layer'):
self._sentence_embedding = tf.reshape(self._M, shape=[-1, 2*self._dim_r*self._lstm_size])
#self._fc = slim.fully_connected(sentence_embedding, 500, activation_fn=tf.nn.relu)
self._fc = slim.fully_connected(self._sentence_embedding, self._classes, activation_fn=None)
self._pre = tf.nn.softmax(self._fc)
with tf.name_scope('penalization'):
AA_T = tf.matmul(self._attention, tf.transpose(self._attention, [0, 2, 1]))
cur_batch = tf.shape(self._x)[0]
I = tf.eye(self._dim_r, batch_shape=[cur_batch], dtype=tf.float64)
P = tf.square(tf.norm(AA_T - I, axis=[1, 2], ord='fro'))
with tf.name_scope('loss'):
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self._fc, labels=self._y)
self._loss = tf.reduce_mean(loss) + tf.reduce_mean(self._alpha*P)
optimizer = tf.train.AdamOptimizer(learning_rate=self._lr)
optimizer = tf.contrib.estimator.clip_gradients_by_norm(optimizer, clip_norm=self._norm)
optimizer = tf.contrib.estimator.clip_gradients_by_norm(optimizer, clip_norm=-self._norm)
self._train_op = optimizer.minimize(self._loss)
with tf.name_scope('acc'):
pre = tf.argmax(self._fc, axis=1)
acc = tf.equal(pre, self._y)
self._acc = tf.reduce_mean(tf.cast(acc, tf.float64))
def fit(self, sess, datapath, batch_size=50, epoch=5, max_len=500):
dataset = Dataset(sess, datapath, batch_size, '\t', max_len=max_len, epoch=epoch)
sess.run(tf.initialize_all_variables())
for steps, (c, ws, lens) in enumerate(dataset):
feed = {self._x: ws, self._y: c, self._lens: lens, self._vocab: self._wv, self._dropout: self._dr}
loss, acc, _ = sess.run([self._loss, self._acc, self._train_op], feed_dict=feed)
yield steps, loss, acc
def predict(self, sess, x, lens):
feed = {self._x: x, self._vocab: self._wv, self._dropout: 1.0, self._lens: lens}
labels, attentions, embedding = sess.run([self._pre, self._attention, self._sentence_embedding], feed_dict=feed)
return labels, attentions, embedding
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/PNG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 10 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/d5b8e16cddf148da83293d5bb68d9224_admin_maxin.jpg!1)
博士僧小星
- 粉丝: 1947
- 资源: 5905
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 652027170733254多亿影视.apk
- 创新实践CNN车牌识别项目
- 小程序版图像分类算法对中文数字识别-不含数据集图片-含逐行注释和说明文档.zip
- 公交卡管理系统一个实验
- C语言学生成绩管理系统(信息可保存读取txt文件)
- 网上零食销售系统论文.docx
- 小程序版图像分类算法对电脑配件分类识别-不含数据集图片-含逐行注释和说明文档.zip
- 小程序版深度学习CNN训练识别8种水果-不含数据集图片-含逐行注释和说明文档.zip
- 小程序版基于深度学习对手势动作分类识别-不含数据集图片-含逐行注释和说明文档.zip
- 小程序版深度学习CNN训练识别手写数字图像中的数字分类-不含数据集图片-含逐行注释和说明文档.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)