2017曦源项目
1.概述
我结合了病人的血液动力学参数,依据采集的333个数据(实际有效采用302个数据)建立了一个预测病人脑埂患病与否的模型(采用python的tensorflow包)
2.对采集数据的清洗
- 一共采集了333例病患的数据,筛去一些资料不全的病患之后,最终剩下302例数据。
- 这302例数据中我将患脑梗的病患设置为1,不患脑梗的病患设置为0,这样方便计算机处理。
- 但是由于这302例数据都是患病的病患的数据,脑梗只是其中的一种疾病,由于缺少健康人群的数据,我只能拿患其他疾病的病患充作0,这最终会影响模型预测的准确性。
- 同时由于参数的众多(34个参数)以及病患数据的少之又少(302),也会影响预测的准确性,最终模型预测准确率61%
3.模型源代码
infarction_inference.py
import tensorflow as tf
INPUT_NODE = 34
OUTPUT_NODE = 2
LAYER1_NODE = 100
#返回前向传播的y值
def get_weight_variable(shape, regularizer):
weights = tf.get_variable("weights", shape, initializer=tf.truncated_normal_initializer(stddev=0.1))
if regularizer != None: tf.add_to_collection('losses', regularizer(weights))
return weights
def inference(input_tensor, regularizer):
with tf.variable_scope('layer1'):
weights = get_weight_variable([INPUT_NODE, LAYER1_NODE], regularizer)
biases = tf.get_variable("biases", [LAYER1_NODE], initializer=tf.constant_initializer(0.0))
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights) + biases)
with tf.variable_scope('layer2'):
weights = get_weight_variable([LAYER1_NODE, OUTPUT_NODE], regularizer)
biases = tf.get_variable("biases", [OUTPUT_NODE], initializer=tf.constant_initializer(0.0))
layer2 = tf.matmul(layer1, weights) + biases
return layer2
infarction_train.py
#训练使用240例数据
import infarction_inference
import tensorflow as tf
# from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import os
BATCH_SIZE = 240
LEARNING_RATE_BASE = 0.8
LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001
TRAINING_STEPS = 20000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH="infarction_model/"
MODEL_NAME="infarction_model"
def train(info):
x = tf.placeholder(tf.float32, [None, infarction_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, infarction_inference.OUTPUT_NODE], name='y-input')
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
y = infarction_inference.inference(x, regularizer)
global_step = tf.Variable(0, trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variables_averages_op = variable_averages.apply(tf.trainable_variables())
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_)
cross_entropy_mean = tf.reduce_mean(cross_entropy)
loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
15, LEARNING_RATE_DECAY,
staircase=True)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
with tf.control_dependencies([train_step, variables_averages_op]):
train_op = tf.no_op(name='train')
saver = tf.train.Saver()
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(TRAINING_STEPS):
xs = []
ys = []
start = (i * BATCH_SIZE) % 240
end = min(start + BATCH_SIZE, 240)
for j in range(start,end):
xs1 = info[j][0:34]
ys1 = np.zeros(2, dtype=np.float32)
label_index = info[j][34]
ys1[int(label_index)] = 1.0
ys.append(ys1)
xs.append(xs1)
_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: xs, y_: ys})
if i % 1000 == 0:
print("After %d training step(s), loss on training batch is %g." % (i, loss_value))
#训练每1000次保存一次当前的模型的所有参数,在infartion_eval.py中直接读取这些保存的模型
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main(argv=None):
info = np.loadtxt(r'all_info.txt')
info = info[0:240]
train(info)
if __name__ == '__main__':
tf.app.run()
infarction_eval.py
#测试使用剩下的62例数据
import tensorflow as tf
import infarction_inference
import infarction_train
import numpy as np
def evaluate(info):
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32, [None, infarction_inference.INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, infarction_inference.OUTPUT_NODE], name='y-input')
xs = []
ys = []
for i in range(0,62):
xs1 = info[i][0:34]
ys1 = np.zeros(2,dtype=np.float32)
label_index = info[i][34]
ys1[int(label_index)] = 1.0
ys.append(ys1)
xs.append(xs1)
validate_feed = {x: xs, y_: ys}
y = infarction_inference.inference(x, None)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
variable_averages = tf.train.ExponentialMovingAverage(infarction_train.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
#读取保存的模型参数
ckpt = tf.train.get_checkpoint_state(infarction_train.MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
accuracy_score = sess.run(accuracy, feed_dict=validate_feed)
print("After %s training step(s), validation accuracy = %g" % (global_step, accuracy_score))
else:
print('No checkpoint file found')
return
def main(argv=None):
info = np.loadtxt('all_info.txt')
info = info[240:]
evaluate(info)
if __name__ == '__main__':
main()
4.预测结果
在20000次训练之后,最终模型预测准确率只有约61%
5.模型改进的可能性
- 多采集病患和非病患的数据
- 增加模型当中的隐藏层数