tensorflow学习笔记
Posted 佟学强
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tensorflow学习笔记相关的知识,希望对你有一定的参考价值。
import tensorflow as tf
import numpy as np
import math
import tensorflow.examples.tutorials.mnist as mn
sess = tf.InteractiveSession()
mnist = mn.input_data.read_data_sets("E:\\Python35\\Lib\\site-packages\\tensorflow\\examples\\tutorials\\mnist\\MNIST_data",one_hot=True)
x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.global_variables_initializer())
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
‘‘‘train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)‘‘‘
bestModel = None
bestPredict = 0.0
bestIter = 0
bestRate = 0.0
bestSample = 0
iters = [1000,1200,1400]
rates = [0.01,0.02]
samples = [100,150,200]
for iter in iters:
for rate in rates:
for sample in samples:
train_step = tf.train.GradientDescentOptimizer(rate).minimize(cross_entropy)
batch = mnist.train.next_batch(sample)
model = train_step.run(feed_dict = {x:batch[0],y_:batch[1]})
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
predict = accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels})
if predict > bestPredict:
bestPredict = predict
bestModel = model
bestIter = iter
bestRate = rate
bestSample = sample
print("bestRate:",bestRate,"bestIter:",bestIter,"bestSample:",bestSample)
‘‘‘
for i in range(1000):
batch = mnist.train.next_batch(200)
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
‘‘‘
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
以上是关于tensorflow学习笔记的主要内容,如果未能解决你的问题,请参考以下文章
《TensorFlow实战Google深度学习框架(第二版)》学习笔记及书评