Tensorflow实现逻辑回归-softmax多分类
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt# softmax多分类
(train_image, train_label), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()print(train_image.shape)
print(train_label.shape)
print(test_image.shape)
print(test_label.shape)plt.imshow(train_image[0])
plt.show()plt.imshow(test_image[0])
plt.show()model = tf.keras.Sequential()
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'))model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc'])
model.fit(train_image, train_label, epochs=5)# 测试集
model.evaluate(test_image, test_label)# 独热编码 onehot
train_label_onehot = tf.keras.utils.to_categorical(train_label)
print(train_label_onehot)test_label_onehot = tf.keras.utils.to_categorical(test_label)
print(test_label_onehot)model = tf.keras.Sequential()
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'))model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.fit(train_image, train_label_onehot, epochs=5)predict = model.predict(test_image)
print(predict.shape)
print(predict[0])
print(np.argmax(predict[0]))
print(test_label[0])
推荐阅读
- TensorFlow实战|TensorFlow实战之softmax多分类
- python|Python 有哪些好玩的语法糖()
- python|zabbix(设置企业微信告警(python3脚本))
- python|Python爬虫某音乐平台的热门栏目音频数据
- python|【python】我用python抓取了19个一线城市三年的房价数据,并做了走势分析
- Unity3D|Unity3D ML-Agent-0.8.1 学习一(基础教程)
- pycharm|SyntaxError: unexpected character after line continuation character
- 机器学习|无源域适应(SFDA)方向的领域探究和论文复现(第二部分)
- 聚类|无源域适应(SFDA)方向的领域探究和论文复现(第一部分)