TensorFlow-softmax分类 独热编码

对数几率回归解决的是二分类的问题, 对于多个选项的问题,我们可以使用softmax函数,它是对数几率回归在 N 个可能不同的值上的推广。
神经网络的原始输出不是一个概率值,实质上只是输入的数值做了复杂的加权和(乘以w+b)与非线性处理之后的一个值而已,那么
如何将这个输出变为概率分布?  -这就是Softmax层的作用

softmax要求每个样本必须属于某个类别,且所有可能的样本均被覆盖
1.softmax个样本分量之和为 1
  (1)概率之和为1.预测一个人来自广州,还是广西,上海等地,,这是个多分类问题,softmax分类之后,谁的概率大,预测值就是谁,当只有两个值的时候,->2。
2.当只有两个类别时,与对数几率回归完全相同
 

 tf.keras交叉熵

在tf.keras里,对于多分类问题我们使用

                                        categorical_crossentropy

                                        sparse_categorical_crossentropy   #顺序数字编码时使用

来计算softmax交叉熵

Fashion MNIST 数据集

Fashion MNIST 的作用是成为经典 MNIST 数据集的简易替换, MNIST 数据集包含手写数字(0、1、2 等)的图像,这些图像的格式与本节课中使用的服饰图像的格式相同。

Fashion MNIST 比常规 MNIST手写数据集更具挑战性。 这两个数据集都相对较小,用于验证某个算法能否如期正常 运行。它们都是测试和调试代码的良好起点。

Fashion MNIST 数据集包含 70000 张灰度图像,涵盖 10 个类别。

我们将使用 60000 张图像训练网络,并使用 10000 张图像评估经过学习的网络分类图像的准确率。

可以从 TensorFlow 直接访问 Fashion MNIST,只需导入和加载数据即可 

 Fashion MNIST会自动去下载那些图片与数据,这里直接导入,就不用慢慢下载了

multi-classification.py 代码如下:

import tensorflow as tf
#print('Tensorflow Version:{}'.format(tf.__version__))
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

(train_image,train_lable),(test_image,test_label)=tf.keras.datasets.fashion_mnist.load_data()   #将fashion_mnist直接加进来
train_image.shape
train_lable.shape
train_image.shape,train_lable.shape
plt.imshow(train_image[0])
#plt.show()
#print(train_image[0])
#print(np.max(train_image[0]))
#print(train_lable)
#数据归一化
train_image=train_image/255
test_image =test_image/255
#print(train_image.shape)
#(60000, 28, 28)
model=tf.keras.Sequential()
#Dense是将一个一维的数据映射另一个一维数据上(张量为一维)
#而这个是二维的,需要flatten转化为一维的
# model.add(tf.keras.layers.Flatten(input_shape=(28,28)))  #扁平成28*28的向量
# model.add(tf.keras.layers.Dense(128,activation='relu'))
# #输出
# model.add(tf.keras.layers.Dense(10,activation='softmax')) #变成长度为10个概率值,softmax把它激活成概率分布
# #编译模型
# model.compile(optimizer='adam',
#               loss='sparse_categorical_crossentropy', #顺序数字编码时使用
#               metrics=['acc'])     #度量正确率
# #训练
# model.fit(train_image,train_lable,epochs=5)
#评价
#model.evaluate(test_image,test_label)
#print(train_lable)
#独热编码
# beijing [1,0,0]
# shanghai [0,1,0]
# shenzhen [0,0,1]
train_lable_onehot=tf.keras.utils.to_categorical(train_lable)
# print(train_lable_onehot)
# # print(train_lable_onehot[-1])
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))  #扁平成28*28的向量
model.add(tf.keras.layers.Dense(128,activation='relu'))
#输出
model.add(tf.keras.layers.Dense(10,activation='softmax')) #变成长度为10个概率值,softmax把它激活成概率分布
#编译模型
model.compile(optimizer='adam',
              loss='categorical_crossentropy', #顺序数字编码时使用
              metrics=['acc'])     #度量正确率
#训练
model.fit(train_image,train_lable_onehot,epochs=5)
predict = model.predict(test_image)
print(test_image.shape)
predict.shape
print(predict[0])
print(np.argmax(predict[0]))    #预测最大值
#看看是不是最大值
print(test_label[0])

 其中的一张图片

 

第10000张

独热编码

print(train_lable)

打印出:        [9 0 0 ... 3 0 5]

print(train_lable_onehot)

print(train_lable_onehot[-1])

打印出:         [0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-panda

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值