TensorFlow实现逻辑回归分类器
来源:互联网 发布:ubuntu怎么上传文件 编辑:程序博客网 时间:2024/04/27 09:48
TensorFlow是谷歌推出的深度学习框架,之前也看过官网的技术手册,但想着“纸上得来终觉浅”,于是决定自己编程实现一下,并把实现过程中遇到的问题记录下来,以便自己随时翻看。本文是关于逻辑回归分类器的实现,相关的机器学习理论比较简单,这里就不再赘述。
代码:
import tensorflow as tfimport numpy as npfrom tensorflow.examples.tutorials.mnist import input_data def train(batch_size=100, lr=0.5, iter_num=1000): mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #类对象(train, validation, test) x = tf.placeholder(tf.float32, shape=(None, 28*28)) y = tf.placeholder(tf.float32, shape=(None, 10)) w=tf.Variable(tf.truncated_normal(shape=(28*28, 10), stddev=0.5), name='Weight') b=tf.Variable(tf.zeros(shape=[10]), name='Bias') variable_initiation = tf.initialize_all_variables() probability = tf.nn.softmax(tf.matmul(x, w) + b) Loss = -tf.reduce_mean(y*tf.log(probability)) trainer= tf.train.GradientDescentOptimizer(learning_rate=lr).minimize(loss=Loss) sess = tf.Session() sess.run(variable_initiation) for iter in range(iter_num): train_dataset = mnist.train.next_batch(batch_size=batch_size) trainer.run(session=sess, feed_dict={x:train_dataset[0], y:train_dataset[1]})() '''评估过程开始''' comparision = tf.equal(tf.argmax(probability, dimension=1), tf.argmax(y, dimension=1)) accuracy = tf.reduce_mean(tf.cast(comparision, dtype=tf.float32)) accuracy = sess.run(fetches=accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels}) print('The accuracy is %.3f' % accuracy) sess.close() if __name__ == '__main__': train()
注意点:(1)获得变量的输出值有两种方法,一是使用符号变量的eval()方法返回结果,二是使用Session的run()方法进行返回;
(2)对于y=tensorflow.reduce_mean(x)方法,返回值y和输入参数x具有相同的数据类型,故若x表示{0, 1}的int32类型时,返回值为0;
0 0
- TensorFlow实现逻辑回归分类器
- Tensorflow实现逻辑分类器
- 代码,逻辑回归(logistic_regression)实现mnist分类(TensorFlow实现)
- Tensorflow实现逻辑回归
- tensorflow入门(4)逻辑回归分类器
- 逻辑回归之tensorflow实现
- Tensorflow实现逻辑回归模型
- 逻辑回归分类器
- 逻辑回归分类器
- Tensorflow学习之逻辑回归的实现
- Logistic Regression 逻辑回归,Tensorflow源码实现
- Logistic Regression逻辑回归分类器weka实现
- 用梯度下降实现逻辑回归分类器
- Scala实现逻辑回归分类,Titanic
- TensorFlow学习笔记(三):TensorFlow实现逻辑回归模型
- tensorflow逻辑回归
- tensorflow之逻辑回归
- tensorflow逻辑回归例子
- 手把手图文教你从Eclipse项目迁移Android Studio
- Codeforces 141C Queue【思维】
- hdu5478 Can you find it+快速幂
- 留念1
- java(11)------标识符
- TensorFlow实现逻辑回归分类器
- 我也聊聊串口通信协议:用户层通信协议的编制
- 纠正waiting for network configuration网络环境报错问题
- 10月英语总结
- 让TabBar上的图片居中
- Linux终端打印彩色输出
- Ubuntu 开启WiFi
- 进程间通信之系统V共享内存
- MySQL数据库十大优化技巧