sklearn:在分类,回归领域不错。图像领域效果差
tensorflow :图像,nlp领域。
特别是卷积神经网络:图像识别
循环神经网络:自然语言处理nlp。
谷歌brain2015推出tensorflow(带graph的可视化,非常方便)
优点:
1、可移植性
支持包括CPU/GPU/TPU,以及能够很好地运行在移动端,
如安卓设备、ios、树莓派等等
2、多语言支持
python/c++程序。
3、高度的灵活性与效率
TensorFlow:张量在图中流动(data flow graphs),用于数值计算的开源软件库
能够灵活进行组装图,执行图。随着开发的进展,Tensorflow的效率不算在提高
4、支持
TensorFlow 由谷歌提供支持,谷歌投入了大量精力开发 TensorFlow,它希望
TensorFlow 成为机器学习研究人员和开发人员的通用语言
推荐Tensorflow 1.0 稳定版(有gpu /cpu版本)
Ubuntu虚拟机不能使用gpu版本
一行代码就 开启gpu非常简单
Tensorflow :计算密集型 (大量矩阵运算)
Django ,Scrapy :io密集型 ,频繁请求
cpu:综合能力还可以,计算能力不突出。
Gpu :对图像计算能力非常厉害。
tensorflow = 图graph(定义程序结构,定义流程图)+ 会话session(真正计算)
图graph = 数据单元(tensor张量) + 计算单位(op,operation)
张量: constant,Variable 等等
Tensorflow 程序 一般有:
前端系统:定义图,定义各种变量,张量等
后端系统: 根据图进行运算。
tensorflow语法比较特殊,第一简单的例子,实现加法运算
import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 默认图定义
a = tf.constant(1.0)
b = tf.constant(2.1)
c = tf.add(a,b)
print(a) # 常量
print(b) # 常量
print(c) # 加法op
with tf.Session() as ses:
ses.run(c)
图:定义好整个程序
Session 会话: 启动整个程序。
图
一组表示 tf.Operation计算单位的对象
和tf.Tensor表示操作之间流动的数据单元的对象
含有默认图
tf.get_default_graph()
# 获取默认图
op.graph
sess.graph
tensor.graph
查看所属于的图
g1 = tf.Graph() # 创建g1图
with g1.as_default():
a = tf.constant(1.0) # 图中定义
assert c.graph is g`
import tensorflow as tf
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# 默认图定义
a = tf.constant(1.0)
b = tf.constant(2.1)
c = tf.add(a,b)
defalut_graph = tf.get_default_graph()
print('defult_graph',defalut_graph)
print('c',c.graph)
g1 = tf.Graph()
with g1.as_default():
e = tf.ones((2,2))
g2 = tf.Graph()
with g2.as_default():
f = tf.zeros((3,3))
print('g1',g1)
print(e.graph)
print('g2',g2)
print(f.graph)
with tf.Session(graph=g2) as ses: # 指定g2图,只能运行g2里面的tensor和op
print(ses.run(f))
defult_graph <tensorflow.python.framework.ops.Graph object at 0x0000017AB1E679B0>
c <tensorflow.python.framework.ops.Graph object at 0x0000017AB1E679B0>
g1 <tensorflow.python.framework.ops.Graph object at 0x0000017AC780F6D8>
<tensorflow.python.framework.ops.Graph object at 0x0000017AC780F6D8>
g2 <tensorflow.python.framework.ops.Graph object at 0x0000017AC056E2B0>
<tensorflow.python.framework.ops.Graph object at 0x0000017AC056E2B0>
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
op:计算单元
1运算op: 标量和矢量运算,如矩阵乘法 matmul
2Constant :常量 ,0维数据,如32
3Variable :变量,在执行过程里随时间发生变化,如线性回归的权重w
以上 都属于tensor张量。
只要是tensorflow API定义的都是tensor。包括constant,variable,Session等
会话:
1 运行图,执行计算
2 分配计算机资源。比如给张量分配内存空间
3 管理资源 (变量的生命周期结束后就回收,队列,线程等资源,都是由session会话掌握)会话一旦结束,所有线程都结束。
tf.Session()
运行图的类,使用默认图
with tf.Session(graph = ) as ses:
指定运行图
会话资源
会话可能拥有很多资源,如 tf.Variable,tf.QueueBase
和tf.ReaderBase,会话结束后需要进行资源释放
使用上下文管理器
with tf.Session(graph = g1) as sess:
sess.run(...)
configproto 和eval的使用
configproto:打印tensor 占用cpu情况
a.eval() 和 ses.run(a)等价
import tensorflow as tf
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# 默认图定义
a = tf.constant(1.0,name='a')
b = tf.constant(2.1,name='b')
c = tf.add(a,b,name='c')
with tf.Session(config= tf.ConfigProto(log_device_placement=True)) as ses:
print(ses.run(c))
print(b.eval())
Device mapping: no known devices.
c: (Add): /job:localhost/replica:0/task:0/device:CPU:0
a: (Const): /job:localhost/replica:0/task:0/device:CPU:0
b: (Const): /job:localhost/replica:0/task:0/device:CPU:0
3.1
2.1
交互式:tf.InteractiveSession()
图里创建的张量, 和图绑定。Session会话运行一张图,就不能运行其他图里面的资源。
tf.placeholder(dtypes =数据类型 , shape =形状 )
placeholder 占位符,feed_dict 填充占位符
import tensorflow as tf
import os
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
data = [[1,2],[3,4]]
data = np.array(data)
holer = tf.placeholder(tf.int8,[None,None])
with tf.Session() as ses:
print(ses.run(holer,feed_dict={holer:data}))