T3再入门

TensorFlow再入门

在2.0环境中运行1.x代码, 使用一下语句
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

使用GPU
导入TensorFlow 查看版本
tf.version
查看当前主机上的运算设备
gpus = tf.config.experimental.list_physical_devices(device_type="GPU")
cpus = tf.config.experimental.list_physical_devices(device_type="CPU")
在指定CPU上运行
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
cpu_c = tf.matmul(cpu_a, cpu_b)
查看GPU是否可用
tf.test.is_gpu_available()
在指定GPU上运行
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10000, 1000])
gpu_b = tf.random.normal([1000, 2000])
gpu_c = tf.matmul(gpu_a, gpu_b)
创建函数cpu_run() 和 gpu_run()
def cpu_run():
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
c = tf.matmul(cpu_a, cpu_b)
return c

    def gpu_run():
        with tf.device('/gpu:0'):
            gpu_a = tf.random.normal([10000, 1000])
            gpu_b = tf.random.normal([1000, 2000])
            c = tf.matmul(gpu_a, gpu_b)
        return c 
比较在CPU和GPU上执行乘法操作的时间 -- 使用timeit工具统计
    cpu_time = timeit.timeit(cpu_run, number=10)
    gpu_time = timeit.timeit(gpu_run, number=10)
        cpu_time
            Out[30]: 4.104530872000055
        gpu_time
            Out[31]: 0.004487383999958183
            
    cpu_time = timeit.timeit(cpu_run, number=100)
    gpu_time = timeit.timeit(gpu_run, number=100)
        cpu_time
            Out[33]: 38.681798154000035
        gpu_time
            Out[34]: 0.23337887200000296

TensorFlow2.0特性
An end-to-end opens source machine learning platform
end-to-end:端到端
open source:开源, 开放设计和实现框架
machine learning:机器学习生态系统
动态图机制(Eager executio)
每次运行会建立和调试动态图
任然保留了静态图
可以在程序调试阶段使用动态图,部署阶段使用静态图。

框架特性
    多种环境支持
        移动设备、PC、服务器、集群、云端、本地、浏览器、嵌入式设备
    支持分布式模式
        TensorFlow自动检测GPU和CPU,充分利用它们并行、分布的执行
    简洁高效
        构建、训练、迭代模型:Eager Execution , Keras
        部署阶段:转化为静态图,提高执行效率
    社区支持

张量-Tensor:tf的基本数据对象 所有运算都是在Tensor中进行 CPU环境下, 张量和Numpy数组共享同一段内存
其实就是多维数组
Python中的list - 在内存中不连续存放,动态指针数组,读写效率低,占用内存空间大
Numpy的ndarray - 在内存中连续存放,存储空间小,读取和写入速度快,存在内存中,只能在CPU中运算,不能主动运算和检测利用GPU使用
Tensor:可以告诉运行于GPU和TPU之上, 支持嵌入式、CPU、单机多卡、多机多卡等复杂环境,
创建Tensor对象 - 由Tensor类实现
tf.constant(value, dtype, shape)函数:创建张量
value - 数字/Python列表/Numpy数组
dtype - 元素的数据类型
shape - 张量的形状
张量.numpy() - 获取张量的Numpy数组
tf.cast(x, dtype) - 改变张量中的元素类型 由低到高
tf.is_tensor() - 判断是否是张量
isinstance() - (数据、类型) 判断这个数据是不是这个类型

特殊张量
    全0张量
        tf.zeros(shape, dtype = tf.folat32)
    全1张量
        tf.ones(shape, dtype = tf.folat32)
    元素值相同
        tf.fill(dims, value) - dims是形状
        tf.constant(value = 9, shape = [2,3])
    随机数张量 -- 正态分布
        tf.random.normal(shape, mean, stddev, dtype) -- mean 均值(默认0), stddev 标准差(默认1)
    创建随机数张量 -- 截断正态分布
        tf.random.truncated_normal(shape, mean, stddev, dtype)
    设置随机种子  
        tf.random.set_seed()
    创建均匀分布张量 
        tf.random.uniform(shape, minval, maxval, dtype) 前闭后开
    随机打乱 -- 只打乱第一维数据
        tf.random.shuffle()
    创建序列
        tf.range(start, limit, delta = 1, dtype) delta-步长
        
Tensor属性
    ndim   
    shape  
    dtype
    shape()
    size()
    rank()

维度变换

张量的存储和视图
    逻辑组织
    物理组织.
改变张量的形状
    tf.reshape(tensor, shape)  shape = -1 自动推导出长度
多维张量的轴:张量的维度 同Numpy

增加和删除维度 -- 只改变视图, 不改变存储
    tf.expand_dims(input, axis)
    tf.squeeze(input, axis) -- 只能删除长度为1的维度
交换维度 -- 改变视图,改变存储顺序
    tf.transpose(a, perm) -- perm = [1, 0] 各个轴的顺序
拼接和分割
    tf.concat(tensors, axis)
    tf.split(value, num_or_size_splits, axis=0) [1:2:1]分割为3个张量
堆叠和分解
    tf.stack(values, axis)
    tf.unstack(values, axis)

部分采样
索引和切片 -- 同Numpy

数据提取
    gather(params, indices) -- 索引列表,将给定张量中队友元素提取
    gather(params, axis, indices) -- 多维采样
    gather_nd()

张量运算 -- x, y 逐元素计算
加减乘除运算
tf.add(x, y) -- 加
tf.sub(x, y) -- 减
tf.multiply(x, y) -- 乘
tf.divide(x, y) -- 除
tf.mod(x, y) -- 取模

幂运算 & 对数运算
    tf.pow(x, y)  --  x的y次幂
    tf.square(x)  --  x的平方
    tf.sqrt(x)  --  x的平方根
    tf.exp(x)  --  x的e次方
    tf.math.log(x)  --  计算自然对数,底数为x

可以利用运算符来完成常用操作。
    a+b a-b a*b a/b a%b a//b a**b 
。。。太多了,自己敲不过来,  略。

当张量和Numpy数组共同运算时, Tensor操作则会自动将Numpy转化为Tensor,反之亦然

向量乘法
    tf.matmul()  /  @运算符
    
数据统计:在某个维度上、或者全局的统计值
    tf.reduce_xxx()
求最值的索引
    tf.argmax()
    tf.argmin()

机器学习(Machine learning): 从数据中学习

学习算法:从数据中产生模型的算法
机器学习是通过学习算法从数据中学习模型的过程
学习数据
数据集/样本集
样本
属性/特征
标记/标签 有标记-监督学习
模型/假设/学习器
真相/真实
监督学习
回归 - 预测连续值
分类 - 预测离散值

无监督学习 -- 在样本数据没有标记的情况下,挖掘出数据内部蕴含的关系
聚类
距离

半监督学习 -- 将有监督学习和无监督学习相结合 大量没有标记的数据和少量标记数据共同学习

一元线性回归 -- y = wx + b
w:权重
b:偏置值
实现:
加载样本数据: x, y
学习模型:计算w, b
预测房价:y = wx + b

多元回归 -- 回归中包括两个或两个以上的自变量
多元线性回归 -- 因变量和自变量之间是线性关系

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容