# CS20SI Operations

CS 20SI: Tensorflow for Deep Learning Research

## Fun with TensorBoard

graph 的可视化，语句为tf.summary.FileWriter，将sess的图输出到./graphs

import tensorflow as tf
a = tf.constant(2)
b = tf.constant(3)
with tf.Session() as sess:
writer = tf.summary.FileWriter('./graphs', sess.graph)
print sess.run(x)
# close the writer when you’re done using it
writer.close()

$python [yourprogram.py]$ tensorboard --logdir="./graphs"

a = tf.constant([2, 2], name="a")
b = tf.constant([3, 6], name="b")
x = tf.add(a, b, name="add")

tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=None)

## Constant types

### 建立标量或者张量

tf​.​constant​(​value​,​ dtype​=​None​,​ shape​=​None​,​ name​=​'Const'​,​ verify_shape​=​False)
# constant of 1d tensor (vector)
a = tf.constant([2, 2], name="vector")
# constant of 2x2 tensor (matrix)
b = tf.constant([[0, 1], [2, 3]], name="b")

### 创建指定初值的张量

tf.fill 可以将tensor填充为同一个值。

tf​.​zeros​(​shape​,​ dtype​=​tf​.​float32​,​ name​=​None)
# create a tensor of shape and all elements are zeros
tf.zeros([2, 3], tf.int32) ==> [[0, 0, 0], [0, 0, 0]]

tf​.​zeros_like​(​input_tensor​,​ dtype​=​None​,​ name​=​None​,​ optimize​=​True)
# create a tensor of shape and type (unless type is specified) as the input_tensor
but all elements are zeros.
# input_tensor is [0, 1], [2, 3], [4, 5]]
tf.zeros_like(input_tensor) ==> [[0, 0], [0, 0], [0, 0]]

tf​.​ones​(​shape​,​ dtype​=​tf​.​float32​,​ name​=​None)
# create a tensor of shape and all elements are ones
tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]]

tf​.​ones_like​(​input_tensor​,​ dtype​=​None​,​ name​=​None​,​ optimize​=​True)
# create a tensor of shape and type (unless type is specified) as the input_tensor
but all elements are ones.
# input_tensor is [0, 1], [2, 3], [4, 5]]
tf.ones_like(input_tensor) ==> [[1, 1], [1, 1], [1, 1]]

tf​.​fill​(​dims​,​ value​,​ name​=​None​)
# create a tensor filled with a scalar value.
tf.ones([2, 3], 8) ==> [[8, 8, 8], [8, 8, 8]]


### 常量序列

tf​.​linspace，线性划分。

tf​.​linspace​(​start​,​ stop​,​ num​,​ name​=​None)
# create a sequence of num evenly-spaced values are generated beginning at start. If
num > 1, the values in the sequence increase by stop - start / num - 1, so that the
last one is exactly stop.
# start, stop, num must be scalars
# comparable to but slightly different from numpy.linspace
# numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
tf.linspace(10.0, 13.0, 4, name="linspace") ==> [10.0 11.0 12.0 13.0]

tf.range递增（减）数列

tf​.​range​(​start​,​ limit​=​None​,​ delta​=​1​,​ dtype​=​None​,​ name​=​'range')
# create a sequence of numbers that begins at start and extends by increments of
delta up to but not including limit
# slight different from range in Python
# 'start' is 3, 'limit' is 18, 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
# 'start' is 3, 'limit' is 1, 'delta' is -0.5
tf.range(start, limit, delta) ==> [3, 2.5, 2, 1.5]
# 'limit' is 5
tf.range(limit) ==> [0, 1, 2, 3, 4]

tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None,
name=None)
tf.random_uniform(shape, minval=0, maxval=None, dtype=tf.float32, seed=None,
name=None)
tf.random_shuffle(value, seed=None, name=None)
tf.random_crop(value, size, seed=None, name=None)
tf.multinomial(logits, num_samples, seed=None, name=None)
tf.random_gamma(shape, alpha, beta=None, dtype=tf.float32, seed=None, name=None)

TF官网的数学操作文档

## 数据类型

TF可以使用Python原生类型、TF原生类型、Numpy数据类型。TF的数据类型基于Numpy，事实上np.int32 == tf.int32返回的是True

Note1: tf.string 没有与Numpy中的字符类型完全对应。但仍可以处理从Numpy中导入的字符——只需要不在Numpy中特别声明dtype。

Note2: TensorFlow和NumPy都是n-d数组库。 NumPy支持ndarray，但不提供创建张量函数和自动计算导数以及GPU支持的方法。

