跟我学算法-tensorflow 实现logistics 回归

时间:2024-11-13 15:07:26

tensorflow每个变量封装了一个程序,需要通过sess.run 进行调用

接下来我们使用一下使用mnist数据,这是一个手写图像的数据,训练集是55000*28*28, 测试集10000* 28*28

第一步:导入数据

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
# 导入数据
mnist = input_data.read_data_sets('data/', one_hot=True) print (" tpye of 'mnist' is %s" % (type(mnist)))
print (" number of trian data is %d" % (mnist.train.num_examples))
print (" number of test data is %d" % (mnist.test.num_examples)) training = mnist.train.images
traininglable = mnist.train.labels
testing = mnist.test.images
testinglabel = mnist.test.labels

第二步:初识化变量

#初始化x和y
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float', [None, 10])
# 初始化W和b
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10])) sess = tf.Session()

第三步: 构造初始化函数

# 构造多分类方程
actv = tf.nn.softmax(tf.matmul(x, W) + b)
# 构造代价函数y*log(y1), y1表示的是预测值
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
#训练模型 learning_rate = 0.01
#优化模型,使得cost最小化
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # 预测结果的最大值索引与真实值的索引进行比对, tf.argmax( , 1) #找出一行中的最大值的索引
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
# 计算正确率, tf.cast 把布尔值转换为数字形式
accr = tf.reduce_mean(tf.cast(pred, 'float'))

第四步:迭代优化参数

init = tf.global_variables_initializer()

# 训练次数
train_epoches = 50
# 每次抽取样本数
batch_size = 100
# 每5次循环打印一次结果
display_step = 5
sess = tf.Session()
sess.run(init) for train_epoch in range(train_epoches):
avg_cost = 0
# 每次选取100个数据,循环的次数
num_batch = int(mnist.train.num_examples/batch_size)
for i in range(num_batch):
# 抽取数据
bacth_x, bacth_y = mnist.train.next_batch(batch_size)
# 进行cost优化
sess.run(optm, feed_dict={x:bacth_x, y:bacth_y})
# 加上cost的值
feeds = {x:bacth_x, y:bacth_y}
avg_cost += sess.run(cost, feed_dict=feeds)/num_batch
# 每5次打印一次结果
if train_epoch % display_step == 0:
feeds_train = {x:bacth_x, y:bacth_y}
feed_test = {x:mnist.test.images, y:mnist.test.labels}
# 计算训练集的准确率, feed_dict的参数
train_acc = sess.run(accr, feed_dict=feeds_train)
# 计算测试集的准确率
test_acc = sess.run(accr, feed_dict=feed_test)
print("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f"
% (train_epoch, train_epoches, avg_cost, train_acc, test_acc))