tensorflow--logistic regression
生活随笔
收集整理的這篇文章主要介紹了
tensorflow--logistic regression
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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 用來(lái)存儲(chǔ)輸入,輸入圖像x構(gòu)成一個(gè)2維的浮點(diǎn)張量,[None,784]是簡(jiǎn)單的平鋪圖,'None'代表處理的批次大小,是任意大小 x=tf.placeholder(tf.float32,[None,784]) y=tf.placeholder(tf.float32,[None,10])# variables 為模型定義權(quán)重和偏置 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函數(shù)
# reduce_sum 對(duì)所有類別求和,reduce_mean 對(duì)和取平均 cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 往graph中添加新的操作,計(jì)算梯度,計(jì)算參數(shù)的更新 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})
# placeholder x,y 用來(lái)存儲(chǔ)輸入,輸入圖像x構(gòu)成一個(gè)2維的浮點(diǎn)張量,[None,784]是簡(jiǎn)單的平鋪圖,'None'代表處理的批次大小,是任意大小 x=tf.placeholder(tf.float32,[None,784]) y=tf.placeholder(tf.float32,[None,10])# variables 為模型定義權(quán)重和偏置 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函數(shù)
# reduce_sum 對(duì)所有類別求和,reduce_mean 對(duì)和取平均 cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))
# 往graph中添加新的操作,計(jì)算梯度,計(jì)算參數(shù)的更新 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 函數(shù):
二分類問(wèn)題
?
softmax 函數(shù):
將k維向量壓縮成另一個(gè)k維向量,進(jìn)行多分類,logistic 是softmax的一個(gè)例外
?
轉(zhuǎn)載于:https://www.cnblogs.com/fanhaha/p/7617497.html
總結(jié)
以上是生活随笔為你收集整理的tensorflow--logistic regression的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 我的新房是坐东南向西北,请问炉灶的坐向?
- 下一篇: 水题讲解:瑞士轮