使用mnist 資料集,
用softmax回歸**標籤
使用交叉熵損失函式計算損失值
使用梯度下降法優化引數
#-*- coding: utf-8 -*-
import
tensorflow as tf
#import
tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("
mnist_data/
", one_hot=true)
x = tf.placeholder(tf.float32, [none, 784])
y_= tf.placeholder(tf.float32, [none, 10]) #
實際標籤
w = tf.variable(tf.zeros([784, 10]))
b = tf.variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, w) +b) #
**標籤
loss = -tf.reduce_sum(y_*tf.log(y)) #
計算損失值
train_step = tf.train.gradientdescentoptimizer(0.01).minimize(loss)#
梯度下降優化引數
init =tf.global_variables_initializer()
with tf.session() as sess:
sess.run(init)
for i in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100) #
傳入訓練集
sess.run(train_step,feed_dict=)#
訓練correct = tf.equal(tf.argmax(y,1), tf.argmax(y_ ,1))
accuracy = tf.reduce_mean(tf.cast(correct, "
float"))
print (sess.run(accuracy, feed_dict=)) #
傳入測試集
posted on
2017-09-06 17:04
...)
編輯收藏
MNIST機器學習入門
路漫漫其修遠兮 環境 python tensorflow from future import absolute import from future import division from future import print function pylint disable unused im...
MNIST機器學習入門 程式與操作
1.ubuntu terminal 2.建立乙個code目錄 mkdir tensorflow code 3.進入tensorflow code cd tensorflow code 4.建立tensorflow mnist 檔案test mnist.py touch test mnist.py 5...
TensorFlow入門 MNIST深入
1 load mnist data 2import tensorflow.examples.tutorials.mnist.input data as input data 3 mnist input data.read data sets mnist data one hot true 45 st...