简单粗暴Tensorflow2 0
简单粗暴Tensorflow2 0
0.3 beta
Xihan Li
2018 08 28
Contents
1 2
2 TensorFlow 4
2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 TensorFlow 8
3.1 TensorFlow 1+1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 TensorFlow 14
4.1 Model Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.2 MLP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.3 CNN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.4 RNN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.5 DRL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.6 * . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.7 Graph Execution *. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
5 TensorFlow 29
5.1 Checkpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.2 TensorBoard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.3 GPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
6 TensorFlow 36
6.1 TensorFlow 1+1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
i
Bibliography 42
ii
TensorFlow, 0.3 beta
PDF https://www.tensorflowers.cn/t/6230
GitHub: https://github.com/snowkylin/TensorFlow-cn
This handbook is a concise introduction to TensorFlow based on TensorFlow s Eager Execution mode,
trying to help developers get started with TensorFlow quickly with some basic machine learning and Python
knowledge.
Friendly reminder: If you find something difficult to understand in reading, please check if you have a clear
understanding of the Prerequisites part of each chapter.
Q&A area - TensorFlow Chinese community A Concise Handbook of TensorFlow forum: https://www.
tensorflowers.cn/b/48 (If you have any questions about this tutorial, please ask in this forum of the Tensor-
Flow Chinese community)
GitHub: https://github.com/snowkylin/TensorFlow-cn
Contents 1
CHAPTER 1
The easiest way to get started with TensorFlow is using Eager Execution.
— https://www.tensorflow.org/get_started/
PyTorch
TensorFlow TensorFlow
Graph Execution
TensorFlow Eager Execution
TensorFlow
TensorFlow
TensorFlow
TensorFlow
TensorFlow https://tensorflow.google.cn/tutorials
TensorFlow
/ TensorFlow
2
TensorFlow, 0.3 beta
• TensorFlow TensorFlow
• TensorFlow
tf.keras.Model tf.keras.layers.Layer
Google TensorFlow
Luke Cheng Rui
Li, Pryce Mu TensorFlow TensorFlow
Tiezhen Wang
https://github.com/snowkylin/TensorFlow-cn/issues
Xihan Li
2018 8
3
CHAPTER 2
TensorFlow
2.1
GPU
Windows
2.2
4
TensorFlow, 0.3 beta
2.2.1
TensorFlow
• NVIDIA CUDA
cuDNN CUDA
• cuDNN CUDA
2.2.2
Anaconda Windows
1. tensorflow conda
2.
1 GPU GPU CPU GPU
1-2 GPU NVIDIA GeForce GTX 1080 Ti NVIDIA GeForce TITAN
GPU
TensorFlow CPU TensorFlow TensorFlow
GPU
2.2. 5
TensorFlow, 0.3 beta
activate tensorflow
3. pip TensorFlow
CPU
GPU
TensorFlow Nightly
TensorFlow 1.8 Eager Execution Nightly
pip install tf-nightly CPU pip install
tf-nightly-gpu GPU GPU
CUDA cuDNN CUDA cuDNN
pip TensorFlow
2.3
import tensorflow as tf
tf.enable_eager_execution()
print(C)
tf.Tensor(
[[19 22]
[43 50]], shape=(2, 2), dtype=int32)
TensorFlow TensorFlow
2.3. 6
TensorFlow, 0.3 beta
2.3. 7
CHAPTER 3
TensorFlow
TensorFlow
• Python import
• Python With
• f (x, y) = x2 + xy + y 2 , ∂f ∂f
∂x =?, ∂y =?
8
TensorFlow, 0.3 beta
import tensorflow as tf
tf.enable_eager_execution()
a = tf.constant(1)
b = tf.constant(1)
c = tf.add(a, b) # c = a + b
print(c)
print(C)
TensorFlow
tf.GradientTape() y(x) = x2 x=3
import tensorflow as tf
tf.enable_eager_execution()
y = tf.square(x)
y_grad = tape.gradient(y, x) # y x
print([y.numpy(), y_grad.numpy()])
x 3 Variable tf.get_variable()
TensorFlow
tf.GradientTape()
[ ] [ ] L(w, b) = ∥Xw +b−y∥ 2
w = (1, 2)T , b = 1
1 2 1
w, b X= ,y =
3 4 2
[62.5, array([[35.],
[50.]], dtype=float32), array([15.], dtype=float32)]
tf.square() tf.reduce_sum()
axis
TensorFlow API
2
tf.reshape() tf.concat() TensorFlow API
TensorFlow
3.2
2013 -2017
y = ax + b a b
import numpy as np
3
a b
f (x)
• x0 k=0
– f (x) ∇f (xk )
– k ←k+1
∑n 2
mina,b L(a, b) = i=1 (axi +b−yi )
3.2.1 NumPy
TensorFlow
NumPy NumPy
3.2. 11
TensorFlow, 0.3 beta
4
a b a
b
a, b = 0, 0
num_epoch = 10000
learning_rate = 1e-3
for e in range(num_epoch):
#
y_pred = a * X + b
grad_a, grad_b = (y_pred - y).dot(X), (y_pred - y).sum()
#
a, b = a - learning_rate * grad_a, b - learning_rate * grad_b
print(a, b)
•
Adam Adagrad
TensorFlow
3.2.2 TensorFlow
5
TensorFlow Eager Execution NumPy
GPU
TensorFlow NumPy TensorFlow
• tape.gradient(ys, xs)
• optimizer.apply_gradients(grads_and_vars)
X = tf.constant(X)
y = tf.constant(y)
4
∑5 ∑5
L(x) = 1
i=1 (axi + b − yi )2 a b ∂L
= i=1 (axi + b − y)xi
∑5 2 ∂a
i=1 (axi + b − y)
∂L
∂b
=
5 Eager Execution Graph Execution TensorFlow 2018 3 1.8
3.2. 12
TensorFlow, 0.3 beta
num_epoch = 10000
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
for e in range(num_epoch):
# tf.GradientTape()
with tf.GradientTape() as tape:
y_pred = a * X + b
loss = 0.5 * tf.reduce_sum(tf.square(y_pred - y))
# TensorFlow
grads = tape.gradient(loss, variables)
# TensorFlow
optimizer.apply_gradients(grads_and_vars=zip(grads, variables))
tf.train.
GradientDescentOptimizer(learning_rate=1e-3) Optimizer
1e-3
apply_gradients()
optimizer.apply_gradients() grads_and_vars
variables grads
Python List
[(grad_w, w), (grad_b, b)] grads = tape.gradient(loss, variables)
tape loss variables = [w, b] grads = [grad_w, grad_b]
Python zip() grads = [grad_w, grad_b] vars = [w, b]
y_pred = tf.matmul(X, w) + b
y_pred = model(X)
3.2. 13
CHAPTER 4
TensorFlow
TensorFlow
y_pred
= model(X) __init__()
1
call(input)
class MyModel(tf.keras.Model):
def __init__(self):
super().__init__() # Python 2 super(MyModel, self).__init__()
# call
14
TensorFlow, 0.3 beta
#
return output
Layer
y_pred = tf.matmul(X, w) + b
import tensorflow as tf
tf.enable_eager_execution()
class Linear(tf.keras.Model):
def __init__(self):
super().__init__()
self.dense = tf.keras.layers.Dense(units=1, kernel_initializer=tf.zeros_initializer(),
bias_initializer=tf.zeros_initializer())
#
model = Linear()
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
for i in range(100):
with tf.GradientTape() as tape:
y_pred = model(X) #
loss = tf.reduce_mean(tf.square(y_pred - y))
grads = tape.gradient(loss, model.variables)
optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
print(model.variables)
w b y_pred = tf.matmul(X, w) + b
tf.keras.layers.Dense call
output = activation(tf.matmul(input, kernel) + bias) +
4.2 MLP
4.1: MNIST
DataLoader MNIST
class DataLoader():
def __init__(self):
mnist = tf.contrib.learn.datasets.load_dataset("mnist")
self.train_data = mnist.train.images # np.array [55000,␣
,→ 784]
self.train_labels = np.asarray(mnist.train.labels, dtype=np.int32) # np.array [55000] of␣
,→ int32
self.eval_data = mnist.test.images # np.array [10000,␣
,→ 784]
self.eval_labels = np.asarray(mnist.test.labels, dtype=np.int32) # np.array [10000] of␣
,→ int32
ReLU activation=tf.nn.relu
1×784 10 0
9 predict
class MLP(tf.keras.Model):
def __init__(self):
super().__init__()
4.2. MLP 16
TensorFlow, 0.3 beta
num_batches = 1000
batch_size = 50
learning_rate = 0.001
model = MLP()
data_loader = DataLoader()
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
• DataLoader
• loss
4.2. MLP 17
TensorFlow, 0.3 beta
num_eval_samples = np.shape(data_loader.eval_labels)[0]
y_pred = model.predict(data_loader.eval_data).numpy()
print("test accuracy: %f" % (sum(y_pred == data_loader.eval_labels) / num_eval_samples))
95%
4.3 CNN
MLP
4.2: CNN
class CNN(tf.keras.Model):
def __init__(self):
super().__init__()
self.conv1 = tf.keras.layers.Conv2D(
filters=32, #
kernel_size=[5, 5], #
padding="same", # padding
activation=tf.nn.relu #
)
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2)
self.conv2 = tf.keras.layers.Conv2D(
filters=64,
4.3. CNN 18
TensorFlow, 0.3 beta
kernel_size=[5, 5],
padding="same",
activation=tf.nn.relu
)
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2)
self.flatten = tf.keras.layers.Reshape(target_shape=(7 * 7 * 64,))
self.dense1 = tf.keras.layers.Dense(units=1024, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(units=10)
Dropout
4.4 RNN
• RNN [Graves2013]
4.4. RNN 19
TensorFlow, 0.3 beta
2
RNN
I am a studen
13 13
t num_batch
seq_length [num_batch, seq_length]
num_chars [num_batch,
num_chars]
DataLoader
class DataLoader():
def __init__(self):
path = tf.keras.utils.get_file('nietzsche.txt',
origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')
with open(path, encoding='utf-8') as f:
self.raw_text = f.read().lower()
self.chars = sorted(list(set(self.raw_text)))
self.char_indices = dict((c, i) for i, c in enumerate(self.chars))
self.indices_char = dict((i, c) for i, c in enumerate(self.chars))
self.text = [self.char_indices[c] for c in self.raw_text]
__init__ BasicLSTMCell
One Hot i n i 1
0 n num_char [num_batch, seq_length, num_chars]
RNN t RNN state t
inputs[:, t, :] RNN output t+1 RNN
RNN num_chars
2 https://github.com/keras-team/keras/blob/master/examples/lstm_text_generation.py
4.4. RNN 20
TensorFlow, 0.3 beta
4.4: RNN
4.4. RNN 21
TensorFlow, 0.3 beta
class RNN(tf.keras.Model):
def __init__(self, num_chars):
super().__init__()
self.num_chars = num_chars
self.cell = tf.nn.rnn_cell.BasicLSTMCell(num_units=256)
self.dense = tf.keras.layers.Dense(units=self.num_chars)
• DataLoader
• loss
data_loader = DataLoader()
model = RNN(len(data_loader.chars))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
for batch_index in range(num_batches):
X, y = data_loader.get_batch(seq_length, batch_size)
with tf.GradientTape() as tape:
y_logit_pred = model(X)
loss = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_logit_pred)
print("batch %d: loss %f" % (batch_index, loss.numpy()))
grads = tape.gradient(loss, model.variables)
optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
tf.argmax()
np.random.choice()
temperature
4.4. RNN 22
TensorFlow, 0.3 beta
X_, _ = data_loader.get_batch(seq_length, 1)
for diversity in [0.2, 0.5, 1.0, 1.2]:
X = X_
print("diversity %f:" % diversity)
for t in range(400):
y_pred = model.predict(X, diversity)
print(data_loader.indices_char[y_pred[0]], end='', flush=True)
X = np.concatenate([X[:, 1:], np.expand_dims(y_pred, axis=1)], axis=-1)
diversity 0.200000:
conserted and conseive to the conterned to it is a self--and seast and the selfes as a seast the␣
,→ expecience and and and the self--and the sered is a the enderself and the sersed and as a the␣
,→ concertion of the series of the self in the self--and the serse and and the seried enes and␣
,→ seast and the sense and the eadure to the self and the present and as a to the self--and the␣
,→ seligious and the enders
diversity 0.500000:
can is reast to as a seligut and the complesed
has fool which the self as it is a the beasing and us immery and seese for entoured underself of␣
,→ the seless and the sired a mears and everyther to out every sone thes and reapres and seralise␣
,→ as a streed liees of the serse to pease the cersess of the selung the elie one of the were as we␣
,→ and man one were perser has persines and conceity of all self-el
diversity 1.000000:
entoles by
their lisevers de weltaale, arh pesylmered, and so jejurted count have foursies as is
descinty iamo; to semplization refold, we dancey or theicks-welf--atolitious on his
such which
here
oth idey of pire master, ie gerw their endwit in ids, is an trees constenved mase commars is leed␣
,→ mad decemshime to the mor the elige. the fedies (byun their ope wopperfitious--antile and the it␣
,→ as the f
diversity 1.200000:
4.4. RNN 23
TensorFlow, 0.3 beta
4.5 DRL
Reinforcement learning RL
AlphaGo
• [Mnih2013]
CartPole
4.5: CartPole
import gym
4.5. DRL 24
TensorFlow, 0.3 beta
env = gym.make('CartPole-v1') #
state = env.reset() #
while True:
env.render() #
action = model.predict(state) #
if done: #
break
Deep Q-Learning
import tensorflow as tf
import numpy as np
import gym
import random
from collections import deque
tf.enable_eager_execution()
num_episodes = 500
num_exploration_episodes = 100
max_len_episode = 1000
batch_size = 32
learning_rate = 1e-3
gamma = 1.
initial_epsilon = 1.
final_epsilon = 0.01
4.5. DRL 25
TensorFlow, 0.3 beta
x = self.dense3(x)
return x
env = gym.make('CartPole-v1') #
model = QNetwork()
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
replay_buffer = deque(maxlen=10000)
epsilon = initial_epsilon
for episode_id in range(num_episodes):
state = env.reset() #
epsilon = max(
initial_epsilon * (num_exploration_episodes - episode_id) / num_exploration_episodes,
final_epsilon)
for t in range(max_len_episode):
env.render() #
if random.random() < epsilon: # epsilon-greedy
action = env.action_space.sample() # epsilon
else:
action = model.predict(
tf.constant(np.expand_dims(state, axis=0), dtype=tf.float32)).numpy()
action = action[0]
next_state, reward, done, info = env.step(action) #
if done: #
episode
print("episode %d, epsilon %f, score %d" % (episode_id, epsilon, t))
break
4.5. DRL 26
TensorFlow, 0.3 beta
4.6 *
tf.keras.Model tf.keras.layers.Layer
class MyLayer(tf.keras.layers.Layer):
def __init__(self):
super().__init__()
#
1
build call
class LinearLayer(tf.keras.layers.Layer):
def __init__(self):
super().__init__()
4.6. * 27
TensorFlow, 0.3 beta
self.b = self.add_variable(name='b',
shape=[1], initializer=tf.zeros_initializer())
LinearLayer
class Linear(tf.keras.Model):
def __init__(self):
super().__init__()
self.layer = LinearLayer()
model = Linear()
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
X_placeholder = tf.placeholder(name='X', shape=[None, 3], dtype=tf.float32)
y_placeholder = tf.placeholder(name='y', shape=[None, 1], dtype=tf.float32)
y_pred = model(X_placeholder)
loss = tf.reduce_mean(tf.square(y_pred - y_placeholder))
train_op = optimizer.minimize(loss)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(100):
sess.run(train_op, feed_dict={X_placeholder: X, y_placeholder: y})
print(sess.run(model.variables))
TensorFlow
TensorFlow
• Python Pickle
• Python **kwargs
5.1 Checkpoint
Python pickle
model.variables TensorFlow ResourceVariable
checkpoint = tf.train.Checkpoint(model=model)
tf.train.Checkpoint() **kwargs
tf.keras.Model
model tf.train.Optimizer optimizer
29
TensorFlow, 0.3 beta
myAwesomeModel model
checkpoint.save(save_path_with_prefix)
save_path_with_prefix + save
checkpoint.save('./save/model.ckpt') save
checkpoint model.ckpt-1.index model.ckpt-1.data-00000-of-00001
checkpoint.save() .index .data
checkpoint
checkpoint restore
model_to_be_restored = MyModel() #
checkpoint = tf.train.Checkpoint(myAwesomeModel=model_to_be_restored) #
myAwesomeModel
checkpoint.restore(save_path_with_prefix_and_index)
save_path_with_prefix_and_index + +
checkpoint.restore('./save/model.ckpt-1') model.ckpt 1
tf.train.latest_checkpoint(save_path)
checkpoint save model.ckpt-1.index
model.ckpt-10.index 10 tf.train.latest_checkpoint('./save') ./save/
model.ckpt-10
# train.py
model = MyModel()
checkpoint = tf.train.Checkpoint(myModel=model) # Checkpoint model
Optimizer
#
checkpoint.save('./save/model.ckpt') #
# test.py
model = MyModel()
5.1. Checkpoint 30
TensorFlow, 0.3 beta
train.py tf.keras.Model
save_weight() model test.py model load_weight()
model load_weight() tf.train.
Checkpoint tf.train.Checkpoint Graph
Execution
import tensorflow as tf
import numpy as np
from zh.model.mlp.mlp import MLP
from zh.model.mlp.utils import DataLoader
tf.enable_eager_execution()
mode = 'test'
num_batches = 1000
batch_size = 50
learning_rate = 0.001
data_loader = DataLoader()
def train():
model = MLP()
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
checkpoint = tf.train.Checkpoint(myAwesomeModel=model) # Checkpoint
model
for batch_index in range(num_batches):
X, y = data_loader.get_batch(batch_size)
with tf.GradientTape() as tape:
y_logit_pred = model(tf.convert_to_tensor(X))
loss = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_logit_pred)
print("batch %d: loss %f" % (batch_index, loss.numpy()))
grads = tape.gradient(loss, model.variables)
optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
if (batch_index + 1) % 100 == 0: # 100 Batch
5.1. Checkpoint 31
TensorFlow, 0.3 beta
checkpoint.save('./save/model.ckpt') #
def test():
model_to_be_restored = MLP()
checkpoint = tf.train.Checkpoint(myAwesomeModel=model_to_be_restored) # Checkpoint
model_to_be_restored
checkpoint.restore(tf.train.latest_checkpoint('./save')) #
num_eval_samples = np.shape(data_loader.eval_labels)[0]
y_pred = model_to_be_restored.predict(tf.constant(data_loader.eval_data)).numpy()
print("test accuracy: %f" % (sum(y_pred == data_loader.eval_labels) / num_eval_samples))
if __name__ == '__main__':
if mode == 'train':
train()
if mode == 'test':
test()
5.2 TensorBoard
loss
TensorBoard
summary_writer = tf.contrib.summary.create_file_writer('./tensorboard')
summary_writer = tf.contrib.summary.create_file_writer('./tensorboard')
with summary_writer.as_default(), tf.contrib.summary.always_record_summaries():
#
for batch_index in range(num_batches):
5.2. TensorBoard 32
TensorFlow, 0.3 beta
# batch loss
tf.contrib.summary.scalar("loss", loss, step=batch_index)
tf.contrib.summary.scalar("MyScalar", my_scalar, step=batch_index) #
tf.contrib.summary.scalar()
scalar TensorBoard API
TensorFlow conda :
tensorboard --logdir=./tensorboard
TensorBoard 30
TensorBoard
• TensorBoard
TensorBoard --logdir
TensorBoard
import tensorflow as tf
import numpy as np
from zh.model.mlp.mlp import MLP
from zh.model.mlp.utils import DataLoader
tf.enable_eager_execution()
5.2. TensorBoard 33
TensorFlow, 0.3 beta
num_batches = 10000
batch_size = 50
learning_rate = 0.001
model = MLP()
data_loader = DataLoader()
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
summary_writer = tf.contrib.summary.create_file_writer('./tensorboard') #
with summary_writer.as_default(), tf.contrib.summary.always_record_summaries():
for batch_index in range(num_batches):
X, y = data_loader.get_batch(batch_size)
with tf.GradientTape() as tape:
y_logit_pred = model(tf.convert_to_tensor(X))
loss = tf.losses.sparse_softmax_cross_entropy(labels=y, logits=y_logit_pred)
print("batch %d: loss %f" % (batch_index, loss.numpy()))
tf.contrib.summary.scalar("loss", loss, step=batch_index) # loss
grads = tape.gradient(loss, model.variables)
optimizer.apply_gradients(grads_and_vars=zip(grads, model.variables))
5.3 GPU
/ / GPU
export CUDA_VISIBLE_DEVICES=2,3
import os
os.environ['CUDA_VISIBLE_DEVICES'] = "2,3"
2,3
TensorFlow tf.
ConfigProto TensorFlow tf.ConfigProto
tf.enable_eager_execution() Config allow_growth
TensorFlow
5.3. GPU 34
TensorFlow, 0.3 beta
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
tf.enable_eager_execution(config=config)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
tf.enable_eager_execution(config=config)
5.3. GPU 35
CHAPTER 6
TensorFlow
import tensorflow as tf
#
a = tf.constant(1) # Tensor
b = tf.constant(1)
c = a + b # c = tf.add(a, b) c a b Add Operation
import tensorflow as tf
36
TensorFlow, 0.3 beta
a = tf.placeholder(dtype=tf.int32) # Tensor
b = tf.placeholder(dtype=tf.int32)
c = a + b
a_ = input("a = ") # a_
b_ = input("b = ")
sess = tf.Session()
c_ = sess.run(c, feed_dict={a: a_, b: b_}) # feed_dict c
print("a + b = %d" % c_)
>>> a = 2
>>> b = 3
a + b = 5
** ** Variable tf.get_variable()
0 1
import tensorflow as tf
a = tf.get_variable(name='a', shape=[])
initializer = tf.assign(a, 0) # tf.assign(x, y) y x
a_plus_1 = a + 1 # a + tf.constant(1)
plus_one_op = tf.assign(a, a_plus_1)
sess = tf.Session()
sess.run(initializer)
for i in range(5):
sess.run(plus_one_op) # a
a_ = sess.run(a) # a a_
print(a_)
1.0
2.0
3.0
4.0
5.0
tf.global_variables_initializer()
import tensorflow as tf
a_plus_1 = a + 1
plus_one_op = tf.assign(a, a_plus_1)
sess = tf.Session()
sess.run(tf.global_variables_initializer()) #
for i in range(5):
sess.run(plus_one_op)
a_ = sess.run(a)
print(a_)
[ ]
1 1 1
1 1 1
1 1
1 1
1 1
import tensorflow as tf
sess = tf.Session()
C_ = sess.run(C)
print(C_)
[[3. 3.]
[3. 3.]]
Placeholder Variable
6.2
TensorFlow
6.2. 38
TensorFlow, 0.3 beta
import tensorflow as tf
#
learning_rate_ = tf.placeholder(dtype=tf.float32)
X_ = tf.placeholder(dtype=tf.float32, shape=[5])
y_ = tf.placeholder(dtype=tf.float32, shape=[5])
a = tf.get_variable('a', dtype=tf.float32, shape=[], initializer=tf.zeros_initializer)
b = tf.get_variable('b', dtype=tf.float32, shape=[], initializer=tf.zeros_initializer)
y_pred = a * X_ + b
loss = tf.constant(0.5) * tf.reduce_sum(tf.square(y_pred - y_))
#
grad_a = tf.reduce_sum((y_pred - y_) * X_)
grad_b = tf.reduce_sum(y_pred - y_)
#
new_a = a - learning_rate_ * grad_a
new_b = b - learning_rate_ * grad_b
update_a = tf.assign(a, new_a)
update_b = tf.assign(b, new_b)
num_epoch = 10000
learning_rate = 1e-3
with tf.Session() as sess:
# a b
tf.global_variables_initializer().run()
#
for e in range(num_epoch):
sess.run(train_op, feed_dict={X_: X, y_: y, learning_rate_: learning_rate})
print(sess.run([a, b]))
TensorFlow
TensorFlow tf.gradients(ys, xs) loss
a b
6.2. 39
TensorFlow, 0.3 beta
#
grad_a = tf.reduce_sum((y_pred - y_) * X_)
grad_b = tf.reduce_sum(y_pred - y_)
TensorFlow optimizer
#
grad_a = tf.reduce_sum((y_pred - y_) * X_)
grad_b = tf.reduce_sum(y_pred - y_)
#
new_a = a - learning_rate_ * grad_a
new_b = b - learning_rate_ * grad_b
update_a = tf.assign(a, new_a)
update_b = tf.assign(b, new_b)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate_)
grad = optimizer.compute_gradients(loss)
train_op = optimizer.apply_gradients(grad)
TensorFlow tf.train.GradientDescentOptimizer()
compute_gradients(loss) loss
apply_gradients(grad)
train_op = tf.train.GradientDescentOptimizer(learning_rate=learning_rate_).minimize(loss)
import tensorflow as tf
learning_rate_ = tf.placeholder(dtype=tf.float32)
X_ = tf.placeholder(dtype=tf.float32, shape=[5])
y_ = tf.placeholder(dtype=tf.float32, shape=[5])
6.2. 40
TensorFlow, 0.3 beta
y_pred = a * X_ + b
loss = tf.constant(0.5) * tf.reduce_sum(tf.square(y_pred - y_))
# TensorFlow
train_op = tf.train.GradientDescentOptimizer(learning_rate=learning_rate_).minimize(loss)
num_epoch = 10000
learning_rate = 1e-3
with tf.Session() as sess:
tf.global_variables_initializer().run()
for e in range(num_epoch):
sess.run(train_op, feed_dict={X_: X, y_: y, learning_rate_: learning_rate})
print(sess.run([a, b]))
6.2. 41
Bibliography
[LeCun1998] 25. LeCun, L. Bottou, Y. Bengio, and P. Haffner. Gradient-based learning applied to docu-
ment recognition. Proceedings of the IEEE, 86(11):2278-2324, November 1998. http://yann.lecun.
com/exdb/mnist/
[Graves2013] Graves, Alex. Generating Sequences With Recurrent Neural Networks. ArXiv:1308.0850
[Cs], August 4, 2013. http://arxiv.org/abs/1308.0850.
[Mnih2013] Mnih, Volodymyr, Koray Kavukcuoglu, David Silver, Alex Graves, Ioannis Antonoglou, Daan
Wierstra, and Martin Riedmiller. Playing Atari with Deep Reinforcement Learning. ArXiv:1312.5602
[Cs], December 19, 2013. http://arxiv.org/abs/1312.5602.
42