tensorFlow(三)邏輯回歸
阿新 • • 發佈:2018-09-14
cor loss utf-8 ets inpu var variables batch 回歸
tensorFlow 基礎見前博客
邏輯回歸廣泛應用在各類分類,回歸任務中。本實驗介紹邏輯回歸在 TensorFlow 上的實現
理論知識回顧
邏輯回歸的主要公式羅列如下:激活函數(activation function):
損失函數(cost function):
其中
損失函數求偏導(derivative cost function):
訓練模型
-
數據準備
首先我們需要先下載MNIST的數據集。使用以下的命令進行下載:
wget https://devlab-1251520893.cos.ap-guangzhou.myqcloud.com/t10k-images-idx3-ubyte.gz wget https:
創建代碼
#-*- coding:utf-8 -*- import time import numpy as npimport tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data MNIST = input_data.read_data_sets("./", one_hot=True) learning_rate = 0.01 batch_size = 128 n_epochs = 25 X = tf.placeholder(tf.float32, [batch_size, 784]) Y = tf.placeholder(tf.float32, [batch_size, 10]) w = tf.Variable(tf.random_normal(shape=[784,10], stddev=0.01), name="weights") b = tf.Variable(tf.zeros([1, 10]), name="bias") logits = tf.matmul(X, w) + b entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits) loss = tf.reduce_mean(entropy) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) n_batches = int(MNIST.train.num_examples/batch_size) for i in range(n_epochs): for j in range(n_batches): X_batch, Y_batch = MNIST.train.next_batch(batch_size) _, loss_ = sess.run([optimizer, loss], feed_dict={ X: X_batch, Y: Y_batch}) print "Loss of epochs[{0}] batch[{1}]: {2}".format(i, j, loss_)
執行結果
Loss of epochs[0] batch[0]: 2.28968191147 Loss of epochs[0] batch[1]: 2.30224704742 Loss of epochs[0] batch[2]: 2.26435565948 Loss of epochs[0] batch[3]: 2.26956915855 Loss of epochs[0] batch[4]: 2.25983452797 Loss of epochs[0] batch[5]: 2.2572259903 ...... Loss of epochs[24] batch[420]: 0.393310219049 Loss of epochs[24] batch[421]: 0.309725940228 Loss of epochs[24] batch[422]: 0.378903746605 Loss of epochs[24] batch[423]: 0.472946226597 Loss of epochs[24] batch[424]: 0.259472459555 Loss of epochs[24] batch[425]: 0.290799200535 Loss of epochs[24] batch[426]: 0.256865829229 Loss of epochs[24] batch[427]: 0.250789999962 Loss of epochs[24] batch[428]: 0.328135550022View Code
測試模型
#-*- coding:utf-8 -*- import time import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data MNIST = input_data.read_data_sets("./", one_hot=True) learning_rate = 0.01 batch_size = 128 n_epochs = 25 X = tf.placeholder(tf.float32, [batch_size, 784]) Y = tf.placeholder(tf.float32, [batch_size, 10]) w = tf.Variable(tf.random_normal(shape=[784,10], stddev=0.01), name="weights") b = tf.Variable(tf.zeros([1, 10]), name="bias") logits = tf.matmul(X, w) + b entropy = tf.nn.softmax_cross_entropy_with_logits(labels=Y, logits=logits) loss = tf.reduce_mean(entropy) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) n_batches = int(MNIST.train.num_examples/batch_size) for i in range(n_epochs): for j in range(n_batches): X_batch, Y_batch = MNIST.train.next_batch(batch_size) _, loss_ = sess.run([optimizer, loss], feed_dict={ X: X_batch, Y: Y_batch}) print "Loss of epochs[{0}] batch[{1}]: {2}".format(i, j, loss_) n_batches = int(MNIST.test.num_examples/batch_size) total_correct_preds = 0 for i in range(n_batches): X_batch, Y_batch = MNIST.test.next_batch(batch_size) preds = tf.nn.softmax(tf.matmul(X_batch, w) + b) correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(Y_batch, 1)) accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32)) total_correct_preds += sess.run(accuracy) print "Accuracy {0}".format(total_correct_preds/MNIST.test.num_examples)
執行結果
Accuracy 0.9108View Code
tensorFlow(三)邏輯回歸