TensorFlow入門 MNIST深入

2022-06-24 23:06:15 字數 3371 閱讀 6430

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

#start tensorflow interactivesession

6import

tensorflow as tf

7 sess =tf.interactivesession()89

#weight initilization

10def

weight_variable(shape):

11 initial = tf.truncated_normal(shape, stddev=0.1)

12return

tf.variable(initial)

1314

defbias_variable(shape):

15 initial = tf.constant(0.1, shape=shape)

16return

tf.variable(initial)

1718

#convolution

19def

conv2d(x, w):

20return tf.nn.conv2d(x, w, strides=[1,1,1,1], padding='

same')

2122

#pooling

23def

max_pool_2x2(x):

24return tf.nn.max_pool(x, ksize=[1,2,2,1],strides=[1,2,2,1], padding='

same')

2526

#create the model27#

placeholder

28 x = tf.placeholder("

float

",[none, 784])

29 y_ = tf.placeholder("

float

", [none, 10])

3031

#variable

32 w = tf.variable(tf.zeros([784,10]))

33 b = tf.variable(tf.zeros([10]))

3435 y = tf.nn.softmax(tf.matmul(x,w) +b)

3637

#first convolutional layer

38 w_conv1 = weight_variable([5,5,1,32])

39 b_conv1 = bias_variable([32])

4041 x_image = tf.reshape(x,[-1,28,28,1])

4243 h_conv1 =tf.nn.relu(conv2d(x_image,w_conv1) +b_conv1)

44 h_pool1 =max_pool_2x2(h_conv1)

4546

#second convolutional layer

47 w_conv2 = weight_variable([5,5,32,64])

48 b_conv2 = bias_variable([64])

4950 h_conv2 =tf.nn.relu(conv2d(h_pool1, w_conv2) +b_conv2)

51 h_pool2 =max_pool_2x2(h_conv2)

5253

#densely connected layer

54 w_fc1 = weight_variable([7*7*64, 1024])

55 b_fc1 = bias_variable([1024])

5657 h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64])

58 h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) +b_fc1)

5960

#dropout

61 keep_prob = tf.placeholder("

float")

62 h_fc1_drop =tf.nn.dropout(h_fc1, keep_prob)

6364

#readout layer

65 w_fc2 = weight_variable([1024,10])

66 b_fc2 = bias_variable([10])

6768 y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2) +b_fc2)

6970

#train and evaluate the model

71 cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))72#

train_step = tf.train.adagradoptimizer(1e-4).minimize(cross_entropy)

73 train_step = tf.train.gradientdescentoptimizer(1e-4).minimize(cross_entropy)

74 correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))

75 accuracy = tf.reduce_mean(tf.cast(correct_prediction, "

float"))

76sess.run(tf.initialize_all_variables())

77for i in range(5000):

78 batch = mnist.train.next_batch(50)

79if i%100 ==0:

80 train_accuracy = accuracy.eval(feed_dict=)

81print

"step %d, train accuracy %g

" %(i,train_accuracy)

82 train_step.run(feed_dict=)

8384

print

"test accuracy %g

" % accuracy.eval(fedd_dict=)

同樣是極客學院的課程,其實也是翻譯的國外的robot-ai部落格上的內容,但是這個部落格,現在打不開了,可能是牆的問題?沒有太深究。

tensorflow教程學習三深入MNIST

載入資料 from tensorflow.examples.tutorials.mnist import input data mnist input data.read data sets mnist data one hot true 我們使用interactivesession類可以更加靈活地...

Tensorflow 入門記錄

tensor為張量,flow為流圖。tensorflow內含有很多寫好的工具,如梯度下降演算法,卷積操作等。在使用tensorflow時,先導入包import tensorflow as tf,在進行定義tensorflow變數時,使用tf.variable 引數 有趣的是乙個叫做佔位符的工具,tf...

tensorflow入門例子

import tensorflow as tf import numpy as np 使用 numpy 生成假資料 phony data 總共 100 個點.100,2 x data np.float32 np.random.rand 100,2 隨機輸入 y data np.dot x data,...