1. 程式人生 > >tensorflow--logistic regression

tensorflow--logistic regression

batch zeros equal 二分 val radi 計算 example png

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