# TensorFlow架构与设计：变量初始化

`Variable`是一个特殊的OP，它拥有状态(Stateful)。本文通过阐述Variable初始化模型，深入理解变量初始化的过程。

### 线性模型

``````x  = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784,10]), name='W')
b = tf.Variable(tf.zeros([10]), name='b')
y = tf.matmul(x, W) + b
``````

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

### 初始化模型

`Variable`是一个特殊的OP，它拥有状态(Stateful)。如果从实现技术探究，`Variable`的Kernel实现直接持有一个`Tensor`实例，其生命周期与变量一致。相对于普通的Tensor实例，其生命周期仅对本次迭代(Step)有效；而Variable对多个迭代都有效，甚至可以存储到文件系统，或从文件系统中恢复。

``````W = tf.Variable(tf.zeros([784,10]), name='W')
``````

### 初始化过程

``````init = tf.global_variables_initializer()
``````

### 同位关系

``````node {
op: "Identity"
input: "W"
attr {
key: "T"
value {
type: DT_FLOAT
}
}
attr {
key: "_class"
value {
list {
s: "loc:@W"
}
}
}
}
``````

### 初始化依赖

``````W = tf.Variable(tf.zeros([784,10]), name='W')
V = tf.Variable(W.initialized_value(), name='V')
``````

``````init = tf.global_variables_initializer()
``````

## 开源技术书

``````https://github.com/horance-liu/tensorflow-internals
``````