计算图
Tensorflow程序的计算可分为两个阶段,在第一个阶段定义所有计算图中的计算,第二阶段为执行计算。
下面定义一个计算,生成计算图
import tensorflow as tf
a = tf.constant([1.0, 2.0], name="a")
b = tf.constant([2.0, 3.0], name="b")
result = a + b
print(tf.get_default_graph())
print(a.graph)
print(b.graph)
result: 均属于同一个计算图
<tensorflow.python.framework.ops.Graph object at 0x0000012C137DAD68>
<tensorflow.python.framework.ops.Graph object at 0x0000012C137DAD68>
<tensorflow.python.framework.ops.Graph object at 0x0000012C137DAD68>
还可以通过tf.Graph函数来生成新的计算图,代码如下,注意注释的部分已经被淘汰
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default():
v1 = tf.get_variable(
# "v", initializer=tf.zeros_initializer(shape[1])
"v1", shape=[4], initializer=tf.zeros_initializer()
)
v2 = tf.get_variable(
"v2", shape=[4], initializer=tf.ones_initializer()
)
with tf.Session(graph=g1) as sess:
# # tf.initialize_all_variables().run()
sess.run(tf.global_variables_initializer())
with tf.variable_scope("", reuse=True):
print(sess.run(tf.get_variable("v1")))
print(sess.run(tf.get_variable("v2")))
result
[ 0. 0. 0. 0.]
[ 1. 1. 1. 1.]
张量
下面介绍张量
import tensorflow as tf
a = tf.constant([1, 2], name="a", dtype=tf.float32)
b = tf.constant([2.0, 3.0], name="b")
result1 = a + b
# 此时还没有结果
print(result1)
result2 = tf.add(a, b, name="add")
# 此时才会有结果
print(result2)
result:可以看见在result1中默认会命名“+”为add,后面相同的名字TensorFlow会进行修改
Tensor("add:0", shape=(2,), dtype=float32)
Tensor("add_1:0", shape=(2,), dtype=float32)
会话
下面是会话
创建会话可以通过两种模式创建,一种是明确创建和关闭会话的函数
sess = tf.Session()
sess.run(...)
sess.close()
另一种就直接通过上下文管理器来管理会话,使用with
with tf.Session() as sess:
sess.run(...)
配置GPU的使用,其中allow_soft_placement会使得某些不被GPU支持的运算放入CPU中,而log_device_placement选择是否打印日记,可以记录那个节点被安排到哪个设备方便调试,而在生产环境中设为False,可以减少日志量
config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)
with tf.Session(graph=g1, config=config) as sess:
......
前向传播示例
下面是一个简单的神经网络的前向传播的过程,注意x为列向量,有两个[]
import tensorflow as tf
w1 = tf.Variable(tf.random_normal([2, 3], stddev=1, seed=1))
w2 = tf.Variable(tf.random_normal([3, 1], stddev=1, seed=1))
print(w1, w2)
x = tf.constant([[0.7, 0.9]])
a = tf.matmul(x, w1)
y = tf.matmul(a, w2) #此处是product
# 也可以是 with tf.Session as sess:
sess = tf.Session()
# 此处需要进行初始化
sess.run(tf.global_variables_initializer()) # 获得所想要得到的运算结果
print(sess.run(y)) #对product进行运算
sess.close()
result
[[ 3.95757794]]
placeholder
input1 = tf.placeholder(tf.float32)
之后需要feed才能run
添加层
def add_layer(inputs,in_size,out_size,activation_fun = None):
weight = tf.VAriable(tf.random_normal([in_size,out_size]))
biase = ...
if activation != None:
return activation(input*weight+biase)
导入数据
# None 表示任意数量
xs = tf.placeholder(tf.float32,[None, 784]) # 28*28
ys = tf.placeholder(tf.float32,[None, 10]) # 10
构建网络
l1 = add_layer(xs, 1, 10 activation = tf.nn.relu)
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),reduction_indices = [1]))
train_step = tf.train.Gradient...
sess.run(...)
GPU support
config = tf.ConfigProto()
config.gpu_options.allow_growth = True # 按需分配,不全部占用
for d in ["/device:GPU:2","/device:GPU:3"] #使用多块GPU
with tf.device(d):