tensorflow:tensorflow进阶

   日期:2020-10-19     浏览:146    评论:0    
核心提示:文章目录推荐文章一.合并与分割1.1 合并1.2 分割2.数据统计2.1 向量范数2.2 最大最小值、均值、和2.3 张量比较2.4 填充与复制3.数据限幅4.高级操作4.1 tf.gather4.2 tf.gather_nd4.3 tf.boolean_mask4.4 tf.where4.5 scatter_nd4.6 meshgrid5.数据集加载5.1 随机打乱5.2 批训练5.3预处理5.4 循环训练6.MNIST手写数字识别实战推荐文章Tensorflow:TensorFlow基础(一

文章目录

    • 推荐文章
    • 一.合并与分割
      • 1.1 合并
      • 1.2 分割
    • 2.数据统计
      • 2.1 向量范数
      • 2.2 最大最小值、均值、和
      • 2.3 张量比较
      • 2.4 填充与复制
    • 3.数据限幅
    • 4.高级操作
      • 4.1 tf.gather
      • 4.2 tf.gather_nd
      • 4.3 tf.boolean_mask
      • 4.4 tf.where
      • 4.5 scatter_nd
      • 4.6 meshgrid
    • 5.数据集加载
      • 5.1 随机打乱
      • 5.2 批训练
      • 5.3预处理
      • 5.4 循环训练
    • 6.MNIST手写数字识别实战

推荐文章

  • Tensorflow:TensorFlow基础(一)
  • Tensorflow:TensorFlow基础(二)

一.合并与分割

import  matplotlib
from    matplotlib import pyplot as plt
# Default parameters for plots
matplotlib.rcParams['font.size'] = 20
matplotlib.rcParams['figure.titlesize'] = 20
matplotlib.rcParams['figure.figsize'] = [9, 7]
matplotlib.rcParams['font.family'] = ['STKaiTi']
matplotlib.rcParams['axes.unicode_minus']=False 
import numpy as np
import  tensorflow as tf
from    tensorflow import keras
from    tensorflow.keras import datasets, layers, optimizers
import  os
from mpl_toolkits.mplot3d import Axes3D

1.1 合并

在 TensorFlow 中,可以通过 tf.concat(tensors, axis),其中 tensors 保存了所有需要
合并的张量 List,axis 指定需要合并的维度。合并张量 A,B 如下:

a = tf.random.normal([2,4]) # 模拟成绩册 A 
b = tf.random.normal([2,4]) # 模拟成绩册 B
tf.concat([a,b],axis=0)
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[ 0.16198424, -0.7170487 , -0.20940438, -0.46842927],
       [ 0.48012358,  0.82777774, -0.37541786, -0.6629169 ],
       [-0.15179256, -0.41726607, -1.9034436 ,  0.72443116],
       [-0.48332193,  0.23101914,  0.87393326, -1.2042308 ]],
      dtype=float32)>
tf.concat([a,b],axis=1)
<tf.Tensor: shape=(2, 8), dtype=float32, numpy=
array([[ 0.16198424, -0.7170487 , -0.20940438, -0.46842927, -0.15179256,
        -0.41726607, -1.9034436 ,  0.72443116],
       [ 0.48012358,  0.82777774, -0.37541786, -0.6629169 , -0.48332193,
         0.23101914,  0.87393326, -1.2042308 ]], dtype=float32)>

使用 tf.stack(tensors, axis) 可以合并多个张量 tensors, 当axis ≥ 0时,在 axis 之前插入;当axis < 0时,在 axis 之后插入新维度。

a = tf.random.normal([2,2])
b = tf.random.normal([2,2])
tf.stack([a,b],axis=0) #
<tf.Tensor: shape=(2, 2, 2), dtype=float32, numpy=
array([[[-2.1611633 ,  0.4318549 ],
        [-1.7556009 ,  0.6960343 ]],

       [[-0.84239227,  0.9800302 ],
        [ 0.5497298 ,  0.0607984 ]]], dtype=float32)>

同样可以选择在其他位置插入新维度,如在最末尾插入:

a = tf.random.normal([2,2])
b = tf.random.normal([2,2])
tf.stack([a,b],axis=-1)
<tf.Tensor: shape=(2, 2, 2), dtype=float32, numpy=
array([[[-2.09798   ,  0.5505884 ],
        [-1.1372471 ,  0.08376882]],

       [[-1.0453051 ,  0.47830236],
        [-1.1234645 , -0.97358865]]], dtype=float32)>

1.2 分割

合并操作的逆过程就是分割,将一个张量分拆为多个张量。

通过 tf.split(x, axis, num_or_size_splits) 可以完成张量的分割操作:

-x:待分割张量
-axis:分割的维度索引号 
-num_or_size_splits:切割方案。当 num_or_size_splits 为单个数值时,如 10,表示切割为 10 份;当 num_or_size_splits 为 List 时,每个元素表示每份的长度, 如[2,4,2,2]表示切割为 4 份,每份的长度分别为 2,4,2,2
x = tf.random.normal([4,2])
print(x)
result = tf.split(x, axis = 0, num_or_size_splits=2)
result

tf.Tensor(
[[ 0.77127916  0.62768835]
 [-0.76758057  1.3676474 ]
 [-0.10122015 -0.917917  ]
 [-0.1592799  -0.33670765]], shape=(4, 2), dtype=float32)






[<tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[ 0.77127916,  0.62768835],
        [-0.76758057,  1.3676474 ]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[-0.10122015, -0.917917  ],
        [-0.1592799 , -0.33670765]], dtype=float32)>]
tf.split(x, axis = 0, num_or_size_splits=[1,2,1])

[<tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[0.77127916, 0.62768835]], dtype=float32)>,
 <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
 array([[-0.76758057,  1.3676474 ],
        [-0.10122015, -0.917917  ]], dtype=float32)>,
 <tf.Tensor: shape=(1, 2), dtype=float32, numpy=array([[-0.1592799 , -0.33670765]], dtype=float32)>]

如果希望在某个维度上全部按长度为 1 的方式分割,还可以直接使用 tf.unstack(x, axis)。这种方式是 tf.split 的一种特殊情况,切割长度固定为 1,只需要指定切割维度即
可。

x = tf.random.normal([4,2])
tf.unstack(x, axis = 0)

[<tf.Tensor: shape=(2,), dtype=float32, numpy=array([-0.69661826,  0.42463547], dtype=float32)>,
 <tf.Tensor: shape=(2,), dtype=float32, numpy=array([ 0.40786335, -0.9408407 ], dtype=float32)>,
 <tf.Tensor: shape=(2,), dtype=float32, numpy=array([-0.71312106, -0.33494622], dtype=float32)>,
 <tf.Tensor: shape=(2,), dtype=float32, numpy=array([0.9833806, 0.7918092], dtype=float32)>]

2.数据统计

在神经网络的计算过程中,经常需要统计数据的各种属性,如最大值,均值,范数等。

2.1 向量范数

L1 范数,定义为向量

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服