最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

tensorflow實(shí)現(xiàn)邏輯回歸模型

 更新時(shí)間:2018年09月08日 09:42:36   作者:Missayaa  
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)邏輯回歸模型的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

邏輯回歸模型

邏輯回歸是應(yīng)用非常廣泛的一個(gè)分類機(jī)器學(xué)習(xí)算法,它將數(shù)據(jù)擬合到一個(gè)logit函數(shù)(或者叫做logistic函數(shù))中,從而能夠完成對(duì)事件發(fā)生的概率進(jìn)行預(yù)測(cè)。

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
#下載好的mnist數(shù)據(jù)集存在F:/mnist/data/中
mnist = input_data.read_data_sets('F:/mnist/data/',one_hot = True)
print(mnist.train.num_examples)
print(mnist.test.num_examples)

trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels

print(type(trainimg))
print(trainimg.shape,)
print(trainlabel.shape,)
print(testimg.shape,)
print(testlabel.shape,)

nsample = 5
randidx = np.random.randint(trainimg.shape[0],size = nsample)

for i in randidx:
  curr_img = np.reshape(trainimg[i,:],(28,28))
  curr_label = np.argmax(trainlabel[i,:])
  plt.matshow(curr_img,cmap=plt.get_cmap('gray'))
  plt.title(""+str(i)+"th Training Data"+"label is"+str(curr_label))
  print(""+str(i)+"th Training Data"+"label is"+str(curr_label))
  plt.show()


x = tf.placeholder("float",[None,784])
y = tf.placeholder("float",[None,10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

#
actv = tf.nn.softmax(tf.matmul(x,W)+b)
#計(jì)算損失
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv),reduction_indices=1))
#學(xué)習(xí)率
learning_rate = 0.01
#隨機(jī)梯度下降
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

#求1位置索引值 對(duì)比預(yù)測(cè)值索引與label索引是否一樣,一樣返回True
pred = tf.equal(tf.argmax(actv,1),tf.argmax(y,1))
#tf.cast把True和false轉(zhuǎn)換為float類型 0,1
#把所有預(yù)測(cè)結(jié)果加在一起求精度
accr = tf.reduce_mean(tf.cast(pred,"float"))
init = tf.global_variables_initializer()
"""
#測(cè)試代碼 
sess = tf.InteractiveSession()
arr = np.array([[31,23,4,24,27,34],[18,3,25,4,5,6],[4,3,2,1,5,67]])
#返回?cái)?shù)組的維數(shù) 2
print(tf.rank(arr).eval())
#返回?cái)?shù)組的行列數(shù) [3 6]
print(tf.shape(arr).eval())
#返回?cái)?shù)組中每一列中最大元素的索引[0 0 1 0 0 2]
print(tf.argmax(arr,0).eval())
#返回?cái)?shù)組中每一行中最大元素的索引[5 2 5]
print(tf.argmax(arr,1).eval()) 
J"""
#把所有樣本迭代50次
training_epochs = 50
#每次迭代選擇多少樣本
batch_size = 100
display_step = 5

sess = tf.Session()
sess.run(init)

#循環(huán)迭代
for epoch in range(training_epochs):
  avg_cost = 0
  num_batch = int(mnist.train.num_examples/batch_size)
  for i in range(num_batch):
    batch_xs,batch_ys = mnist.train.next_batch(batch_size)
    sess.run(optm,feed_dict = {x:batch_xs,y:batch_ys})
    feeds = {x:batch_xs,y:batch_ys}
    avg_cost += sess.run(cost,feed_dict = feeds)/num_batch

  if epoch % display_step ==0:
    feeds_train = {x:batch_xs,y:batch_ys}
    feeds_test = {x:mnist.test.images,y:mnist.test.labels}
    train_acc = sess.run(accr,feed_dict = feeds_train)
    test_acc = sess.run(accr,feed_dict = feeds_test)
    #每五個(gè)epoch打印一次信息
    print("Epoch:%03d/%03d cost:%.9f train_acc:%.3f test_acc: %.3f" %(epoch,training_epochs,avg_cost,train_acc,test_acc))

print("Done")

程序訓(xùn)練結(jié)果如下:

