tensorflow基础

一. 基本教程

官方推荐搭建模型的时候用更高层的api,但是知道tensorflow的低层api更有利于知道其运行原理,也是很必要的。

1. tensorflow中的基本结构

Tensor

tensor(张量)是tensorflow的核心单元,表示原始数据,内部用n维numpy.ndarray表示,rank表示其维度,shape表示其形状, 0维为常数,1维为向量,2维为矩阵,可以向更高维拓展。

3. # a rank 0 tensor; a scalar with shape [],
[1., 2., 3.] # a rank 1 tensor; a vector with shape [3]
[[1., 2., 3.], [4., 5., 6.]] # a rank 2 tensor; a matrix with shape [2, 3]
[[[1., 2., 3.]], [[7., 8., 9.]]] # a rank 3 tensor with shape [2, 1, 3]
my_image = tf.zeros([10, 299, 299, 3])  # batch x height x width x color

可以用tf.rank()得到tensor的维度

r = tf.rank(my_image)
# After the graph runs, r will hold the value 4.

可以用tf.shape()得到tensor的形状,tf.reshape(tensor, newShape)可以改变tensor的形状

rank_three_tensor = tf.ones([3, 4, 5])
matrix = tf.reshape(rank_three_tensor, [6, 10])  # Reshape existing content into
                                                 # a 6x10 matrix

可以用tf.cast(ori_tensor, dtype)进行tensor的数据类型转换

# Cast a constant integer tensor into floating point.
float_tensor = tf.cast(tf.constant([1, 2, 3]), dtype=tf.float32)

可用tensor.eval得到tensor的值

constant = tf.constant([1, 2, 3])
tensor = constant * constant
print tensor.eval()  #返回ndarray格式类型

tf程序可以看成是构建一个利用输入tensor得到输出tensor关系的图,然后运行程序得到想要的结果。

Tensor组成

数据类型:float32, int32, or string等,tensor的数据类型都是已知的,并且里面的元素类型都是相同的。
形状:可以只知道一部分,有些只有在计算的时候才能知道完整的形状是多少,如shape = [3,?,3]

一些特殊的Tensor

  • tf.Variable
  • tf.constant
  • tf.placeholder
  • tf.SparseTensor
    除了Variable外,其余的都是不可变的,也就是说在一次上下文执行中,它的数值都是固定的,但是不同的执行不一定相同,比如tf.placeholder的feeding是一组随机数,每次执行的初始值都是不变的,但是后面的运行过程中数值是不会改变的。

需要注意的是feed是可以对所有的tensor进行赋值的
tf.Variable
tf.Variable可以指定一个名字,通过这个名字来复用这个Variable

my_variable = tf.get_variable("my_variable", [1, 2, 3])  #[1,2,3]指shape
my_int_variable = tf.get_variable("my_int_variable", [1, 2, 3], dtype=tf.int32,
  initializer=tf.zeros_initializer)
other_variable = tf.get_variable("other_variable", dtype=tf.int32,
  initializer=tf.constant([23, 42]))

在创建一个variable时可以设置其trainable参数来控制它是否会在训练时被修改
低层api创建了一个variable必须要显式的对它进行初始化,可以自己同时将所有的初始化也可以自己手动初始化(尤其在有依赖的时候)

session.run(tf.global_variables_initializer())
# Now all variables are initialized.
session.run(my_variable.initializer)  #手动初始化my_variable

有依赖情况时的赋值,用v.initialized_value()

v = tf.get_variable("v", shape=(), initializer=tf.zeros_initializer())
w = tf.get_variable("w", initializer=v.initialized_value() + 1)

在执行时得到variable的值

v = tf.get_variable("v", shape=(), initializer=tf.zeros_initializer())
assignment = v.assign_add(1)
with tf.control_dependencies([assignment]):
  w = v.read_value()  # w is guaranteed to reflect v's value after the
                      # assign_add operation.

因为tf.get_variable可以创建也可以复用,所以在同一个上下文中对同一个名称进行两次调用会报错,这个时候可以指定变量域,就可以避免这个问题。
因为是在不同的域中,所以两次都会进行创建

def my_image_filter(input_images):
    with tf.variable_scope("conv1"):
        # Variables created here will be named "conv1/weights", "conv1/biases".
        relu1 = conv_relu(input_images, [5, 5, 32, 32], [32])
    with tf.variable_scope("conv2"):
        # Variables created here will be named "conv2/weights", "conv2/biases".
        return conv_relu(relu1, [5, 5, 32, 32], [32])

也可以设置scope的reuse属性为true,同一个域下就会进行复用而不会有复用还是创建的矛盾

with tf.variable_scope("model"):
  output1 = my_image_filter(input1)
with tf.variable_scope("model", reuse=True):
  output2 = my_image_filter(input2)
或者
with tf.variable_scope("model") as scope:
  output1 = my_image_filter(input1)
  scope.reuse_variables()
  output2 = my_image_filter(input2)

防止打错名字,可以将scope设为一个变量

with tf.variable_scope("model") as scope:
  output1 = my_image_filter(input1)
with tf.variable_scope(scope, reuse=True):
  output2 = my_image_filter(input2)

计算图

低层api的tf编程分为两部分,构建计算图(tf.Graph)和运算计算图(tf.Session),和spark中的RDD相似。不执行运算的话图就只是简单的图结构,没有tensor流过。

图的构成

图结构
边 - tensors
点 - 操作(计算或者定义)
如:

