tensorflow 全连接层
用全连接层训练病例data然后测试其准确性
import tensorflow as tf
import numpy as np
import random
info = np.loadtxt(r'D:\深度学习和机器学习\xiyuan project\data\all_info.txt')
label = [info[i,-1] for i in range(len(info))]
info.shape
(302, 35)
info
array([[ 1. , 41. , 163. , ..., 48.66667, 49.35185,
0. ],
[ 1. , 72. , 150. , ..., 51.23967, 59.48543,
0. ],
[ 0. , 77. , 170. , ..., 66.44579, 57.12984,
0. ],
...,
[ 1. , 77. , 157. , ..., 49.87981, 60.03322,
0. ],
[ 1. , 75. , 152. , ..., 60.41177, 71.94764,
1. ],
[ 1. , 72. , 159. , ..., 58.10487, 54.23615,
0. ]])
train_data = [info[i,:-1] for i in range(len(info))][:251]
test_data = [info[i,:-1] for i in range(len(info))][251:]
train_label = label[:251]
test_label = label[251:]
len(test_data)
51
len(train_data)
251
len(test_label)
51
test_label
[0.0,
1.0,
1.0,
0.0,
0.0,
1.0,
1.0,
0.0,
0.0,
0.0,
1.0,
1.0,
1.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0,
1.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
1.0,
1.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0,
1.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0]
test_label_2 = np.zeros([len(test_label),2])
train_label_2 = np.zeros([len(train_label),2])
for i in range(len(test_label)):
if int(test_label[i])==0:
test_label_2[i][0] = 1.0
else:
test_label_2[i][1] = 1.0
for i in range(len(train_label)):
if int(train_label[i])==0:
train_label_2[i][0] = 1.0
else:
train_label_2[i][1] = 1.0
train_label_2
array([[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
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., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 1., 0.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.],
[ 0., 1.]])
1.设置输入和输出节点的个数,配置神经网络的参数。
INPUT_NODE = 34 # 输入节点
OUTPUT_NODE = 2 # 输出节点
LAYER1_NODE = 100 # 隐藏层数
# BATCH_SIZE = 100 # 每次batch打包的样本个数
# 模型相关的参数
LEARNING_RATE_BASE = 0.0005
LEARNING_RATE_DECAY = 0.99
REGULARAZTION_RATE = 0.0001
TRAINING_STEPS = 5000
MOVING_AVERAGE_DECAY = 0.99
2. 定义辅助函数来计算前向传播结果,使用ReLU做为激活函数。
def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2):
# 不使用滑动平均类
if avg_class == None:
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)
return tf.matmul(layer1, weights2) + biases2
else:
# 使用滑动平均类
layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1))
return tf.matmul(layer1, avg_class.average(weights2)) + avg_class.average(biases2)
3. 定义训练过程。
def train():
x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
# 生成隐藏层的参数。
weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))
# 生成输出层的参数。
weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))
# 计算不含滑动平均类的前向传播结果
y = inference(x, None, weights1, biases1, weights2, biases2)
# 定义训练轮数及相关的滑动平均类,在使用tensorflow训练神经网络时一般会将代表训练轮数的变量指定为不可训练的参数
global_step = tf.Variable(0, trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
# 在所有代表神经网络参数的变量上使用滑动平均,其他辅助变量(global_step)就不需要了
variables_averages_op = variable_averages.apply(tf.trainable_variables())
average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2)
# 计算交叉熵及其平均值
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
# 损失函数的计算
regularizer = tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE)
regularaztion = regularizer(weights1) + regularizer(weights2)
loss = cross_entropy_mean + regularaztion
# 设置指数衰减的学习率。
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
1,
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')
# 计算正确率
correct_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 初始化回话并开始训练过程。
with tf.Session() as sess:
tf.global_variables_initializer().run()
test_feed = {x: test_data, y_: test_label_2}
train_feed = {x: train_data, y_: train_label_2}
# 循环的训练神经网络。
for i in range(TRAINING_STEPS):
if i % 1000 == 0:
loss_1 = sess.run(loss, feed_dict=train_feed)
print("After %d training step(s), loss using average model is %g " % (i, loss_1))
xs,ys=train_data, train_label_2
sess.run(train_op,feed_dict={x:xs,y_:ys})
test_acc=sess.run(accuracy,feed_dict=test_feed)
train_acc = sess.run(accuracy, feed_dict=train_feed)
print(("After %d training step(s), test accuracy using average model is %g" %(TRAINING_STEPS, test_acc)))
print(("After %d training step(s), train accuracy using average model is %g" %(TRAINING_STEPS, train_acc)))
4. 主程序入口,这里设定模型训练次数为5000次。
def main(argv=None):
train()
if __name__=='__main__':
main()
After 0 training step(s), loss using average model is 284.597
After 1000 training step(s), loss using average model is 0.71054
After 2000 training step(s), loss using average model is 0.71054
After 3000 training step(s), loss using average model is 0.71054
After 4000 training step(s), loss using average model is 0.71054
After 5000 training step(s), test accuracy using average model is 0.647059
After 5000 training step(s), train accuracy using average model is 0.629482