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的更多相关文章

  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. Hyper-V

    docker与vm同时安装,会导致虚拟机无法启动及网络不通等问题,解决方法分为两步. 1.在网络连接中禁用vEthernet连接. 2.控制面板>程序>程序和功能中 >启用或关闭wi ...

  2. CentOS安装Python3.7

    vscode设置python3.7调试环境:https://www.cnblogs.com/dotnetcrazy/p/9095793.html 先下载一下压缩包(FTP传也一样):weget htt ...

  3. Markdown基础(内含:锚点使用,使用HTML,新页面跳转,目录生成)

    Github样式显示参考:点我 之前说过用word写文章,这次说说Markdown写文章(推荐) 逆天推荐使用VSCode编写 装这个插件写作更方便: 内含:锚点使用,使用HTML,新页面跳转,目录生 ...

  4. javascript之判断专题

    javascript有数组,对象,函数,字符串,布尔,还有Symbol,set,map,weakset,weakmap. 判断这些东西也是有很多坑,像原生的typeof,instanceOf有一些bu ...

  5. 什么是Zookeeper?

    https://mp.weixin.qq.com/s?__biz=MzAxOTc0NzExNg==&mid=2665514106&idx=1&sn=e54aae8cf68f5a ...

  6. A1043. Is It a Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  7. gradle的安装配置成功标志

    gradle主要位于AndroidStudio中 看我的目录 在环境变量里添加用户变量 GRADLE_HOME 然后在环境变量 path 中增加 %GRADLE_HOME%\bin;,如图所示 测试配 ...

  8. Can not issue data manipulation statements with executeQuery()错误解决

    转: Can not issue data manipulation statements with executeQuery()错误解决 2012年03月27日 15:47:52 katalya 阅 ...

  9. Python 的内置函数

    函数 功能 示例 示例结果 abs(x) 返回x的绝对值 abs(-2) 2 chr(x) 返回整数x所代表的字符 chr(65) A divmod(x,y) 返回x除以y的商和余数的元组 divmo ...

  10. Explicit Constructors(显式构造函数)

    按照默认规定,只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象,如下面所示:class String {String ( const char* p ); // ...