alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

【592】Keras相关函数说明

参考:Keras API reference

参考:Keras: 基于 Python 的深度学习库


 

 

  • Merging layers

    • tf.keras.layers.Concatenate:按照输入顺序进行连接,axis 的值对应于 shape 的 0、1、2 的位置
      复制代码
      import keras 
      from keras.layers import Concatenate
      import numpy as np 
      
      x = np.arange(20).reshape(2, 2, 5)
      x 
      
      array([[[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9]],
      
             [[10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19]]])
      
      y = np.arange(20, 40).reshape(2, 2, 5)
      y 
      
      array([[[20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29]],
      
             [[30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39]]])
      
      Concatenate(axis=0)([x, y])
      # when axis=0, it changes the outmost layer
      
      <tf.Tensor: shape=(4, 2, 5), dtype=int64, numpy=
      array([[[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9]],
      
             [[10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19]],
      
             [[20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29]],
      
             [[30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39]]])>
      
      Concatenate(axis=0)([y, x])
      
      <tf.Tensor: shape=(4, 2, 5), dtype=int64, numpy=
      array([[[20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29]],
      
             [[30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39]],
      
             [[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9]],
      
             [[10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19]]])>
      
      Concatenate(axis=1)([x, y])
      # when axis=1, it changes the middle layer
      # column
      
      <tf.Tensor: shape=(2, 4, 5), dtype=int64, numpy=
      array([[[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9],
              [20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29]],
      
             [[10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19],
              [30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39]]])>
      
      Concatenate(axis=1)([y, x])
      
      <tf.Tensor: shape=(2, 4, 5), dtype=int64, numpy=
      array([[[20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29],
              [ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9]],
      
             [[30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39],
              [10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19]]])>
      
      Concatenate(axis=2)([x, y])
      # get the same result with
      # Concatenate(axis=-1)([x, y])
      # when axis=-1, it changes the inmost layer
      # row
      
      <tf.Tensor: shape=(2, 2, 10), dtype=int64, numpy=
      array([[[ 0,  1,  2,  3,  4, 20, 21, 22, 23, 24],
              [ 5,  6,  7,  8,  9, 25, 26, 27, 28, 29]],
      
             [[10, 11, 12, 13, 14, 30, 31, 32, 33, 34],
              [15, 16, 17, 18, 19, 35, 36, 37, 38, 39]]])>
      
      Concatenate(axis=-1)([y, x])
      
      <tf.Tensor: shape=(2, 2, 10), dtype=int64, numpy=
      array([[[20, 21, 22, 23, 24,  0,  1,  2,  3,  4],
              [25, 26, 27, 28, 29,  5,  6,  7,  8,  9]],
      
             [[30, 31, 32, 33, 34, 10, 11, 12, 13, 14],
              [35, 36, 37, 38, 39, 15, 16, 17, 18, 19]]])>
      View Code - Examples
      复制代码
    • tf.keras.layers.Add:按照输入顺序进行相加
      复制代码
      import keras 
      from keras.layers import Concatenate, Add
      import numpy as np 
      
      x = np.arange(20).reshape(2, 2, 5)
      x 
      
      array([[[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9]],
      
             [[10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19]]])
      
      y = np.arange(20, 40).reshape(2, 2, 5)
      y 
      
      array([[[20, 21, 22, 23, 24],
              [25, 26, 27, 28, 29]],
      
             [[30, 31, 32, 33, 34],
              [35, 36, 37, 38, 39]]])
      
      Add()([x, y])
      
      <tf.Tensor: shape=(2, 2, 5), dtype=int64, numpy=
      array([[[20, 22, 24, 26, 28],
              [30, 32, 34, 36, 38]],
      
             [[40, 42, 44, 46, 48],
              [50, 52, 54, 56, 58]]])>
      View Code - Examples
      复制代码
    • Average layer
    • Maximum layer
    • Minimum layer
    • Subtract layer
    • Multiply layer
    • Dot layer

 

  • tf.keras.preprocessing.image:图像数据处理

  • tf.keras.preprocessing.image.load_img:加载一张图片为 PIL 格式。

  • tf.keras.preprocessing.image.img_to_array:将 PIL 格式图片实例转换为一个 Numpy 数组。

  • tf.keras.preprocessing.image.ImageDataGenerator:生成批量的图片张量(包含数据增强)。

    • flow_from_directory:Takes the path to a directory & generates batches of augmented data.

 

  • tf.keras.Model:在函数式 API 中,给定一些输入张量和输出张量可以构建一个模型
    中文链接

    • compile:用于配置训练模型。
    • fit:以给定数量的轮次(数据集上的迭代)训练模型。
    • evaluate:在测试模式下返回模型的误差值和评估标准值。
    • predict:为输入样本生成输出预测。
    • fit_generator:使用 Python 生成器(或 Sequence 实例)逐批生成的数据,按批次训练模型。
    • evaluate_generator:在数据生成器上评估模型。
    • predict_generator:为来自数据生成器的输入样本生成预测。
    • summary:打印网络结构。
    • save:保存模型参数及结构。
    • save_model
    • load_model
    • get_weights
    • save_weights
    • load_weights

posted on   McDelfino  阅读(57)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2013-07-04 【124】排球基本技术
点击右上角即可分享
微信分享提示