一看就懂的Tensorflow实战(Tensorflow入门)

2020-07-29 11:18:52 浏览数 (1)

MNIST 数据集入门

MNIST 数据集简介

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

数据集大小:~12MB

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

导入tensorflow(v1.6.0版本)
代码语言:javascript复制
import tensorflow as tf
print(tf.__version__)

代码语言:javascript复制
1.6.0
tensorflow加载MNIST数据集
代码语言:javascript复制
# 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

代码语言:javascript复制
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数据集
代码语言:javascript复制
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')

代码语言:javascript复制
<built-in method astype of numpy.ndarray object at 0x0000016EEF769080> (64, 784)
<built-in method astype of numpy.ndarray object at 0x0000016EEF772620> (64, 10)
[ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]
<matplotlib.image.AxesImage at 0x16eef82a630>

png

tensorflow入门(hello world)

代码语言:javascript复制
import tensorflow as tf
代码语言:javascript复制
# TensorFlow 实现简单的 hello world

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

代码语言:javascript复制
b'Hello, TensorFlow!'

tensorflow入门(基本操作)

常量操作
代码语言:javascript复制
# 常量基本操作
a = tf.constant(2)
b = tf.constant(3)
代码语言:javascript复制
# 启动默认的图
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))

代码语言:javascript复制
a: 2 b: 3
Addition with constants: 5
Multiplication with constants: 6
变量操作
代码语言:javascript复制
# 作为图输入变量的基本操作。
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
代码语言:javascript复制
# tf 中定义的操作
add = tf.add(a, b) #加法操作
mul = tf.multiply(a, b) #乘法操作
代码语言:javascript复制
# 启动默认的图
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}))

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

代码语言:javascript复制
[[ 12.]]

tensorflow入门(Eager API)

代码语言:javascript复制
from __future__ import absolute_import, division, print_function

import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
代码语言:javascript复制
# 设置 Eager API
print("Setting Eager mode...")
tfe.enable_eager_execution()

代码语言:javascript复制
Setting Eager mode...
Eager API 常量操作
代码语言:javascript复制
# 定义常量 tensors
print("Define constant tensors")
a = tf.constant(2)
print("a = %i" % a)
b = tf.constant(3)
print("b = %i" % b)

代码语言:javascript复制
Define constant tensors
a = 2
b = 3

代码语言:javascript复制
# 执行操作不需要 tf.Session
print("Running operations, without tf.Session")
c = a   b
print("a   b = %i" % c)
d = a * b
print("a * b = %i" % d)

代码语言:javascript复制
Running operations, without tf.Session
a   b = 5
a * b = 6
Eager API 张量操作
代码语言:javascript复制
# 与 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)

代码语言:javascript复制
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.]]

代码语言:javascript复制
# 在不需要 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)

代码语言:javascript复制
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)

代码语言:javascript复制
# 遍历张量
print("Iterate through Tensor 'a':")
for i in range(a.shape[0]):
    for j in range(a.shape[1]):
        print(a[i][j])

代码语言:javascript复制
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]https://github.com/aymericdamien/TensorFlow-Examples

0 人点赞