• 吴裕雄--天生自然TensorFlow2教程:维度变换


    图片视图
    [b, 28, 28] # 保存b张图片,28行,28列(保存数据一般行优先),图片的数据没有被破坏
    [b, 28*28] # 保存b张图片,不考虑图片的行和列,只保存图片的数据,不关注图片数据的细节
    [b, 2, 14*28] # 保存b张图片,把图片分为上下两个部分,两个部分具体多少行是不清楚的
    [b, 28, 28, 1] # 保存b张图片,28行,28列,1个通道
    First Reshape(重塑视图)
    
    import tensorflow as tf
    a = tf.random.normal([4, 28, 28, 3])
    a.shape, a.ndim
    
    tf.reshape(a, [4, 784, 3]).shape  # 给出一张图片某个通道的数据,丢失行、宽的信息
    
    tf.reshape(a, [4, -1, 3]).shape  # 4*(-1)*3 = 4*28*28*3
    
    tf.reshape(a, [4, 784*3]).shape  # 给出一张图片的所有数据,丢失行、宽和通道的信息
    
    tf.reshape(a, [4, -1]).shape
    Second Reshape(恢复视图)
    
    tf.reshape(tf.reshape(a, [4, -1]), [4, 28, 28, 3]).shape
    
    tf.reshape(tf.reshape(a, [4, -1]), [4, 14, 56, 3]).shape
    
    tf.reshape(tf.reshape(a, [4, -1]), [4, 1, 784, 3]).shape
    
    first reshape:
    images: [4,28,28,3]
    reshape to: [4,784,3]
    
    second reshape:
    [4,784,3]  height:28,28  [4,28,28,3] √
    [4,784,3]  height:14,56  [4,14,56,3] ×
    [4,784,3]  28,height:28  [4,28,28,3] ×
    Transpose(转置)
    
    a = tf.random.normal((4, 3, 2, 1))
    a.shape
    
    tf.transpose(a).shape
    
    tf.transpose(a, perm=[0, 1, 3, 2]).shape  # 按照索引替换维度
    
    a = tf.random.normal([4, 28, 28, 3])  # b,h,w,c
    a.shape
    
    tf.transpose(a, [0, 2, 1, 3]).shape  # b,2,h,c
    
    tf.transpose(a, [0, 3, 2, 1]).shape  # b,c,w,h
    
    tf.transpose(a, [0, 3, 1, 2]).shape  # b,c,h,w
    Expand_dims(增加维度)
    
    a:[classes, students, classes]
    add school dim(增加学校的维度):
    [1, 4, 35, 8] + [1, 4, 35, 8] = [2, 4, 35, 8]
    
    a = tf.random.normal([4, 25, 8])
    a.shape
    
    tf.expand_dims(a, axis=0).shape  # 索引0前
    
    tf.expand_dims(a, axis=3).shape  # 索引3前
    
    tf.expand_dims(a,axis=-1).shape  # 索引-1后
    
    tf.expand_dims(a,axis=-4).shape  # 索引-4后,即左边空白处
    Squeeze(挤压维度)
    Only squeeze for shape = 1 dim(只删除维度为1的维度)
    
    [4, 35, 8, 1] = [4, 35, 8]
    [1, 4, 35, 8] = [14, 35, 8]
    [1, 4, 35, 1] = [4, 35, 8]
    
    tf.squeeze(tf.zeros([1,2,1,1,3])).shape
    
    a = tf.zeros([1,2,1,3])
    a.shape
    tf.squeeze(a,axis=0).shape
    tf.squeeze(a,axis=2).shape
    tf.squeeze(a,axis=-2).shape
    tf.squeeze(a,axis=-4).shape
  • 相关阅读:
    JVM 性能调优工具:jstat 使用
    JVM 性能调优工具,列表
    Mac 上 java 究竟在哪里,本文彻底让你搞清楚!
    Java 中常用锁实现的方式有两种:1. 用并发包中的锁类;2. 使用同步代码块
    Android 9.0 配置 charles 的 https 抓包
    背景,不要用:background-size: contain; 推荐用:background-size: 100% 100%;
    textarea 过滤 emoji 表情和空格(如果只 replace emoji 表情,会产生一个空格,所以再 replace 空格)
    小程序 textarea 无法隐藏的解决方案
    textarea 过滤 emoji 表情
    wx.setClipboardData:检测有复制内容再弹窗
  • 原文地址:https://www.cnblogs.com/tszr/p/12105236.html
Copyright © 2020-2023  润新知