输出hello,world
import tensorflow as tf
tensor = tf.constant('hello, world')
sess = tf.Session()
result = sess.run(tensor)
sess.close()
print(result)
输出结果
2019-04-06 09:26:52.204823: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
b'hello, world'
常量,变量和数据类型
import tensorflow as tf
#构建string类型常量
hello = tf.constant('hello, world', dtype=tf.string)
#构建int32类型变量
a = tf.Variable(10, dtype=tf.int32)
TensorFlow的名称源自张量,张量是任意维度的数组,借助tf,可以操控具有大量维度的张量。
- 标量是零维数组(零阶张量)。如5, 'hewdy'
- 矢量是一维数组(一阶张量)。如[2,3,5,7]
- 矩阵是二维数组(二阶张量)。如[[1,2,3,4], [2,3,4,5]]
TensorFlow图(也成为计算图或数据流图是一种图数据结构,很多TensorFlow程序由单个图构成,但是tf可以选择创建多个图,图的节点是指令,图的边是张量。张量流经图,在每个节点由一个指令操控。一个指令的输出张量通常是后续指令的输入张量。Tensorflow会实现延迟执行模型,即系统仅会根据相关节点的需求在需要时计算节点。
#也可先创建变量,再为它分配一个值(始终需要指定一个默认值)
y = tf.Variable(0)
y = y.assign(10)
图必须在tf会话中运行,绘画存储了他所运行的状态 在使用tf.Variable时,必须在会话开始时调用tf.global_variables_initializer,以明确初始化这些变量
y = tf.Variable([0])
y = y.assign([5])
with tf.Session() as sess:
initialization = tf.global_variables_initializer()
print(y.eval())
TensorFlow编程实质上是一个两步流程
- 1.将变量常量,指令整合到一个图中。
- 2.在一个会话中评估这些变量常量和指令。
创建自己的Graph(以便跟踪状态),进行简单的加法
import tensorflow as tf
import numpy as np
import pandas as pd
#创建图
g = tf.Graph()
#将图形建立为默认图形
with g.as_default():
x = tf.constant(8, name='x_const')
y = tf.constant(5, name='y_const')
sum = tf.add(x, y, name='x_y_sum')
with tf.Session() as sess:
print(sum.eval())
简单的计算题
import tensorflow as tf
#输入存储容器
a = tf.placeholder(tf.float16)
b = tf.placeholder(tf.float16)
c = tf.placeholder(tf.float16)
#进行计算
d = tf.add(a, b)
e = tf.multiply(d, c)
f = tf.pow(e, 2)
g = tf.divide(f, a)
h = tf.sqrt(g)
#初始化
sess = tf.Session()
feed = {a:1, b:2, c:3}
#计算
res = sess.run(h,feed_dict=feed)
#输出
sess.close()
print(res)
矢量加法
import tensorflow as tf
import numpy as np
import pandas as pd
#一个包含质数的矢量
primes = tf.constant([2,3,5,7,11,13], dtype=tf.int32)
print("primes:", primes)
#一个全为1的矢量
ones = tf.ones([6], dtype=tf.int32)
print("ones:", ones)
#两个矢量相加
sum = tf.add(primes, ones)
print("sum:", sum)
#将prime矢量翻倍
two = tf.constant([2,2,2,2,2,2], dtype=tf.int32)
primes_dou = primes * two
print("prime_dou", primes_dou)
tensor与numpy的互相转化
import tensorflow as tf
import numpy as np
import pandas as pd
some_matrix = tf.constant([[1,2,3],[4,5,6]], dtype=tf.int32)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
num = some_matrix.eval(session=sess)
print(num)
ten = tf.convert_to_tensor(num)
print(ten)
输出结果
[[1 2 3]
[4 5 6]]
Tensor("Const_1:0", shape=(2, 3), dtype=int32)
广播机制
tf支持广播,利用广播,元素级运算中较小数组会增大到与较大数组相同的形状。 当张量被广播时,从概念上说,系统会复制其条目(出于性能考虑,实际并不复制,广播专为实现性能优化而设计)
练习:创建一个矢量,第i个元素为其平方减1,并输出
import tensorflow as tf
import numpy as np
import pandas as pd
ten = tf.constant([2,3,7,9,11], dtype=tf.int32)
prime = tf.pow(ten, 2)
one = tf.constant(1, dtype=tf.int32)
prime_one = tf.subtract(prime, one)
print(prime_one)
矩阵乘法
在线性代数中,当两个矩阵相乘,第一个列数必须等于第二个矩阵行数 如3 * 4矩阵与4 * 2矩阵相乘得3 * 2矩阵
import tensorflow as tf
import numpy as np
import pandas as pd
a = tf.constant([[5,2,4,3],[5,1,6,-2], [-1,3,-1,-2]], dtype=tf.int32)
b = tf.constant([[2,2], [3,5], [4,5], [1,6]], dtype=tf.int32)
matrix = tf.matmul(a, b)
print(matrix)
张量变形
由于加法与矩阵乘法均对运算数施加了条件,所以使用者需要频繁的改变张量得形状
import tensorflow as tf
import numpy as np
import pandas as pd
a = tf.constant([[1,2,3,4], [5,6,7,8], [1,4,6,7]], dtype=tf.int32)
b = tf.reshape(a, [1,12])
print(a)
print(b)
输出结果
Tensor("Const:0", shape=(3, 4), dtype=int32)
Tensor("Reshape:0", shape=(1, 12), dtype=int32)
1405



被折叠的 条评论
为什么被折叠?



