Tensorflow基础知识与神经网络构建–step by step 入门TensorFlow(一)
标签: Tensorflow
我们将采用Jupyter notebook交互式编程的方式,通过一步步代码的讲解,学习Tensorflow编程。推荐使用Jupyter notebook作为开发环境按照文中的代码step by step 的学习。
文章列表:
Tensorflow基础知识与神经网络构建–step by step 入门TensorFlow(一)
深层神经网络实现–step by step 入门TensorFlow(二)
MNIST 数字识别和数据持久化–step by step 入门TensorFlow(三)
计算图
tensorflow程序一般分为2部分:1、构造计算图 2、执行计算
一般不用指定计算图,系统会维护一个默认的计算图
下面关于a、b、c的定义都是构造图,并且使用的是默认
下面的程序演示的就是图的构造
import tensorflow as tf
print(a.graph is tf.get_default_graph())
True
可以构造新图,但图上的变量不会共享
import tensorflow as tf
g1 = tf.Graph()
with g1.as_default():
v = tf.get_variable("v",shape=[1], initializer=tf.zeros_initializer())
g2 = tf.Graph()
with g2.as_default():
v = tf.get_variable("v",shape=[1], initializer=tf.ones_initializer())
with tf.Session(graph=g1) as sess:
sess.run(tf.global_variables_initializer())
with tf.variable_scope("",reuse=True):
print(sess.run(tf.get_variable("v")))
with tf.Session(graph=g2) as sess:
sess.run(tf.global_variables_initializer())
with tf.variable_scope("",reuse=True):
print(sess.run(tf.get_variable("v")))
[ 0.]
[ 1.]
还可以指定运算设备
g = tf.Graph()
with g.device('/gpu:0'):
result = a + b
with tf.