import tensorflow as tf
class AttentionModel(object):
def __init__(self,
n_disease,
n_drug,
n_visit,
n_disease_category,
n_drug_category,
n_embed=500,
n_rnn=(200, 200),
dropout_rate=0.2,
batch_size=100):
self.n_disease = n_disease
self.n_drug = n_drug
self.n_visit = n_visit
self.n_disease_category = n_disease_category
self.n_drug_category = n_drug_category
self.n_embed = n_embed
self.n_rnn = n_rnn
self.dropout_rate = dropout_rate
self.batch_size = batch_size
def gru_unit(self, hidden_size, dropout_rate):
gru_cell = tf.nn.rnn_cell.GRUCell(num_units=hidden_size)
gru_cell = tf.nn.rnn_cell.DropoutWrapper(cell=gru_cell, input_keep_prob=1.0, output_keep_prob=1 - dropout_rate)
return gru_cell
def gru_unit_0(self, hidden_size, dropout_rate):
gru_cell = tf.nn.rnn_cell.GRUCell(num_units=hidden_size, name="0")
gru_cell = tf.nn.rnn_cell.DropoutWrapper(cell=gru_cell, input_keep_prob=1.0, output_keep_prob=1 - dropout_rate)
return gru_cell
# def build_model(self, x_disease, x_drug, disease_label, drug_label):
# x_code = tf.concat([x_disease, x_drug], axis=2)
# x_code = tf.reshape(x_code, [-1, self.n_disease + self.n_drug])
# w_embed = tf.get_variable('w_embed',
# shape=[self.n_disease + self.n_drug, self.n_embed])
# b_embed = tf.get_variable('b_mebed', shape=[self.n_embed])
# hidden = tf.nn.relu(tf.add(tf.matmul(x_code, w_embed), b_embed))
# hidden = tf.reshape(hidden, [-1, self.n_visit, self.n_embed])
# stacked_gru = tf.nn.rnn_cell.MultiRNNCell(
# [self.gru_unit(self.n_rnn[0], self.dropout_rate) for i in range(len(self.n_rnn))], state_is_tuple=True)
# outputs, states = tf.nn.dynamic_rnn(cell=stacked_gru, inputs=hidden, dtype=tf.float32)
# w_alpha = tf.get_variable('w_alpha',
# shape=[self.n_rnn[-1], 1])
# b_alpha = tf.get_variable('b_alpha', shape=[1])
# w_code = tf.get_variable('w_code',
# shape=[self.n_rnn[-1], self.n_disease_category + self.n_drug_category])
# b_code = tf.get_variable('b_code', shape=[self.n_disease_category + self.n_drug_category])
# code_loss = tf.Tensor
# code_y_last = tf.Tensor
# code_label = tf.concat([disease_label, drug_label], axis=2)
# for i in range(1, self.n_visit):
# outputs_reshape = tf.reshape(outputs, [-1, self.n_rnn[-1]])
# alpha = tf.reshape(
# tf.reduce_mean(tf.nn.softmax(tf.reshape(tf.add(tf.matmul(outputs_reshape, w_alpha), b_alpha),
# [-1, self.n_visit])[:, 0:i]), axis=1), [-1, 1])
# h_ = alpha * outputs[:, i, :]
# code_loss = tf.reduce_sum(
# tf.nn.softmax_cross_entropy_with_logits_v2(logits=tf.add(tf.matmul(h_, w_code), b_code),
# labels=code_label[:, i, :]))
# code_y_last = tf.nn.softmax(tf.add(tf.matmul(h_, w_code), b_code))
def build_model(self, x_disease, x_drug, disease_label, drug_label):
x_code = tf.concat([x_disease, x_drug], axis=2)
x_code = tf.reshape(x_code, [-1, self.n_disease + self.n_drug])
w_embed = tf.get_variable('w_embed',
shape=[self.n_disease + self.n_drug, self.n_embed])
b_embed = tf.get_variable('b_mebed', shape=[self.n_embed])
hidden = tf.nn.relu(tf.add(tf.matmul(x_code, w_embed), b_embed))
hidden = tf.reshape(hidden, [-1, self.n_visit, self.n_embed])
stacked_gru = tf.nn.rnn_cell.MultiRNNCell(
[self.gru_unit(self.n_rnn[0], self.dropout_rate) for i in range(len(self.n_rnn))], state_is_tuple=True)
outputs, states = tf.nn.dynamic_rnn(cell=stacked_gru, inputs=hidden, dtype=tf.float32)
stacked_gru_0 = tf.nn.rnn_cell.MultiRNNCell(
[self.gru_unit_0(self.n_rnn[0], self.dropout_rate) for i in range(len(self.n_rnn))], state_is_tuple=True)
outputs_0, states_0 = tf.nn.dynamic_rnn(cell=stacked_gru_0, inputs=hidden, dtype=tf.float32)
w_alpha = tf.get_variable('w_alpha',
shape=[self.n_rnn[-1], 1])
b_alpha = tf.get_variable('b_alpha', shape=[1])
w_code = tf.get_variable('w_code',
shape=[self.n_rnn[-1], self.n_disease_category + self.n_drug_category])
b_code = tf.get_variable('b_code', shape=[self.n_disease_category + self.n_drug_category])
code_loss = tf.Tensor
code_y_last = tf.Tensor
code_label = tf.concat([disease_label, drug_label], axis=2)
for i in range(1, self.n_visit):
outputs_reshape = tf.reshape(outputs, [-1, self.n_rnn[-1]])
alpha = tf.reshape(
tf.reduce_mean(tf.nn.softmax(tf.reshape(tf.add(tf.matmul(outputs_reshape, w_alpha), b_alpha),
[-1, self.n_visit])[:, 0:i]), axis=1), [-1, 1])
h_ = alpha * outputs_0[:, i, :]
code_loss = tf.reduce_sum(
tf.nn.softmax_cross_entropy_with_logits_v2(logits=tf.add(tf.matmul(h_, w_code), b_code),
labels=code_label[:, i, :]))
code_y_last = tf.nn.softmax(tf.add(tf.matmul(h_, w_code), b_code))
# x_code = tf.concat([x_disease, x_drug], axis=2)
# x_code = tf.reshape(x_code, [-1, self.n_disease + self.n_drug])
# w_embed = tf.get_variable('w_embed',
# shape=[self.n_disease + self.n_drug, self.n_embed])
# b_embed = tf.get_variable('b_mebed', shape=[self.n_embed])
# hidden = tf.nn.relu(tf.add(tf.matmul(x_code, w_embed), b_embed))
# hidden = tf.reshape(hidden, [-1, self.n_visit, self.n_embed])
# stacked_gru = tf.nn.rnn_cell.MultiRNNCell(
# [self.gru_unit(self.n_rnn[0], self.dropout_rate) for i in range(len(self.n_rnn))], state_is_tuple=True)
# outputs, states = tf.nn.dynamic_rnn(cell=stacked_gru, inputs=hidden, dtype=tf.float32)
# w_alpha = tf.get_variable('w_alpha',
# shape=[self.n_rnn[-1], 1])
# b_alpha = tf.get_variable('b_alpha', shape=[1])
# w_code = tf.get_variable('w_code',
# shape=[self.n_rnn[-1], self.n_disease_category + self.n_drug_category])
# b_code = tf.get_variable('b_code', shape=[self.n_disease_category + self.n_drug_category])
# code_loss = tf.Tensor
# code_y_last = tf.Tensor
# code_label = tf.concat([disease_label, drug_label], axis=2)
# for i in range(1, self.n_visit):
# outputs_reshape = tf.reshape(outputs, [-1, self.n_rnn[-1]])
# alpha = tf.reshape(tf.nn.softmax(tf.reshape(tf.add(tf.matmul(outputs_reshape, w_alpha), b_alpha),
# [-1, self.n_visit])[:, 0:i]), [-1, i, 1])
# c = tf.reduce_sum(alpha * outputs[:, 0:i, :], axis=1)
# h_ = tf.concat([c, outputs[:, i, :]], axis=1)
# # h_ = c * outputs[:, i, :]
# code_loss = tf.reduce_sum(
# tf.nn.softmax_cross_entropy_with_logits_v2(logits=tf.add(tf.matmul(h_, w_code), b_code),
# labels=code_label[:, i, :]))
# code_y_last = tf.nn.softmax(tf.add(tf.matmul(h_, w_code), b_code))
return code_loss, code_y_last
def print2file(self, content, out_file):
outfd = open(out_file, 'a')
outfd.write(content + '\n')
outfd.close()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip基于注意力机制的疾病诊断预测模型python源码.zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于注意力机制的疾病诊断预测模型python源码.zip (12个子文件)
attention_model
AttentionModelTest.py 4KB
Standard.py 725B
Config.py 1KB
AttentionModel.py 8KB
AttentionModelTrain.py 6KB
doctor_ai
ModelTrain.py 6KB
Standard.py 725B
ModelTest.py 4KB
Model.py 3KB
Config.py 1KB
data_process
DBOptions.py 592B
CreateDataset.py 7KB
共 12 条
- 1
资源评论
Make程序设计
- 粉丝: 6786
- 资源: 3575
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 部署yolov8的tensorrt模型支持检测分割姿态估计的C++源码+部署步骤.zip
- 以简单、易用、高性能为目标、开源的时序数据库,支持Linux及Windows, Time Series Database.zip
- python-leetcode面试题解之第198题打家劫舍-题解.zip
- python-leetcode面试题解之第191题位1的个数-题解.zip
- python-leetcode面试题解之第186题反转字符串中的单词II-题解.zip
- 一个基于python的web后端高性能开发框架,下载可用
- python-leetcode面试题解之第179题最大数-题解.zip
- python-leetcode面试题解之第170题两数之和III数据结构设计-题解.zip
- python-leetcode面试题解之第168题Excel表列名称-题解.zip
- python-leetcode面试题解之第167题两数之和II输入有序数组-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功