1. import tensorflow as tf
  2. from tensorflow.examples.tutorials.mnist import input_data
  3. mnist=input_data.read_data_sets("tmp/data", one_hot=True)
  4.  
  5. learning_rate=0.01
  6. training_epochs=25
  7. batch_size=100
  8. display_step=1
  9.  
  10. # placeholder x,y 用来存储输入,输入图像x构成一个2维的浮点张量,[None,784]是简单的平铺图,'None'代表处理的批次大小,是任意大小
  11. x=tf.placeholder(tf.float32,[None,784])
  12. y=tf.placeholder(tf.float32,[None,10])
  13.  
  14. # variables 为模型定义权重和偏置
  15. w=tf.Variable(tf.zeros([784,10]))
  16. b=tf.Variable(tf.zeros([10]))
  17.  
  18. pred=tf.nn.softmax(tf.matmul(x,w)+b) # w*x+b要加上softmax函数
  19.  
  20. # reduce_sum 对所有类别求和,reduce_mean 对和取平均
  21. cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
  22.  
  23. # 往graph中添加新的操作,计算梯度,计算参数的更新
  24. optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
  25.  
  26. init=tf.initialize_all_variables()
  27.  
  28. with tf.Session() as sess:
  29. sess.run(init)
  30. for epoch in range(training_epochs):
  31. total_batch=int(mnist.train.num_examples/batch_size)
  32. for i in range(total_batch):
  33. batch_xs,batch_ys=mnist.train.next_batch(batch_size)
  34. sess.run(optimizer,feed_dict={x:batch_xs,y:batch_ys})
  35. if( epoch+1)%display_step==0:
  36. print "cost=", sess.run(cost,feed_dict={x:batch_xs,y:batch_ys})
  37.  
  38. prediction=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
  39. accuracy=tf.reduce_mean(tf.cast(prediction,tf.float32))
  40. print "Accuracy:" ,accuracy.eval({x:mnist.test.image,y:mnist.test.labels})

  

logistic 函数:

二分类问题

softmax 函数:

将k维向量压缩成另一个k维向量,进行多分类,logistic 是softmax的一个例外

tensorflow--logistic regression的更多相关文章

  1. Linear and Logistic Regression in TensorFlow

    Linear and Logistic Regression in TensorFlow Graphs and sessions TF Ops: constants, variables, funct ...

  2. 逻辑回归 Logistic Regression

    逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...

  3. logistic regression与SVM

    Logistic模型和SVM都是用于二分类,现在大概说一下两者的区别 ① 寻找最优超平面的方法不同 形象点说,Logistic模型找的那个超平面,是尽量让所有点都远离它,而SVM寻找的那个超平面,是只 ...

  4. Logistic Regression - Formula Deduction

    Sigmoid Function \[ \sigma(z)=\frac{1}{1+e^{(-z)}} \] feature: axial symmetry: \[ \sigma(z)+ \sigma( ...

  5. SparkMLlib之 logistic regression源码分析

    最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...

  6. [OpenCV] Samples 06: [ML] logistic regression

    logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...

  7. Stanford机器学习笔记-2.Logistic Regression

    Content: 2 Logistic Regression. 2.1 Classification. 2.2 Hypothesis representation. 2.2.1 Interpretin ...

  8. Logistic Regression vs Decision Trees vs SVM: Part II

    This is the 2nd part of the series. Read the first part here: Logistic Regression Vs Decision Trees ...

  9. Logistic Regression Vs Decision Trees Vs SVM: Part I

    Classification is one of the major problems that we solve while working on standard business problem ...

  10. Logistic Regression逻辑回归

    参考自: http://blog.sina.com.cn/s/blog_74cf26810100ypzf.html http://blog.sina.com.cn/s/blog_64ecfc2f010 ...

随机推荐

  1. HDU5985 Lucky Coins 概率dp

    题意:给你N种硬币,每种硬币有Si个,有Pi 概率朝上,每次抛所有硬币抛起,所有反面的拿掉,问每种硬币成为最后的lucky硬币的概率. 题解:都知道是概率dp,但是模拟赛时思路非常模糊,很纠结,dp[ ...

  2. 20165223 week1测试查漏补缺

    week1查漏补缺 经过第一周的学习后,在蓝墨云班课上做了一套31道题的小测试,下面是对测试题中遇到的错误的分析和总结: 一.背记题 不属于Java后继技术的是? Ptyhon Java后继技术有? ...

  3. HEOI2013 Segment

    传说中的“李超树”. 大意:给你若干线段,试求横坐标x上的最上方一条线段的编号.无则输出零. 解:用线段树维护. 插入的时候保存自己这个区间上可能成为最大值的线段,被抛弃的则看情况下放. 查询时从最底 ...

  4. 编译:ffmpeg,精简ffmpeg.exe

    网上下载的各种 ffmpeg.exe ,最少都有11M+ 而我只需要处理 mp4 和 mp3,在网上搜索了一下精简ffmpeg的文章,折腾一天,也没有完全搞定,但多少有些收获,记录一下: 从 www. ...

  5. django orm跨表查询废话最少最精简版

    在model.py中: class B1(models.Model): u1= models.CharField(max_length=32) #多 class B2(models.Model): f ...

  6. get请求中params参数的使用

    一.当发送一个get请求的时候,如果有参数,那么参数应该怎么处理呢? 比如,百度阅读里面,查询书的列表,点击进去,它是一个get请求,地址是:https://yuedu.baidu.com/book/ ...

  7. unittest的使用一

    selenium: (1).firefox官方下载驱动geckodriver,windows:放在\python36或者是27的目录下 Mac: /usr/local/bin (2).firefox的 ...

  8. Docker下安装Influxdb-1.6.1和Grafana5.2.2

    第一步.安装Influxdb 首先启动docker systemctl start docker 然后安装Influxdb(这里解释一下为啥用docker,因为官网下载的话需要FQ[fan-qiang ...

  9. c#Linq联合查询

    public void Test2() { var queryResult1 = from e in empArray from l in empLevelArray select new { e, ...

  10. 数据库 价格字段 设置 decimal(8,2),价格为100W,只显示999999.99

    DECIMAL(M,D),M是数字最大位数,D是小数点右侧数字个数,整数M-D位 decimal(8,2)数值范围是 -999999.99 ~ 999999.99 1000000超过了6位,严格模式下 ...