Note3: 使用Python类型来指定TensorFlow对象是快速和容易的，它对于原型设计的想法很有用。 但是，这样做有一个重要的缺陷。 Python类型缺乏显式地声明数据类型的能力，但是TensorFlow的数据类型更为具体。 例如，所有的整数是相同的类型，但是TensorFlow有8位，16位，32位和64位整数可用。 因此，如果您使用Python类型，则TensorFlow必须推断您的数据类型。

## 变量

• 常量是常数，变量可以被赋值，因此可以被改变。
• 常量值被存储在图graph中，并且每当图被加载时值就会被复制。变量则分开存储，并可能依赖于一个参数服务。

### 定义变量

#create variable a with scalar value
a = tf . Variable ( 2 , name = "scalar" )
#create variable b as a vector
b = tf . Variable ([ 2 , 3 ], name = "vector" )
#create variable c as a 2x2 matrix
c = tf . Variable ([[ 0 , 1 ], [ 2 , 3 ]], name = "matrix" )
# create variable W as 784 x 10 tensor, filled with zeros
W = tf . Variable ( tf . zeros ([ 784 , 10 ]))

x = tf.Variable(...)
x.initializer # init
x.assign(...) # write op
# and more

init = tf . global_variables_initializer ()
with tf . Session () as sess:
tf . run ( init)

init_ab = tf . variables_initializer ([ a , b ], name = "init_ab") with tf . Session () as sess:
tf . run ( init_ab)

### 估计变量值


# W is a random 700 x 100 variable object
W = tf . Variable ( tf . truncated_normal ([ 700 , 10 ]))
with tf . Session () as sess:
sess . run ( W . initializer)
print W . eval ()

### 对变量赋值

W = tf . Variable ( 10 )
W . assign ( 100 )
with tf . Session () as sess:
sess . run ( W . initializer)
print W . eval () # >> 10

sess . run ( assign_op)

# create a variable whose original value is 2
a = tf . Variable ( 2 , name = "scalar" )
# assign a * 2 to a and call that op
a_times_two a_times_two = a . assign ( a * 2)
init = tf . global_variables_initializer ()
with tf . Session () as sess:
sess . run ( init)
# have to initialize a, because a_times_two op depends on the value of a
sess . run ( a_times_two ) # >> 8
sess . run ( a_times_two ) # >> 16

W = tf.Variable(10)
with tf.Session() as sess:
sess.run(W.initializer)
print sess.run(W.assign_sub(2)) # >> 18

W = tf.Variable(10)
sess1 = tf.Session()
sess2 = tf.Session()
sess1.run(W.initializer)
sess2.run(W.initializer)
print sess2.run(W.assign_sub(2)) # >> 8
print sess2.run(W.assign_sub(50)) # >> -42
sess1.close()
sess2.close()

# W is a random 700 x 100 tensor W = tf . Variable ( tf .truncated_normal ([ 700 , 10 ]))
U = tf . Variable ( W * 2)
U = tf . Variable ( W . intialized_value () * 2)

## 交互会话

sess = tf . InteractiveSession ()
a = tf . constant ( 5.0)
b = tf . constant ( 6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'
print ( c . eval ())
sess . close ()

## 控制依赖

# your graph g have 5 ops: a, b, c, d, e
with g . control_dependencies ([ a , b , c ]):
# d and e will only run after a, b, and c have executed.
d = ...
e = …

## Placeholders 与 feed_dict

Tensorflow程序通常有两个阶段：

1. 定义图
2. 使用会话执行图中的操作

tf . placeholder ( dtype , shape = None , name = None)
# create a placeholder of type float 32-bit, shape is a vector of 3 elements
a = tf . placeholder ( tf . float32 , shape =[ 3 ])
# create a constant of type float 32-bit, shape is a vector of 3 elements
b = tf . constant ([ 5 , 5 , 5 ], tf . float32)
# use the placeholder as you would a constant or a variable
c = a + b # Short for tf.add(a, b)
# If we try to fetch c , we will run into error.
with tf . Session () as sess:
print ( sess . run ( c ))
>> NameError

with tf . Session () as sess:
# feed [1, 2, 3] to placeholder a via the dict {a: [1, 2, 3]}
# fetch value of c
print ( sess . run ( c , { a : [ 1 , 2 , 3 ]}))
>> [ 6. 7. 8.]

x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(10):
sess.run(z)
writer.close()

x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for _ in range(10):
sess.run(tf.add(x, y)) # create the op add only when you need to compute it
writer.close()

print tf.get_default_graph().as_graph_def()

node {
attr {
key: "T"
value {
type: DT_INT32
}
}
}

node {
...
}
node {
...
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120