TensorFlow入門 邏輯回歸之softmax

2021-09-21 18:36:57 字數 3278 閱讀 4702

#coding=utf-8

import tensorflow as tf

import numpy as np

import pandas as pd

import cv2 as cv

import os

from tensorflow.examples.tutorials.mnist import input_data

print

(tf.__version__)

mnist = input_data.read_data_sets(

"mnist_data/"

, one_hot=

true

)def

logistic_regression()

:"""

輸入訓練或者測試資料(28*28)

這裡shape中的none,意味著行數不定,由輸入的時候來確定

"""x = tf.placeholder(shape=

[none

,784

], dtype=tf.float32)

""" 輸入資料對應的標籤,代表著(0-9)的數字,用於和訓練結果進行比較

這裡shape中的none,意味著行數不定,由輸入的時候來確定

"""y = tf.placeholder(shape=

[none,10

], dtype=tf.float32)

# 隱藏層的權重,訓練過程中會不斷更新

w = tf.variable(tf.zeros(

[784,10

]), dtype=tf.float32)

# 隱藏層的偏置,訓練過程中會不斷更新

b = tf.variable(tf.zeros([10

]), dtype=tf.float32)

""" 輸出層的權重,訓練過程中會不斷更新

1.tf.add(tf.matmul(x, w), b)

上面的api返回的是乙個n*10的矩陣,每行的元素代表識別的輸入影象結果,採用的one-hot編碼

2.tf.nn.softmax

softmax在這裡的作用是將矩陣中的元素(float型別)轉換為(0-1)的小數,同時所有成員之和為1

這裡會將結果(0-10)每個值可能性的權重差異性放大

"""model = tf.nn.softmax(tf.add(tf.matmul(x, w)

, b)

)# softmax的損失函式,不能改

# y * tf.log(model)相當於tf.multiply(x, y, name=none),這是點乘不是矩陣乘法(tf.matmul),要求x,y具備相同的型別,返回的值也是同型別的,如下表示的是取出model中最大值(是個0到1的小樹),之後取對數(越大的值取對數後越趨近於0)對整個輸入集合求和

loss =

-tf.reduce_sum(y * tf.log(model)

)# 學習力

optimizer = tf.train.gradientdescentoptimizer(

0.01

)# 優化器設定

step = optimizer.minimize(loss)

# 初始化tensorflow變數

init = tf.global_variables_initializer(

)"""

1.從訓練結果(乙個包含10個元素的一維矩陣)中取出最大的乙個元素所在的序號

2.和真實數字代表的一維矩陣進行比較

3.輸出維度和元素個數相同的矩陣

4.例如: [true]代表本次識別成功,

[false]則代表本次識別失敗

acc_mat:行數由輸入來決定,列數為1

demo:

[false]

[false]

[false]

[true]

[true]

..."""

acc_mat = tf.equal(tf.argmax(model,1)

, tf.argmax(y,1)

)"""

tf.cast(acc_mat, dtype=tf.float32)

上面是將acc_mat中的true轉換為1,false轉換為0

tf.reduce_sum

用於將二維矩陣中每個元素求和

-------------------------

那麼經過上面操作後,acc_ret代表的就是識別成功的次數

"""acc_ret = tf.reduce_sum(tf.cast(acc_mat, dtype=tf.float32)

)with tf.session(

)as sess:

# 初始化tf的變數

sess.run(init)

# 訓練次數

for i in

range

(10000):

# 每次訓練給到的訓練有多少張

batch_xs, batch_ys = mnist.train.next_batch(

100)

# 開始訓練

sess.run(step, feed_dict=

)# 每間隔多少次,測試下訓練成功率,測試的時候輸入測試1000張

if(i +1)

%1000==0

: curr_acc = sess.run(acc_ret, feed_dict=

)print

("current accu : %f"

%(curr_acc)

)logistic_regression(

)

結果:

current accu : 894.000000

current accu : 912.000000

current accu : 913.000000

current accu : 909.000000

current accu : 910.000000

current accu : 902.000000

current accu : 912.000000

current accu : 910.000000

current accu : 918.000000

current accu : 912.000000

process finished with exit code 0

邏輯回歸的訓練效果會比之前的tensorflow入門:mnist資料集解析效果好一些

TensorFlow 實現Softmax 回歸模型

importtensorflowastf importnumpyasnp importtensorflow.examples.tutorials.mnist.input dataasinput data mnist input data.read data sets mnist data one h...

tensorflow實現softmax回歸函式

softmax函式可以把多個值歸一化到 0,1 區間中,以實現分類等問題 輸出 5 dtype float32,numpy array 0.1738882 0.7230672,0.8248408,0.8263163 1.5385914 1.3104331 0.91867334 1.5094105,0...

Tensorflow實現邏輯回歸

import tensorflow as tf 匯入mnist資料集 from tensorflow.examples.tutorials.mnist import input data mnist input data.read data sets temp data one hot true 定...