TensorFlow實(shí)現(xiàn)Logistic回歸
本文實(shí)例為大家分享了TensorFlow實(shí)現(xiàn)Logistic回歸的具體代碼,供大家參考,具體內(nèi)容如下
1.導(dǎo)入模塊
import numpy as np import pandas as pd from pandas import Series,DataFrame from matplotlib import pyplot as plt %matplotlib inline #導(dǎo)入tensorflow import tensorflow as tf #導(dǎo)入MNIST(手寫(xiě)數(shù)字?jǐn)?shù)據(jù)集) from tensorflow.examples.tutorials.mnist import input_data
2.獲取訓(xùn)練數(shù)據(jù)和測(cè)試數(shù)據(jù)
import ssl ssl._create_default_https_context = ssl._create_unverified_context mnist = input_data.read_data_sets('./TensorFlow',one_hot=True) test = mnist.test test_images = test.images train = mnist.train images = train.images
3.模擬線性方程
#創(chuàng)建占矩陣位符X,Y X = tf.placeholder(tf.float32,shape=[None,784]) Y = tf.placeholder(tf.float32,shape=[None,10]) #隨機(jī)生成斜率W和截距b W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) #根據(jù)模擬線性方程得出預(yù)測(cè)值 y_pre = tf.matmul(X,W)+b #將預(yù)測(cè)值結(jié)果概率化 y_pre_r = tf.nn.softmax(y_pre)
4.構(gòu)造損失函數(shù)
# -y*tf.log(y_pre_r) --->-Pi*log(Pi) 信息熵公式 cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(y_pre_r),axis=1))
5.實(shí)現(xiàn)梯度下降,獲取最小損失函數(shù)
#learning_rate:學(xué)習(xí)率,是進(jìn)行訓(xùn)練時(shí)在最陡的梯度方向上所采取的「步」長(zhǎng); learning_rate = 0.01 optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
6.TensorFlow初始化,并進(jìn)行訓(xùn)練
#定義相關(guān)參數(shù) #訓(xùn)練循環(huán)次數(shù) training_epochs = 25 #batch 一批,每次訓(xùn)練給算法10個(gè)數(shù)據(jù) batch_size = 10 #每隔5次,打印輸出運(yùn)算的結(jié)果 display_step = 5 #預(yù)定義初始化 init = tf.global_variables_initializer() #開(kāi)始訓(xùn)練 with tf.Session() as sess: #初始化 sess.run(init) #循環(huán)訓(xùn)練次數(shù) for epoch in range(training_epochs): avg_cost = 0. #總訓(xùn)練批次total_batch =訓(xùn)練總樣本量/每批次樣本數(shù)量 total_batch = int(train.num_examples/batch_size) for i in range(total_batch): #每次取出100個(gè)數(shù)據(jù)作為訓(xùn)練數(shù)據(jù) batch_xs,batch_ys = mnist.train.next_batch(batch_size) _, c = sess.run([optimizer,cost],feed_dict={X:batch_xs,Y:batch_ys}) avg_cost +=c/total_batch if(epoch+1)%display_step == 0: print(batch_xs.shape,batch_ys.shape) print('epoch:','%04d'%(epoch+1),'cost=','{:.9f}'.format(avg_cost)) print('Optimization Finished!') #7.評(píng)估效果 # Test model correct_prediction = tf.equal(tf.argmax(y_pre_r,1),tf.argmax(Y,1)) # Calculate accuracy for 3000 examples # tf.cast類型轉(zhuǎn)換 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) print("Accuracy:",accuracy.eval({X: mnist.test.images[:3000], Y: mnist.test.labels[:3000]}))
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- tensorflow實(shí)現(xiàn)簡(jiǎn)單邏輯回歸
- Tensorflow使用支持向量機(jī)擬合線性回歸
- TensorFlow實(shí)現(xiàn)iris數(shù)據(jù)集線性回歸
- 用TensorFlow實(shí)現(xiàn)戴明回歸算法的示例
- 用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例
- 詳解用TensorFlow實(shí)現(xiàn)邏輯回歸算法
- TensorFlow實(shí)現(xiàn)Softmax回歸模型
- 運(yùn)用TensorFlow進(jìn)行簡(jiǎn)單實(shí)現(xiàn)線性回歸、梯度下降示例
- 用tensorflow構(gòu)建線性回歸模型的示例代碼
- 用tensorflow實(shí)現(xiàn)彈性網(wǎng)絡(luò)回歸算法
相關(guān)文章
python實(shí)現(xiàn)在windows服務(wù)中新建進(jìn)程的方法
這篇文章主要介紹了python實(shí)現(xiàn)在windows服務(wù)中新建進(jìn)程的方法,涉及Python針對(duì)Windows服務(wù)與進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例
這篇文章主要介紹了Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03python實(shí)現(xiàn)rest請(qǐng)求api示例
這篇文章主要介紹了python實(shí)現(xiàn)rest請(qǐng)求api示例,需要的朋友可以參考下2014-04-04python轉(zhuǎn)換wrf輸出的數(shù)據(jù)為網(wǎng)頁(yè)可視化json格式
這篇文章主要介紹了python轉(zhuǎn)換wrf輸出的數(shù)據(jù)為網(wǎng)頁(yè)可視化json格式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Python爬蟲(chóng)實(shí)現(xiàn)網(wǎng)頁(yè)信息抓取功能示例【URL與正則模塊】
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)網(wǎng)頁(yè)信息抓取功能,涉及Python使用URL與正則模塊針對(duì)網(wǎng)頁(yè)信息的讀取與匹配相關(guān)操作技巧,需要的朋友可以參考下2017-05-05YOLOv5車牌識(shí)別實(shí)戰(zhàn)教程(七)實(shí)時(shí)監(jiān)控與分析
這篇文章主要介紹了YOLOv5車牌識(shí)別實(shí)戰(zhàn)教程(七)實(shí)時(shí)監(jiān)控與分析,在這個(gè)教程中,我們將一步步教你如何使用YOLOv5進(jìn)行車牌識(shí)別,幫助你快速掌握YOLOv5車牌識(shí)別技能,需要的朋友可以參考下2023-04-04python中ASCII碼字符與int之間的轉(zhuǎn)換方法
今天小編就為大家分享一篇python中ASCII碼字符與int之間的轉(zhuǎn)換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07