|
1 | | -# encoding: utf-8 |
2 | | -# 为了 Python3 的兼容,如果你用的 Python2.7 |
3 | | -from __future__ import print_function, division |
4 | | -import tensorflow as tf |
5 | | - |
6 | | -print('Loaded TF version', tf.__version__, '\n\n') |
7 | | - |
8 | | -# Tensor 在数学中是“张量” |
9 | | -# 标量,矢量/向量,张量 |
10 | | - |
11 | | -# 简单地理解 |
12 | | -# 标量表示值 |
13 | | -# 矢量表示位置(空间中的一个点) |
14 | | -# 张量表示整个空间 |
15 | | - |
16 | | -# 一维数组是矢量 |
17 | | -# 多维数组是张量, 矩阵也是张量 |
18 | | - |
19 | | - |
20 | | -# 4个重要的类型 |
21 | | -# @Variable 计算图谱中的变量 |
22 | | -# @Tensor 一个多维矩阵,带有很多方法 |
23 | | -# @Graph 一个计算图谱 |
24 | | -# @Session 用来运行一个计算图谱 |
25 | | - |
26 | | - |
27 | | -# 三个重要的函数 |
28 | | - |
29 | | -# Variable 变量 |
30 | | -# tf.Variable.__init__( |
31 | | -# initial_value=None, @Tensor |
32 | | -# trainable=True, |
33 | | -# collections=None, |
34 | | -# validate_shape=True, |
35 | | -# caching_device=None, |
36 | | -# name=None, |
37 | | -# variable_def=None, |
38 | | -# dtype=None) |
39 | | -# 注意:Variable是一个Class,Tensor也是一个Class |
40 | | - |
41 | | -# Constant 常数 |
42 | | -# tf.constant(value, dtype=None, shape=None, name='Const') |
43 | | -# return: a constant @Tensor |
44 | | - |
45 | | -# Placeholder 暂时变量? |
46 | | -# tf.placeholder(dtype, shape=None, name=None) |
47 | | -# return: 一个还尚未存在的 @Tensor |
48 | | - |
49 | | - |
50 | | - |
51 | | -# 让我们用计算图谱来实现一些简单的函数 |
52 | | -# + - * / 四则运算 |
53 | | -def basic_operation(): |
54 | | - v1 = tf.Variable(10) |
55 | | - v2 = tf.Variable(5) |
56 | | - addv = v1 + v2 |
57 | | - print(addv) |
58 | | - print(type(addv)) |
59 | | - print(type(v1)) |
60 | | - |
61 | | - c1 = tf.constant(10) |
62 | | - c2 = tf.constant(5) |
63 | | - addc = c1 + c2 |
64 | | - print(addc) |
65 | | - print(type(addc)) |
66 | | - print(type(c1)) |
67 | | - |
68 | | - # 用来运行计算图谱的对象/实例? |
69 | | - # session is a runtime |
70 | | - sess = tf.Session() |
71 | | - |
72 | | - # Variable -> 初始化 -> 有值的Tensor |
73 | | - tf.initialize_all_variables().run(session=sess) |
74 | | - |
75 | | - print('变量是需要初始化的') |
76 | | - print('加法(v1, v2) = ', addv.eval(session=sess)) |
77 | | - print('加法(v1, v2) = ', sess.run(addv)) |
78 | | - print('加法(c1, c2) = ', addc.eval(session=sess)) |
79 | | - print('\n\n') |
80 | | - #这种定义操作,再执行操作的模式被称之为“符号式编程” Symbolic Programming |
81 | | - |
82 | | - # tf.Graph.__init__() |
83 | | - # Creates a new, empty Graph. |
84 | | - graph = tf.Graph() |
85 | | - with graph.as_default(): |
86 | | - value1 = tf.constant([1,2]) |
87 | | - value2 = tf.Variable([3,4]) |
88 | | - mul = value1 / value2 |
89 | | - |
90 | | - with tf.Session(graph=graph) as mySess: |
91 | | - tf.initialize_all_variables().run() |
92 | | - print('一一对应的除法(value1, value2) = ', mySess.run(mul)) |
93 | | - print('一一对应的除法(value1, value2) = ', mul.eval()) |
94 | | - |
95 | | - # tensor.eval(session=sess) |
96 | | - # sess.run(tensor) |
97 | | - |
98 | | - |
99 | | -# 省内存?placeholder才是王道 |
100 | | -# def use_placeholder(): |
101 | | - graph = tf.Graph() |
102 | | - with graph.as_default(): |
103 | | - value1 = tf.placeholder(dtype=tf.float64) |
104 | | - value2 = tf.Variable([3, 4], dtype=tf.float64) |
105 | | - mul = value1 * value2 |
106 | | - |
107 | | - with tf.Session(graph=graph) as mySess: |
108 | | - tf.initialize_all_variables().run() |
109 | | - # 我们想象一下这个数据是从远程加载进来的 |
110 | | - # 文件,网络 |
111 | | - # 假装是 10 GB |
112 | | - value = load_from_remote() |
113 | | - for partialValue in load_partial(value, 2): |
114 | | - # runResult = mySess.run(mul, feed_dict={value1: partialValue}) |
115 | | - evalResult = mul.eval(feed_dict={value1: partialValue}) |
116 | | - print('乘法(value1, value2) = ', runResult) |
117 | | - # cross validation |
118 | | - |
119 | | -def load_from_remote(): |
120 | | - return [-x for x in range(1000)] |
121 | | - |
122 | | - |
123 | | -# 自定义的 Iterator |
124 | | -# yield, generator function |
125 | | -def load_partial(value, step): |
126 | | - index = 0 |
127 | | - while index < len(value): |
128 | | - yield value[index:index+step] |
129 | | - index += step |
130 | | - return |
131 | | - |
132 | | -if __name__ == '__main__': |
133 | | - basic_operation() |
134 | | - # use_placeholder() |
| 1 | +# encoding: utf-8 |
| 2 | +# 为了 Python3 的兼容,如果你用的 Python2.7 |
| 3 | +from __future__ import print_function, division |
| 4 | +import tensorflow as tf |
| 5 | + |
| 6 | +print('Loaded TF version', tf.__version__, '\n\n') |
| 7 | + |
| 8 | +# Tensor 在数学中是“张量” |
| 9 | +# 标量,矢量/向量,张量 |
| 10 | + |
| 11 | +# 简单地理解 |
| 12 | +# 标量表示值 |
| 13 | +# 矢量表示位置(空间中的一个点) |
| 14 | +# 张量表示整个空间 |
| 15 | + |
| 16 | +# 一维数组是矢量 |
| 17 | +# 多维数组是张量, 矩阵也是张量 |
| 18 | + |
| 19 | + |
| 20 | +# 4个重要的类型 |
| 21 | +# @Variable 计算图谱中的变量 |
| 22 | +# @Tensor 一个多维矩阵,带有很多方法 |
| 23 | +# @Graph 一个计算图谱 |
| 24 | +# @Session 用来运行一个计算图谱 |
| 25 | + |
| 26 | + |
| 27 | +# 三个重要的函数 |
| 28 | + |
| 29 | +# Variable 变量 |
| 30 | +# tf.Variable.__init__( |
| 31 | +# initial_value=None, @Tensor |
| 32 | +# trainable=True, |
| 33 | +# collections=None, |
| 34 | +# validate_shape=True, |
| 35 | +# caching_device=None, |
| 36 | +# name=None, |
| 37 | +# variable_def=None, |
| 38 | +# dtype=None) |
| 39 | +# 注意:Variable是一个Class,Tensor也是一个Class |
| 40 | + |
| 41 | +# Constant 常数 |
| 42 | +# tf.constant(value, dtype=None, shape=None, name='Const') |
| 43 | +# return: a constant @Tensor |
| 44 | + |
| 45 | +# Placeholder 暂时变量? |
| 46 | +# tf.placeholder(dtype, shape=None, name=None) |
| 47 | +# return: 一个还尚未存在的 @Tensor |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +# 让我们用计算图谱来实现一些简单的函数 |
| 52 | +# + - * / 四则运算 |
| 53 | +def basic_operation(): |
| 54 | + v1 = tf.Variable(10) |
| 55 | + v2 = tf.Variable(5) |
| 56 | + addv = v1 + v2 |
| 57 | + print(addv) |
| 58 | + print(type(addv)) |
| 59 | + print(type(v1)) |
| 60 | + |
| 61 | + c1 = tf.constant(10) |
| 62 | + c2 = tf.constant(5) |
| 63 | + addc = c1 + c2 |
| 64 | + print(addc) |
| 65 | + print(type(addc)) |
| 66 | + print(type(c1)) |
| 67 | + |
| 68 | + # 用来运行计算图谱的对象/实例? |
| 69 | + # session is a runtime |
| 70 | + sess = tf.Session() |
| 71 | + |
| 72 | + # Variable -> 初始化 -> 有值的Tensor |
| 73 | + tf.initialize_all_variables().run(session=sess) |
| 74 | + |
| 75 | + print('变量是需要初始化的') |
| 76 | + print('加法(v1, v2) = ', addv.eval(session=sess)) |
| 77 | + print('加法(v1, v2) = ', sess.run(addv)) |
| 78 | + print('加法(c1, c2) = ', addc.eval(session=sess)) |
| 79 | + print('\n\n') |
| 80 | + #这种定义操作,再执行操作的模式被称之为“符号式编程” Symbolic Programming |
| 81 | + |
| 82 | + # tf.Graph.__init__() |
| 83 | + # Creates a new, empty Graph. |
| 84 | + graph = tf.Graph() |
| 85 | + with graph.as_default(): |
| 86 | + value1 = tf.constant([1,2]) |
| 87 | + value2 = tf.Variable([3,4]) |
| 88 | + mul = value1 / value2 |
| 89 | + |
| 90 | + with tf.Session(graph=graph) as mySess: |
| 91 | + tf.initialize_all_variables().run() |
| 92 | + print('一一对应的除法(value1, value2) = ', mySess.run(mul)) |
| 93 | + print('一一对应的除法(value1, value2) = ', mul.eval()) |
| 94 | + |
| 95 | + # tensor.eval(session=sess) |
| 96 | + # sess.run(tensor) |
| 97 | + |
| 98 | + |
| 99 | +# 省内存?placeholder才是王道 |
| 100 | +# def use_placeholder(): |
| 101 | + graph = tf.Graph() |
| 102 | + with graph.as_default(): |
| 103 | + value1 = tf.placeholder(dtype=tf.float64) |
| 104 | + value2 = tf.Variable([3, 4], dtype=tf.float64) |
| 105 | + mul = value1 * value2 |
| 106 | + |
| 107 | + with tf.Session(graph=graph) as mySess: |
| 108 | + tf.initialize_all_variables().run() |
| 109 | + # 我们想象一下这个数据是从远程加载进来的 |
| 110 | + # 文件,网络 |
| 111 | + # 假装是 10 GB |
| 112 | + value = load_from_remote() |
| 113 | + for partialValue in load_partial(value, 2): |
| 114 | + # runResult = mySess.run(mul, feed_dict={value1: partialValue}) |
| 115 | + evalResult = mul.eval(feed_dict={value1: partialValue}) |
| 116 | + print('乘法(value1, value2) = ', runResult) |
| 117 | + # cross validation |
| 118 | + |
| 119 | +def load_from_remote(): |
| 120 | + return [-x for x in range(1000)] |
| 121 | + |
| 122 | + |
| 123 | +# 自定义的 Iterator |
| 124 | +# yield, generator function |
| 125 | +def load_partial(value, step): |
| 126 | + index = 0 |
| 127 | + while index < len(value): |
| 128 | + yield value[index:index+step] |
| 129 | + index += step |
| 130 | + return |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + basic_operation() |
| 134 | + # use_placeholder() |
0 commit comments