程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] logistic regression (softmax multi classification)

編輯:Python

Tensorflow Realize the logical regression -softmax Many classification

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# softmax Many classification 
(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)
# Test set 
model.evaluate(test_image, test_label)
# Hot coding alone 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])

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved