Tensorflow学习笔记(四)

下载数据集

>>> from tensorflow.examples.tutorials.mnist import input_data
>>> mnist = input_data.read_data_sets('./data/' ,one_hot=True)

显示

显示数据集样本数

>>> print "train data size:" ,mnist.train.num_examples
train data size: 55000
>>> print "validation data size:" ,mnist.validation.num_examples
validation data size: 5000
>>> print "test data size:" ,mnist.test.num_examples
test data size: 10000

显示标签

>>> mnist.train.labels[0]
array([0., 0., 0., 0., 0., 0., 0., 1., 0., 0.])

显示像素值

mnist.train.images[0]

数据传入神经网络

>>> BATCH_SZIE = 200
>>> xs , ys = mnist.train.next_batch(BATCH_SZIE)
>>> print "xs shape:" , xs.shape
xs shape: (200, 784)
>>> print "ys shape:" , ys.shape
ys shape: (200, 10)

手写数字识别常用函数

tf.get_collection(" ")

从集合中取出全部变量来生成一个列表

tf.add()

>>> x = tf.constant([[2,1],[2,3]])
>>> y = tf.constant([[0,-1],[4,10]])
>>> z = tf.add(x,y)
>>> with tf.Session() as sess:
...     print sess.run(z)
... 
[[ 2  0]
 [ 6 13]]

tf.cast()

>>> A = tf.constant([[1,2,3],[2,3,4]])
>>> print A.dtype

>>> b = tf.cast(A, tf.float32)
>>> print b.dtype

tf.equal()

>>> A = [[1,3,5,7,9]]
>>> B = [[1,5,5,3,9]]
>>> with tf.Session() as sess:
...     print sess.run(tf.equal(A,B))
... 
[[ True False  True False  True]]

tf.reduce_mean(x,axis)

>>> x = [[1,1],[2,2]]
>>> x = tf.cast(x,tf.float32)
>>> with tf.Session() as sess:
...     print sess.run(tf.reduce_mean(x))
...     print sess.run(tf.reduce_mean(x,0))
...     print sess.run(tf.reduce_mean(x,1))
... 
1.5
[1.5 1.5]
[1. 2.]

os.path.join()

把字符串按路径命名规则拼接

字符串.split()

'./model/mnist_model-1001'.split('/')[-1].split('-')[-1]
两次拆分,第一次根据/拆分,取-1项也就是最后一项,然后再根据-取最后一项,结果为1001

你可能感兴趣的:(Tensorflow学习笔记(四))