Tensorflow實現邏輯回歸

2021-07-31 08:15:07 字數 4163 閱讀 4259

import tensorflow as tf

# 匯入mnist資料集

from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets("/temp/data/",one_hot=true)

# 定義一些常量

learning_rate=0.01

epoch_times=25

display_step=1

# 定義批量梯度下降次數,每100張圖計算一次梯度

batch_size=100

# 定義x,y大小 none代表第幾張圖,784=28*28,10 classes

# y=[[1,0,0...,0],

# [0,0,1...,0],

# [0,1,0...,0],

# [0,0,0...,1],

# ...........]

x=tf.placeholder("float", [none,784])

y=tf.placeholder("float", [none,10])

# 定義w,b大小

w=tf.variable(tf.zeros([784,10]))

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

# matmul()矩陣相乘 ;multiply()實數相乘

# sigmoid將乙個real value對映到(0,1)的區間(當然也可以是(-1,1)),這樣可以用來做二分類。

# softmax把乙個k維的real value向量(a1,a2,a3,a4….)對映成乙個(b1,b2,b3,b4….)

# 其中b是乙個0-1的常數,然後可以根據bi的大小來進行多分類的任務,如取權重最大的一維。

# 返回乙個10維矩陣

# 注意x,w前後順序 [none,784]*[784,10]=[none,10]

pred=tf.nn.softmax(tf.matmul(x, w)+b)

# -y*tf.log(pred):交叉熵代價函式

# 代價函式接近於0.(比如y=0,pred~0;y=1,pred~1時,代價函式都接近0)

# 這裡的*指對應下標的數相乘,不屬於向量相乘,因此返回矩陣大小仍為[none,10]

# tf.reduce_sum(-y*tf.log(pred),1):

# 返回每個例項的交叉熵(向量),1代表從水平方向求和

# tf.reduce_mean():返回所有交叉熵的平均值(實數)

cost= tf.reduce_mean(tf.reduce_sum(-y*tf.log(pred),1))

# 呼叫梯度下降函式

optimizer=tf.train.gradientdescentoptimizer(learning_rate).minimize(cost)

# 初始化引數方法

init=tf.global_variables_initializer()

# 執行引數初始化方法

sess=tf.session()

sess.run(init)

for epoch in range(epoch_times):

# 初始訓練集誤差為0

train_cost=0

# 批量梯度下降,返回總批量次數(55000/100=550)

batch_numbers=int(mnist.train.num_examples/batch_size)

for i in range(batch_numbers):

# 每次取100張

batch_xs,batch_ys=mnist.train.next_batch(batch_size)

# 執行優化函式

# 這裡返回乙個[optimizer,cost]的list, 其中 _代表optimizer,batch_cost代表cost的值

_,batch_cost=sess.run([optimizer,cost],feed_dict=)

# 等價於上面

# sess.run(optimizer,feed_dict=)

# batch_cost=sess.run(cost,feed_dict=)

# 返回訓練集誤差:每次計算100張圖的batch_cost,計算了i次,所以最後除以batch_numbers

train_cost+=batch_cost/batch_numbers

# 列印每次迭代的誤差

if (epoch+1)%display_step==0:

# %04d: % 轉義說明符 ; 0 指以0填充前面的位數 ;4 四位數; d 十進位制整數

# "".format(train_cost) 以保留小數點後9位顯示train_cost

print("epoch :","%04d"%(epoch+1),"train_cost","".format(train_cost))

print("optimization finished!")

# tf.arg_max(pred,1):得到向量中最大數的下標,1代表水平方向

# tf.equal():返回布林值,相等返回1,否則0

# 最後返回大小[none,1]的向量,1所在位置為布林型別資料

correct_prediction=tf.equal(tf.arg_max(pred,1),tf.arg_max(y,1))

# tf.cast():將布林型向量轉換成浮點型向量

# tf.reduce_mean():求所有數的均值

# 返回正確率:也就是所有為1的數目佔所有數目的比例

accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

# 列印正確率

print("train_accuracy :",sess.run(accuracy,feed_dict=))

print("test_accuracy :",sess.run(accuracy,feed_dict=))

epoch : 0001 train_cost 1.182139

epoch : 0002 train_cost 0.664811

epoch : 0003 train_cost 0.552634

epoch : 0004 train_cost 0.498518

epoch : 0005 train_cost 0.465431

epoch : 0006 train_cost 0.442534

epoch : 0007 train_cost 0.425447

epoch : 0008 train_cost 0.412136

epoch : 0009 train_cost 0.401330

epoch : 0010 train_cost 0.392388

epoch : 0011 train_cost 0.384719

epoch : 0012 train_cost 0.378171

epoch : 0013 train_cost 0.372416

epoch : 0014 train_cost 0.367238

epoch : 0015 train_cost 0.362694

epoch : 0016 train_cost 0.358609

epoch : 0017 train_cost 0.354874

epoch : 0018 train_cost 0.351399

epoch : 0019 train_cost 0.348344

epoch : 0020 train_cost 0.345431

epoch : 0021 train_cost 0.342731

epoch : 0022 train_cost 0.340269

epoch : 0023 train_cost 0.337952

epoch : 0024 train_cost 0.335766

epoch : 0025 train_cost 0.333705

optimization finished!

train_accuracy : 0.907927

test_accuracy : 0.9146

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書寫邏輯回歸

mnist 讀取訓練集 mnist.train.images mnist.train.labels 讀取訓練集的長度 mnist.train.num examples 函式 tf.placeholder 必須指定資料型別,shape可以不指定,這樣就可以使用多種shape了 tf.variable ...