### 数据类型

TensorFlow的基本数据类型在借鉴NumPy（且当前对NumPy的类型几乎完全兼容）的基础上，也有些自己原生的一些数据类型，完整的数据类型列表可以参见官网，下表给出一些基本的数据类型：

Data Type Python type Description
DT_FLOAT tf.float32 32 bits floating point
DT_DOUBLE tf.float64 64 bits floating point
DT_INT32 tf.int32 32 bits signed int
DT_INT64 tf.int64 64 bits signed int
DT_STRING tf.string Variable length byte array
DT_BOOL tf.bool Boolean

### 变量

import tensorflow as tf
vector = tf.constant([2.0, 8], name="vector")
print tf.get_default_graph().as_graph_def()


#### 变量声明

import tensorflow as tf
# use InteractiveSession
sess = tf.InteractiveSession()
cons = tf.constant(8, name="consant")
var = tf.Variable(8, name="Variable")
# ==> Tensor("consant:0", shape=(), dtype=int32)
print cons
# ==> <tf.Variable 'Variable:0' shape=() dtype=int32_ref>
print var


#### 变量初始化

import tensorflow as tf
cons = tf.constant(8, name="consant")
var = tf.Variable(8, name="Variable")
init = tf.global_variables_initializer()  ## 全局初始化函数声明
with tf.Session() as sess:
sess.run(init) # 执行全局初始化
print sess.run(cons)  # OK, result is 8
# 未初始化： FailedPreconditionError: Attempting to use uninitialized value Variable_2
# 初始化：result is 8.
print sess.run(var)


import tensorflow as tf
var1 = tf.Variable(8, name="var1")
var2 = tf.Variable(8, name="var2")

init = tf.variables_initializer([var1]) ## 初始化var1
with tf.Session() as sess:
sess.run(init) # 执行全局初始化
# OK, result is 8
print sess.run(var1)
# 未初始化： FailedPreconditionError: Attempting to use uninitialized value var2_2
print sess.run(var2)


import tensorflow as tf
var2 = tf.Variable(8, name="var1")
with tf.Session() as sess:
sess.run(var2.initializer)
print sess.run(var2) # OK, result is 8.


#### 变量评估和赋值

import tensorflow as tf
var1 = tf.Variable(8, name="var1")
var1.assign(100) # 变量赋值，var1 任然是8
init = tf.variables_initializer([var1])
with tf.Session() as sess:
print var1.eval() # OK, result is 8.


import tensorflow as tf
var1 = tf.Variable(8, name="var1")

var2 = var1.assign(var1*2)

init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(var1.initializer)
print var2.eval() # result is 16
print var2.eval() # result is 32
print var2.eval() # result is 64


var2被assign的不是一个值，而是一个assign的operator，因此在Session里，每次run时就会做一次评估，这就好比C语言中的宏扩展。

### TensorBoard

"The computations you'll use TensorFlow for -like training a massive deep neural network - can be complex and confusing. To make it easier to understand, debug, and optimize TensorFlow programs, we've included a suite of visualization tools call TensorBoard".

writer = tf.summary.FileWriter('./graphs', sess.graph)


tensorboard --logdir="./graphs"