Epoch:000/050 cost:1.177228655 train_acc:0.800 test_acc: 0.855
Epoch:005/050 cost:0.440933891 train_acc:0.890 test_acc: 0.894
Epoch:010/050 cost:0.383387268 train_acc:0.930 test_acc: 0.905
Epoch:015/050 cost:0.357281335 train_acc:0.930 test_acc: 0.909
Epoch:020/050 cost:0.341473956 train_acc:0.890 test_acc: 0.913
Epoch:025/050 cost:0.330586549 train_acc:0.920 test_acc: 0.915
Epoch:030/050 cost:0.322370980 train_acc:0.870 test_acc: 0.916
Epoch:035/050 cost:0.315942993 train_acc:0.940 test_acc: 0.916
Epoch:040/050 cost:0.310728854 train_acc:0.890 test_acc: 0.917
Epoch:045/050 cost:0.306357428 train_acc:0.870 test_acc: 0.918
Done

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Python中的變量及其命名和打印

    詳解Python中的變量及其命名和打印

    這篇文章主要介紹了Python中的變量及其命名和打印,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-03-03
  • Python 多進(jìn)程原理及實(shí)現(xiàn)

    Python 多進(jìn)程原理及實(shí)現(xiàn)

    這篇文章主要介紹了Python 多進(jìn)程原理及實(shí)現(xiàn),幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python反編譯的兩種實(shí)現(xiàn)方式

    Python反編譯的兩種實(shí)現(xiàn)方式

    這篇文章主要介紹了Python反編譯的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python實(shí)現(xiàn)中一次讀取多個(gè)值的方法

    Python實(shí)現(xiàn)中一次讀取多個(gè)值的方法

    下面小編就為大家分享一篇Python實(shí)現(xiàn)中一次讀取多個(gè)值的方法,具有很好的參考價(jià)值,我對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python三方庫(kù)之requests的快速上手

    python三方庫(kù)之requests的快速上手

    這篇文章主要介紹了python三方庫(kù)之requests的快速上手,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • 詳解如何在Python中實(shí)現(xiàn)遺傳算法

    詳解如何在Python中實(shí)現(xiàn)遺傳算法

    遺傳算法是一種模擬自然進(jìn)化過(guò)程與機(jī)制來(lái)搜索最優(yōu)解的方法,這篇文章主要為大家介紹了如何在Python中實(shí)現(xiàn)遺傳算法,感興趣的小伙伴可以了解一下
    2023-06-06
  • python中15種3D繪圖函數(shù)總結(jié)

    python中15種3D繪圖函數(shù)總結(jié)

    這篇文章主要為大家詳細(xì)介紹了python中15種3D繪圖函數(shù)的用法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-09-09
  • Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例

    Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息示例

    這篇文章主要介紹了Python使用Selenium模塊模擬瀏覽器抓取斗魚直播間信息,涉及Python基于Selenium模塊的模擬瀏覽器登陸、解析、抓取信息,以及MongoDB數(shù)據(jù)庫(kù)的連接、寫入等相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python中遍歷字典過(guò)程中更改元素導(dǎo)致異常的解決方法

    Python中遍歷字典過(guò)程中更改元素導(dǎo)致異常的解決方法

    這篇文章主要介紹了Python中遍歷字典過(guò)程中更改元素導(dǎo)致錯(cuò)誤的解決方法,針對(duì)增刪元素后出現(xiàn)dictionary changed size during iteration的異常解決做出討論和解決,需要的朋友可以參考下
    2016-05-05
  • Python本地及虛擬解釋器配置過(guò)程解析

    Python本地及虛擬解釋器配置過(guò)程解析

    這篇文章主要介紹了Python本地及虛擬解釋器配置過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10

最新評(píng)論

报价| 堆龙德庆县| 门源| 类乌齐县| 合阳县| 黔西| 平利县| 克拉玛依市| 大连市| 越西县| 连南| 衡阳县| 突泉县| 象山县| 云阳县| 叶城县| 泊头市| 苍南县| 庄河市| 门源| 桐庐县| 诸暨市| 神池县| 额济纳旗| 宁都县| 榆社县| 班戈县| 黄平县| 平舆县| 威海市| 屏东市| 建水县| 东莞市| 铁力市| 南充市| 凤台县| 德昌县| 阿尔山市| 湄潭县| 新绛县| 通化县|