【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
原文地址:https://www.cnblogs.com/alex-bn-lee/p/14970452.html