Tensorflow-Logistic regression
来源:互联网 发布:大乐透出号绝密算法 编辑:程序博客网 时间:2024/06/15 04:33
1、sigmoid-不使用relu
#!/usr/bin/python3# -*- coding:utf-8 -*-import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt"""Logistic regression"""# 数据x1 = np.random.normal(-4, 2, 1000)[:,np.newaxis] # 1000x1x2 = np.random.normal(4,2 , 1000)[:,np.newaxis]train_x = np.vstack((x1, x2)) # 2000x1train_y = np.asarray([0.] * len(x1) + [1.] * len(x2))[:,np.newaxis] # 2000x1plt.scatter(train_x, train_y)# plt.show()x=tf.placeholder(tf.float32,[None,1],'x')y_=tf.placeholder(tf.float32,[None,1],'y_')with tf.variable_scope('wb'): w=tf.get_variable('w',(1,1),dtype=tf.float32,initializer=tf.random_uniform_initializer) b= tf.Variable(tf.zeros([1, 1]) + 0.1)with tf.variable_scope('wb2') as scope: # scope.reuse_variables() w2=tf.get_variable('w2',(10,1),dtype=tf.float32,initializer=tf.random_uniform_initializer) b2= tf.Variable(tf.zeros([1, 1]) + 0.1)y=tf.nn.sigmoid(tf.add(tf.matmul(x,w),b))# y=tf.nn.relu(tf.add(tf.matmul(x,w),b))# y=tf.nn.sigmoid(tf.add(tf.matmul(y,w2),b2))# loss function# loss=tf.reduce_mean(tf.reduce_sum(tf.square(y-y_),reduction_indices=[1]))loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y))train_op=tf.train.GradientDescentOptimizer(0.1).minimize(loss)sess=tf.InteractiveSession(graph=tf.get_default_graph())tf.global_variables_initializer().run()for step in range(1000): sess.run(train_op,feed_dict={x:train_x,y_:train_y})all_xs = np.linspace(-10, 10, 100)[:,np.newaxis]prdiction_value = sess.run(y, feed_dict={x: all_xs})lines = plt.plot(all_xs, prdiction_value, 'r-', lw=5)plt.show()sess.close()
结果:
2、sigmoid-使用relu
#!/usr/bin/python3# -*- coding:utf-8 -*-import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt"""Logistic regression"""# 数据x1 = np.random.normal(-4, 2, 1000)[:,np.newaxis] # 1000x1x2 = np.random.normal(4,2 , 1000)[:,np.newaxis]train_x = np.vstack((x1, x2)) # 2000x1train_y = np.asarray([0.] * len(x1) + [1.] * len(x2))[:,np.newaxis] # 2000x1plt.scatter(train_x, train_y)# plt.show()x=tf.placeholder(tf.float32,[None,1],'x')y_=tf.placeholder(tf.float32,[None,1],'y_')with tf.variable_scope('wb'): w=tf.get_variable('w',(1,10),dtype=tf.float32,initializer=tf.random_uniform_initializer) b= tf.Variable(tf.zeros([1, 10]) + 0.1)with tf.variable_scope('wb2') as scope: # scope.reuse_variables() w2=tf.get_variable('w2',(10,1),dtype=tf.float32,initializer=tf.random_uniform_initializer) b2= tf.Variable(tf.zeros([1, 1]) + 0.1)# y=tf.nn.sigmoid(tf.add(tf.matmul(x,w),b))y=tf.nn.relu(tf.add(tf.matmul(x,w),b))y=tf.nn.sigmoid(tf.add(tf.matmul(y,w2),b2))# loss function# loss=tf.reduce_mean(tf.reduce_sum(tf.square(y-y_),reduction_indices=[1]))loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y))train_op=tf.train.GradientDescentOptimizer(0.1).minimize(loss)sess=tf.InteractiveSession(graph=tf.get_default_graph())tf.global_variables_initializer().run()for step in range(1000): sess.run(train_op,feed_dict={x:train_x,y_:train_y})all_xs = np.linspace(-10, 10, 100)[:,np.newaxis]prdiction_value = sess.run(y, feed_dict={x: all_xs})lines = plt.plot(all_xs, prdiction_value, 'r-', lw=5)plt.show()sess.close()
结果:
3、softmax(多分类)
sigmoid只能针对二分类问题,对于多分类可以使用softmax
#!/usr/bin/python3# -*- coding:utf-8 -*-import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt"""Logistic regression"""from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# mnist图像大小是28x28 分成0~9 共10类x=tf.placeholder(tf.float32,[None,28*28*1])y_=tf.placeholder(tf.float32,[None,10])with tf.variable_scope('wb'): w=tf.get_variable('w',[28*28,10],initializer=tf.random_uniform_initializer)*0.001 b=tf.Variable(tf.zeros([10])+0.1,dtype=tf.float32)y=tf.nn.softmax(tf.add(tf.matmul(x,w),b))loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y))train_op=tf.train.AdamOptimizer(0.5).minimize(loss)correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))# Calculate accuracyaccuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))sess=tf.InteractiveSession(graph=tf.get_default_graph())tf.global_variables_initializer().run()for step in range(1000): batch_xs, batch_ys = mnist.train.next_batch(128) train_op.run({x:batch_xs,y_:batch_ys}) if step % 100==0: print("step",step,'acc',accuracy.eval({x:batch_xs,y_:batch_ys}),'loss',loss.eval({x:batch_xs,y_:batch_ys}))# test accprint('test acc',accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))sess.close()
结果:
说明:
没有使用隐藏层,所以精度并不是很高,
接下来会使用DNN、CNN以及RNN来提升其精度!
阅读全文
0 0
- Tensorflow: Logistic Regression Mnist
- TensorFlow实现 Logistic Regression
- Tensorflow-Logistic regression
- 【TensorFlow】TensorFlow 的 Logistic Regression
- Logistic Regression 逻辑回归,Tensorflow源码实现
- TensorFlow简单实例(二):logistic regression
- Lecture note 3: Linear and Logistic Regression in TensorFlow
- logistic regression
- logistic regression
- logistic regression
- Logistic Regression
- Logistic Regression
- logistic regression
- Logistic Regression
- Logistic Regression
- logistic regression
- Logistic regression
- Logistic Regression
- 你真的适合学习JAVA开发吗?
- 1294:简单统计
- android系统使用Camera2 应用
- Linear Ensembles of Word Embedding Models
- 1310:最近平方数
- Tensorflow-Logistic regression
- 莫小安 CentOS7使用firewalld打开关闭防火墙与端口
- Calico 的网络结构是什么?- 每天5分钟玩转 Docker 容器技术(68)
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- HANA删除重复记录的方法
- 1503:互质个数
- 题目44-子串和
- 计蒜客-求出现次数最多的字符
- NMF 非负矩阵分解 -- 原理与应用