四. softmax多分类

多个选项的分类问题,用softmax.
softmax把神经网络输出值变为概率分布,要求每个样本必须属于某个类别,且所有类别均被覆盖。

categorical_crossentropysparse_categorical_crossentropy计算softmax交叉熵。

  • categorical_crossentropy:label是one-hot处理过的,独热编码
[[0. 0. 0. ... 0. 0. 1.]#不同分类
 [1. 0. 0. ... 0. 0. 0.]
 [1. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [1. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
 
train_label_onehot = tf.keras.utils.to_categorical(train_label)#独热编码
model.compile(
                optimizer='adam',
                loss='categorical_crossentropy',
                metrics=['acc']
        )
  • sparse_categorical_crossentropy:顺序模型,如[1, 0, 2, 0, 2]
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt

if __name__ == '__main__':
        '''
        训练集,测试集.
        下载fashion_mnist数据集
        '''
        (train_image,train_label), (test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data()

        #train_image.shape:  (60000, 28, 28) train_label.shape:  (60000,)
        print("train_image.shape: ",train_image.shape,"train_label.shape: ",train_label.shape)

        #test_image.shape:  (10000, 28, 28) test_label.shape:  (10000,)
        print("test_image.shape: ", test_image.shape, "test_label.shape: ", test_label.shape)
        '''
        显示第一章图片
        plt.imshow()函数负责对图像进行处理,并显示其格式,但是不能显示。其后跟着plt.show()才能显示出来。
        '''
        plt.imshow(train_image[0]),plt.show()

        train_image = train_image/255#像素最大255,归一化
        test_image = test_image/255

        model = tf.keras.Sequential()
        '''
        Flatten用于将输入层的数据压成一维的数据,一般用再卷积层和全连接层之间
        因为全连接层只能接收一维数据,而卷积层可以处理二维数据,
        就是全连接层处理的是向量,而卷积层处理的是矩阵
        '''
        model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
        model.add(tf.keras.layers.Dense(128,activation='relu'))
        model.add(tf.keras.layers.Dense(10,activation='softmax'))#一共分成10类,softmax把10个输出变为概率分布

        model.compile(
                optimizer='adam',
                loss='sparse_categorical_crossentropy',#顺序模型,[1, 0, 2, 0, 2]这种
                metrics=['acc']
        )
        history=model.fit(train_image,train_label,epochs=5)
        print(history)
        predict=model.evaluate(test_image,test_label)#测试

设置学习速率

model.compile(
                #optimizer='adam',
                optimizer=tf.keras.optimizers.Adam(learning_rate=0.01),
                loss='sparse_categorical_crossentropy',#顺序模型,[1, 0, 2, 0, 2]这种
                metrics=['acc']
        )
        '''
        def __init__(self,
               learning_rate=0.001,
               beta_1=0.9,
               beta_2=0.999,
               epsilon=1e-7,
               amsgrad=False,
               name='Adam',
               **kwargs):
        '''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值