a = tf.constant(3.0, dtype=tf.float32)
b = tf.constant(4.0) # also tf.float32 implicitly
total = a + b
print(a)
print(b)
print(total)

不执行直接打印,是没有值的

Tensor("Const:0", shape=(), dtype=float32)
Tensor("Const_1:0", shape=(), dtype=float32)
Tensor("add:0", shape=(), dtype=float32)

每一个操作都有一个独特的名字,这个名字和Python程序中给定标记没有关系,比如定义了a,但是在图结构中,它的名字是Const
collections
提供了一系列的collections,不同的collection具备不同的功能,如tf.Variable创建后就会加入到“global variables" and "trainable variables“中,前者表示全局,后者表示可以被训练。

也可以对操作命名,便于debug和观察,输出Tensor的时候也会带上相应操作的名字

c_0 = tf.constant(0, name="c")  # => operation named "c"

# Already-used names will be "uniquified".
c_1 = tf.constant(2, name="c")  # => operation named "c_1"

# Name scopes add a prefix to all operations created in the same context.
with tf.name_scope("outer"):
  c_2 = tf.constant(2, name="c")  # => operation named "outer/c"

  # Name scopes nest like paths in a hierarchical file system.
  with tf.name_scope("inner"):
    c_3 = tf.constant(3, name="c")  # => operation named "outer/inner/c"

  # Exiting a name scope context will return to the previous prefix.
  c_4 = tf.constant(4, name="c")  # => operation named "outer/c_1"

  # Already-used name scopes will be "uniquified".
  with tf.name_scope("inner"):
    c_5 = tf.constant(5, name="c")  # => operation named "outer/inner_1/c"

Session

tf.Session用于执行计算图,根据情况可以返回Tensor值或者为None(如train操作),用with会在退出block后自动关闭,否则需要调用tf.Session.close释放资源。

# Create a default in-process session.
with tf.Session() as sess:
  # ...

# Create a remote session.
with tf.Session("grpc://example.org:2222"):
  # ...

tf.Session(args)可以指定执行的机器、图等其他参数

x = tf.constant([[37.0, -23.0], [1.0, 4.0]])
w = tf.Variable(tf.random_uniform([2, 2]))
y = tf.matmul(x, w)
output = tf.nn.softmax(y)
init_op = w.initializer

with tf.Session() as sess:
  # Run the initializer on `w`.
  sess.run(init_op)

  # Evaluate `output`. `sess.run(output)` will return a NumPy array containing
  # the result of the computation.
  print(sess.run(output))

  # Evaluate `y` and `output`. Note that `y` will only be computed once, and its
  # result used both to return `y_val` and as an input to the `tf.nn.softmax()`
  # op. Both `y_val` and `output_val` will be NumPy arrays.
  y_val, output_val = sess.run([y, output])

在run的时候给placeholder赋值

# Define a placeholder that expects a vector of three floating-point values,
# and a computation that depends on it.
x = tf.placeholder(tf.float32, shape=[3])
y = tf.square(x)

with tf.Session() as sess:
  # Feeding a value changes the result that is returned when you evaluate `y`.
  print(sess.run(y, {x: [1.0, 2.0, 3.0]}))  # => "[1.0, 4.0, 9.0]"
  print(sess.run(y, {x: [0.0, 0.0, 5.0]}))  # => "[0.0, 0.0, 25.0]"

  # Raises `tf.errors.InvalidArgumentError`, because you must feed a value for
  # a `tf.placeholder()` when evaluating a tensor that depends on it.
  sess.run(y)

  # Raises `ValueError`, because the shape of `37.0` does not match the shape
  # of placeholder `x`.
  sess.run(y, {x: 37.0})

可以对run进行配置,比如记录下它的执行过程和时间

y = tf.matmul([[37.0, -23.0], [1.0, 4.0]], tf.random_uniform([2, 2]))

with tf.Session() as sess:
  # Define options for the `sess.run()` call.
  options = tf.RunOptions()
  options.output_partition_graphs = True
  options.trace_level = tf.RunOptions.FULL_TRACE

  # Define a container for the returned metadata.
  metadata = tf.RunMetadata()

  sess.run(y, options=options, run_metadata=metadata)

  # Print the subgraphs that executed on each device.
  print(metadata.partition_graphs)

  # Print the timings of each operation that executed.
  print(metadata.step_stats)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,809评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,189评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,290评论 0 359
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,399评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,425评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,116评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,710评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,629评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,155评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,261评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,399评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,068评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,758评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,252评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,381评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,747评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,402评论 2 358

推荐阅读更多精彩内容

  • 原文链接:https://yq.aliyun.com/articles/178374 0. 简介 在过去,我写的主...
    dopami阅读 5,661评论 1 3
  • 现在人工智能越来越火,也有很多的框架供大家选择,为什么推荐大家选tensorflow呢(因为他有一个很强的“亲爹”...
    CMHAILANQIU阅读 1,312评论 1 9
  • 在爱情这个字眼里,我一向是自私的。哪怕对爱的人造成再大的困扰,我也依然默默的爱着。为了自己的幸福与快乐,也不去过多...
    情海无涯阅读 60评论 0 0
  • 谨以此纪念我的一个中考失利的同学。(以她的角度写的。) 救救孩子。 ...
    血色夕阳阅读 311评论 0 0
  • Generate Patch Apply patch will change version1 to be the...
    lucientlau阅读 798评论 0 0