侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

tensorflow实现逻辑回归模型

技术  /  管理员 发布于 7年前   379

逻辑回归模型

逻辑回归是应用非常广泛的一个分类机器学习算法,它将数据拟合到一个logit函数(或者叫做logistic函数)中,从而能够完成对事件发生的概率进行预测。

import numpy as npimport tensorflow as tfimport matplotlib.pyplot as pltfrom tensorflow.examples.tutorials.mnist import input_data#下载好的mnist数据集存在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.imagestrainlabel = mnist.train.labelstestimg = mnist.test.imagestestlabel = mnist.test.labelsprint(type(trainimg))print(trainimg.shape,)print(trainlabel.shape,)print(testimg.shape,)print(testlabel.shape,)nsample = 5randidx = 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)#计算损失cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv),reduction_indices=1))#学习率learning_rate = 0.01#随机梯度下降optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)#求1位置索引值 对比预测值索引与label索引是否一样,一样返回Truepred = tf.equal(tf.argmax(actv,1),tf.argmax(y,1))#tf.cast把True和false转换为float类型 0,1#把所有预测结果加在一起求精度accr = tf.reduce_mean(tf.cast(pred,"float"))init = tf.global_variables_initializer()"""#测试代码 sess = tf.InteractiveSession()arr = np.array([[31,23,4,24,27,34],[18,3,25,4,5,6],[4,3,2,1,5,67]])#返回数组的维数 2print(tf.rank(arr).eval())#返回数组的行列数 [3 6]print(tf.shape(arr).eval())#返回数组中每一列中最大元素的索引[0 0 1 0 0 2]print(tf.argmax(arr,0).eval())#返回数组中每一行中最大元素的索引[5 2 5]print(tf.argmax(arr,1).eval()) J"""#把所有样本迭代50次training_epochs = 50#每次迭代选择多少样本batch_size = 100display_step = 5sess = tf.Session()sess.run(init)#循环迭代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)    #每五个epoch打印一次信息    print("Epoch:%03d/%03d cost:%.9f train_acc:%.3f test_acc: %.3f" %(epoch,training_epochs,avg_cost,train_acc,test_acc))print("Done")

程序训练结果如下:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    使用tensorflow实现线性回归
    下一条:
    TensorFlow实现Logistic回归
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客