Tensorflow基础入门

MNIST 数据集入门

MNIST 数据集简介

Tensorflow基础入门_第1张图片
数字手写体识别数据集,常用来作为Deep Learning入门的基础数据集。它有60000个训练样本集和10000个测试样本集,每个样本图像的宽高为 28 * 28。此数据集是以二进制存储的,不能直接以图像格式查看。

数据集大小:~12MB

下载地址:http://yann.lecun.com/exdb/mnist/index.html

import tensorflow as tf
print(tf.__version__)

1.6.0

tensorflow加载MNIST数据集

# Import MNIST
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../../data/", one_hot=True)

# Load data
X_train = mnist.train.images
Y_train = mnist.train.labels
X_test = mnist.test.images
Y_test = mnist.test.labels

train-images-idx3-ubyte.gz:  training set images (9912422 bytes)
train-labels-idx1-ubyte.gz:  training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz:   test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz:   test set labels (4542 bytes)
Extracting ../../data/train-images-idx3-ubyte.gz
Extracting ../../data/train-labels-idx1-ubyte.gz
Extracting ../../data/t10k-images-idx3-ubyte.gz
Extracting ../../data/t10k-labels-idx1-ubyte.gz

查看并可视化MNIST数据集

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# Get the next 64 images array and labels
batch_X, batch_Y = mnist.train.next_batch(64)
print(batch_X.astype,batch_X.shape) # (64,28*28)
print(batch_Y.astype,batch_Y.shape) # (64,10[0-9哪一类])
print(batch_Y[0]) # [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
plt.imshow(np.reshape(batch_X[0], [28, 28]), cmap='gray')

 (64, 784)
 (64, 10)
[ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]

Tensorflow基础入门_第2张图片

tensorflow入门(hello world)

import tensorflow as tf
# TensorFlow 实现简单的 hello world

# 创建一个常量操作
# 这个常量操作会在默认的图中添加一个节点
#
# 构造函数返回的值表示常量op的输出。
hello = tf.constant('Hello, TensorFlow!')
# 启动 tf session
sess = tf.Session()
# 运行图
print(sess.run(hello))

b'Hello, TensorFlow!'

tensorflow入门(基本操作)

常量操作

# 常量基本操作
a = tf.constant(2)
b = tf.constant(3)
# 启动默认的图
with tf.Session() as sess:
    print ("a: %i" % sess.run(a), "b: %i" % sess.run(b))
    print ("Addition with constants: %i" % sess.run(a+b))
    print ("Multiplication with constants: %i" % sess.run(a*b))

a: 2 b: 3
Addition with constants: 5
Multiplication with constants: 6

变量操作

# 作为图输入变量的基本操作。
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
# tf 中定义的操作
add = tf.add(a, b) #加法操作
mul = tf.multiply(a, b) #乘法操作
# 启动默认的图
with tf.Session() as sess:
    # 使用变量输入运行每个操作。
    print ("Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}))
    print ("Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3}))

Addition with variables: 5
Multiplication with variables: 6

矩阵操作

# 创建一个生成1x2矩阵的常数op。
matrix1 = tf.constant([[3., 3.]])
# 创建另一个常数,生成一个2x1矩阵。
matrix2 = tf.constant([[2.],[2.]])
# tf 中定义的操作
product = tf.matmul(matrix1, matrix2) #矩阵乘法操作
with tf.Session() as sess:
    result = sess.run(product)
    print(result)

[[ 12.]]

tensorflow入门(Eager API)

from __future__ import absolute_import, division, print_function

import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
# 设置 Eager API
print("Setting Eager mode...")
tfe.enable_eager_execution()

Setting Eager mode...

Eager API 常量操作

# 定义常量 tensors
print("Define constant tensors")
a = tf.constant(2)
print("a = %i" % a)
b = tf.constant(3)
print("b = %i" % b)

Define constant tensors
a = 2
b = 3

# 执行操作不需要 tf.Session
print("Running operations, without tf.Session")
c = a + b
print("a + b = %i" % c)
d = a * b
print("a * b = %i" % d)

Running operations, without tf.Session
a + b = 5
a * b = 6

Eager API 张量操作

# 与 Numpy完全兼容
print("Mixing operations with Tensors and Numpy Arrays")

# Define constant tensors
a = tf.constant([[2., 1.],
                 [1., 0.]], dtype=tf.float32)
print("Tensor:\n a = %s" % a)
b = np.array([[3., 0.],
              [5., 1.]], dtype=np.float32)
print("NumpyArray:\n b = %s" % b)

Mixing operations with Tensors and Numpy Arrays
Tensor:
 a = tf.Tensor(
[[2. 1.]
 [1. 0.]], shape=(2, 2), dtype=float32)
NumpyArray:
 b = [[3. 0.]
 [5. 1.]]

# 在不需要 tf.Session 的情况下运行该操作
print("Running operations, without tf.Session")

c = a + b
print("a + b = %s" % c)

d = tf.matmul(a, b)
print("a * b = %s" % d)

Running operations, without tf.Session
a + b = tf.Tensor(
[[5. 1.]
 [6. 1.]], shape=(2, 2), dtype=float32)
a * b = tf.Tensor(
[[11.  1.]
 [ 3.  0.]], shape=(2, 2), dtype=float32)

# 遍历张量
print("Iterate through Tensor 'a':")
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(a[i][j])

Iterate through Tensor 'a':
tf.Tensor(2.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(0.0, shape=(), dtype=float32)

参考

TensorFlow-Examples


你可能感兴趣的:(Tensorflow基础)