基于tensorflow-多分类问题

当涉及两个以上类别的问题时,就是多分类回归问题,不再用signmoid函数,而是用softmax函数,softmax函数产生每个类别的概率,并且概率向量的所有元素相加为1,。当预测时,具有最高的softmax值类别成为输出或预测类别。
多分类回归的损失函数可写为:J(w)=[yi*log(φ(zi))]求和。其中,φ(z)是softmax函数。
导入相关的库:

import operator
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from functools import reduce
from sklearn import datasets as skds
from sklearn.model_selection import train_test_split
from tensorflow.examples.tutorials.mnist import input_data

加载数据集:

#mnist=input_data.read_data_sets(os.path.join(datasetslib.datasets_root,'mnist'),one_hot=True)
mnist=input_data.read_data_sets('MNIST_data',one_hot=True)

设置一些参数:

num_inputs=784
num_outputs=10

learning_rate=0.001
num_epochs=1
batch_size=100
num_batches=int(mnist.train.num_examples/batch_size)

定义模型和损失函数:

x=tf.placeholder(dtype=tf.float32,shape=[None,num_inputs],name='x')   #行数没有确定,列数是num_inputs,列数的数量只有1
y=tf.placeholder(dtype=tf.float32,shape=[None,num_outputs],name='y')   #行数没有确定,列数是num_outputs,列数的数量只有1

weights=tf.Variable(tf.zeros([784,10]),name='w')   #weights的维数1*1
biases=tf.Variable(tf.zeros([10]),name='b')

model=tf.nn.softmax(tf.matmul(x,weights)+biases)

loss=tf.reduce_mean(-tf.reduce_sum(y*tf.log(model),axis=1))
optimizer=tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss)

运行模型并输出分类的精度:

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(num_epochs):
        for epoch in range(num_batches):
            batch_x,batch_y=mnist.train.next_batch(batch_size)
            sess.run(optimizer,feed_dict={x:batch_x,y:batch_y})
            predictions_check=tf.equal(tf.argmax(model,1),tf.argmax(y,1))
            accuracy_function=tf.reduce_mean(tf.cast(predictions_check,tf.float32))
            feed_dict={x:mnist.test.images,y:mnist.test.labels}
            accuracy_score=sess.run(accuracy_function,feed_dict)
            print('epoch  {0:04f} accuracy={1:.8f}'.format(epoch,accuracy_score))

定义两个辅助函数帮助训练模型,在每次迭代中学习不同的批次,绘制每次迭代的分类精度:

def mnist_batch_func(batch_size=100):
    batch_x,batch_y=mnist.train.next_batch(batch_size)
    return [batch_x,batch_y]

def tensorflow_classification(num_epochs,num_batches,batch_size,
                             batch_func,optimizer,test_x,test_y):
    accuracy_epochs=np.empty(shape=[num_epochs],dtype=np.float32)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(num_epochs):
            for batch in range(num_batches):
                batch_x,batch_y=batch_func(batch_size)
                feed_dict={x:batch_x,y:batch_y}
                sess.run(optimizer,feed_dict)
            predictions_check=tf.equal(tf.argmax(model,1),tf.argmax(y,1))
            accuracy_function=tf.reduce_mean(tf.cast(predictions_check,tf.float32))
            feed_dict={x:test_x,y:test_y}
            accuracy_score=sess.run(accuracy_function,feed_dict)
            accuracy_epochs[epoch]=accuracy_score
            print('epoch  {0:04f} accuracy={1:.8f}'.format(epoch,accuracy_score))

	    plt.figure(figsize=(14,8))
            plt.axis([0,num_epochs,np.min(accuracy_epochs),np.max(accuracy_epochs)])
            plt.plot(accuracy_epochs,label='Accuracy Score')
            plt.title('Accuracy over Iterations')
            plt.xlabel('#Epoch')
            plt.ylabel('Accuracy Score')
            plt.legend()
            plt.show()

运行30次迭代:

num_epochs=30
tensorflow_classification(num_epochs=num_epochs,
             num_batches=num_batches,
             batch_size=batch_size,
             batch_func=mnist_batch_func,
             optimizer=optimizer,
             test_x=mnist.test.images,test_y=mnist.test.labels)

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值