tensorflow--logistic regression
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("tmp/data", one_hot=True) learning_rate=0.01
training_epochs=25
batch_size=100
display_step=1 # placeholder x,y 用来存储输入,输入图像x构成一个2维的浮点张量,[None,784]是简单的平铺图,'None'代表处理的批次大小,是任意大小
x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10]) # variables 为模型定义权重和偏置
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10])) pred=tf.nn.softmax(tf.matmul(x,w)+b) # w*x+b要加上softmax函数 # reduce_sum 对所有类别求和,reduce_mean 对和取平均
cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1)) # 往graph中添加新的操作,计算梯度,计算参数的更新
optimizer=tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) init=tf.initialize_all_variables() with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs):
total_batch=int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
sess.run(optimizer,feed_dict={x:batch_xs,y:batch_ys})
if( epoch+1)%display_step==0:
print "cost=", sess.run(cost,feed_dict={x:batch_xs,y:batch_ys}) prediction=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(prediction,tf.float32))
print "Accuracy:" ,accuracy.eval({x:mnist.test.image,y:mnist.test.labels})
logistic 函数:
二分类问题
softmax 函数:
将k维向量压缩成另一个k维向量,进行多分类,logistic 是softmax的一个例外
tensorflow--logistic regression的更多相关文章
- Linear and Logistic Regression in TensorFlow
Linear and Logistic Regression in TensorFlow Graphs and sessions TF Ops: constants, variables, funct ...
- 逻辑回归 Logistic Regression
逻辑回归(Logistic Regression)是广义线性回归的一种.逻辑回归是用来做分类任务的常用算法.分类任务的目标是找一个函数,把观测值匹配到相关的类和标签上.比如一个人有没有病,又因为噪声的 ...
- logistic regression与SVM
Logistic模型和SVM都是用于二分类,现在大概说一下两者的区别 ① 寻找最优超平面的方法不同 形象点说,Logistic模型找的那个超平面,是尽量让所有点都远离它,而SVM寻找的那个超平面,是只 ...
- Logistic Regression - Formula Deduction
Sigmoid Function \[ \sigma(z)=\frac{1}{1+e^{(-z)}} \] feature: axial symmetry: \[ \sigma(z)+ \sigma( ...
- SparkMLlib之 logistic regression源码分析
最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...
- [OpenCV] Samples 06: [ML] logistic regression
logistic regression,这个算法只能解决简单的线性二分类,在众多的机器学习分类算法中并不出众,但它能被改进为多分类,并换了另外一个名字softmax, 这可是深度学习中响当当的分类算法 ...
- Stanford机器学习笔记-2.Logistic Regression
Content: 2 Logistic Regression. 2.1 Classification. 2.2 Hypothesis representation. 2.2.1 Interpretin ...
- 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 ...
- 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 ...
- Logistic Regression逻辑回归
参考自: http://blog.sina.com.cn/s/blog_74cf26810100ypzf.html http://blog.sina.com.cn/s/blog_64ecfc2f010 ...
随机推荐
- print
说一说这个print函数,我们经常使用,但有一些细节却往往错过了 print print()输出会换行是因为默认end="\n" 想要不换行,且覆盖 print("\r第 ...
- 深挖JDK动态代理(一)
最近在研究RPC框架,避免不了的就是在RPC调用中使用最多的则是动态代理的机制了,基于此,我们先来研究一下JDK动态代理 我们先来尝试着编写一下JDK动态代理的代码 1. 由于JDK动态代理是基于接 ...
- 编写高质量代码:改善Java程序的151个建议 --[98~105]
建议的采用顺序是List中泛型顺序依次为T.?.Object (1).List是确定的某一个类型 List表示的是List集合中的元素都为T类型,具体类型在运行期决定:List<?>表示的 ...
- Arch Linux下Visual Stdio Code在格式化C代码时报错
libtinfo.so.5: cannot open shared object file: No such file or directory Arch Linux下Visual Stdio Cod ...
- 洛谷P2824 排序
解:splay + 线段树合并,分裂. 首先有个乱搞做法是外层拿splay维护,有序区间缩成splay上一个节点.内层再开个数据结构支持合并分裂有序集合. 内层我一开始想的是splay,然后就没有复杂 ...
- 用popart构建常染色体单倍型网络(Autosomal haplotypes network construction with popart)
1)将vcf转化为plink格式,假定输入的vcf文件名为:17893893-17898893.vcf,也可以参考链接:将vcf文件转化为plink格式并且保持phasing状态 /vcftools ...
- Linux批量修改(删除)文件名某些字符(rename命令)
假设在路径C:/下存在多个类似以下的文件名 file_nall_abc1.txt file_nall_abc2.txt file_nall_abc3.txt file_nall_abc4.txt fi ...
- spring MVC 如何接收前台传入的JSON对象数组并处理
spring MVC 如何接收前台传入的JSON对象数组 主要方法: (主要用到的包是 net.sf.json 即:json-lib-2.3-jdk15.jar 完整相关jar包: commons- ...
- Day037--Python--线程的其他方法,GIL, 线程事件,队列,线程池,协程
1. 线程的一些其他方法 threading.current_thread() # 线程对象 threading.current_thread().getName() # 线程名称 threadi ...
- pytest 3.fixture介绍一 conftest.py
前言: 前面一篇pytest2 讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作,但这种是整个脚本全局生效的,如果我想实现以下场景: 用例1需要先登录,用例2不需要登录, ...