tensorflow|四. softmax多分类

多个选项的分类问题,用softmax.
softmax把神经网络输出值变为概率分布,要求每个样本必须属于某个类别,且所有类别均被覆盖。
【tensorflow|四. softmax多分类】categorical_crossentropy和sparse_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 pltif __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/255model = 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): '''

    推荐阅读