keras.layer.input()用法说明

 更新时间:2020年06月16日 15:23:38   作者:TinaO-O  
这篇文章主要介绍了keras.layer.input()用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

tenserflow建立网络由于先建立静态的graph,所以没有数据,用placeholder来占位好申请内存。

那么keras的layer类其实是一个方便的直接帮你建立深度网络中的layer的类。

该类继承了object,是个基础的类,后续的诸如input_layer类都会继承与layer

由于model.py中利用这个方法建立网络,所以仔细看一下:他的说明详尽而丰富。

input()这个方法是用来初始化一个keras tensor的,tensor说白了就是个数组。他强大到之通过输入和输出就能建立一个keras模型。shape或者batch shape 必须只能给一个。shape = [None,None,None],会创建一个?*?*?的三维数组。

下面还举了个例子,a,b,c都是keras的tensor, `model = Model(input=[a, b], output=c)`

def Input(shape=None, batch_shape=None,
     name=None, dtype=None, sparse=False,
     tensor=None):
  """`Input()` is used to instantiate a Keras tensor.
  A Keras tensor is a tensor object from the underlying backend
  (Theano, TensorFlow or CNTK), which we augment with certain
  attributes that allow us to build a Keras model
  just by knowing the inputs and outputs of the model.
  For instance, if a, b and c are Keras tensors,
  it becomes possible to do:
  `model = Model(input=[a, b], output=c)`
  The added Keras attributes are:
    `_keras_shape`: Integer shape tuple propagated
      via Keras-side shape inference.
    `_keras_history`: Last layer applied to the tensor.
      the entire layer graph is retrievable from that layer,
      recursively.
  # Arguments
    shape: A shape tuple (integer), not including the batch size.
      For instance, `shape=(32,)` indicates that the expected input
      will be batches of 32-dimensional vectors.
    batch_shape: A shape tuple (integer), including the batch size.
      For instance, `batch_shape=(10, 32)` indicates that
      the expected input will be batches of 10 32-dimensional vectors.
      `batch_shape=(None, 32)` indicates batches of an arbitrary number
      of 32-dimensional vectors.
    name: An optional name string for the layer.
      Should be unique in a model (do not reuse the same name twice).
      It will be autogenerated if it isn't provided.
    dtype: The data type expected by the input, as a string
      (`float32`, `float64`, `int32`...)
    sparse: A boolean specifying whether the placeholder
      to be created is sparse.
    tensor: Optional existing tensor to wrap into the `Input` layer.
      If set, the layer will not create a placeholder tensor.
  # Returns
    A tensor.
  # Example
  ```python
  # this is a logistic regression in Keras
  x = Input(shape=(32,))
  y = Dense(16, activation='softmax')(x)
  model = Model(x, y)
  ```
  """

tip:我们在model.py中用到了shape这个attribute,

 input_image = KL.Input(
      shape=[None, None, config.IMAGE_SHAPE[2]], name="input_image")
    input_image_meta = KL.Input(shape=[config.IMAGE_META_SIZE],
                  name="input_image_meta")

阅读input()里面的句子逻辑:

可以发现,进入if语句的情况是batch_shape不为空,并且tensor为空,此时进入if,用assert判断如果shape不为空,那么久会有错误提示,告诉你要么输入shape 要么输入batch_shape, 还提示你shape不包含batch个数,就是一个batch包含多少张图片。

那么其实如果tensor不空的话,我们可以发现,也会弹出这个提示,但是作者没有写这种题型,感觉有点没有安全感。注意点好了

  if not batch_shape and tensor is None:
    assert shape is not None, ('Please provide to Input either a `shape`'
                  ' or a `batch_shape` argument. Note that '
                  '`shape` does not include the batch '
                  'dimension.')

如果单纯的按照规定输入shape,举个例子:只将shape输入为None,也就是说tensor的dimension我都不知道,但我知道这是个向量,你看着办吧。

input_gt_class_ids = KL.Input(
shape=[None], name="input_gt_class_ids", dtype=tf.int32)

就会调用Input()函数中的这个判断句式,注意因为shape是个List,所以shape is not None 会返回true。同时有没有输入batch_shape的话,就会用shape的参数去创造一个batch_shape.

if shape is not None and not batch_shape:
batch_shape = (None,) + tuple(shape)

比如如果输入:

shape = (None,)
batch_shape = (None,)+shape
batch_shape
#会得到(None, None)

可以发现,这里要求使用者至少指明你的数据维度,比如图片的话,是三维的,所以shape至少是[None,None,None],而且我认为shape = [None,1] 与shape = [None]是一样的都会创建一个不知道长度的向量。

以上这篇keras.layer.input()用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Django celery实现异步任务操作,并在后台运行(守护进程)

    Django celery实现异步任务操作,并在后台运行(守护进程)

    这篇文章主要介绍了Django celery实现异步任务操作,并在后台运行(守护进程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python实现FIFO缓存置换算法

    Python实现FIFO缓存置换算法

    这篇文章主要为大家详细介绍了Python实现FIFO(先进先出)缓存置换算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • python实现决策树C4.5算法详解(在ID3基础上改进)

    python实现决策树C4.5算法详解(在ID3基础上改进)

    下面小编就为大家带来一篇python实现决策树C4.5算法详解(在ID3基础上改进)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • python发送邮件脚本

    python发送邮件脚本

    这篇文章主要为大家详细介绍了发送邮件python脚本,支持多个附件,中文,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Window10下python3.7 安装与卸载教程图解

    Window10下python3.7 安装与卸载教程图解

    本文通过图文并茂的形式给大家介绍了WINDOW10下PYTHON3.7 安装与卸载,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • python3.6数独问题的解决

    python3.6数独问题的解决

    这篇文章主要为大家详细介绍了python3.6数独问题的解决,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • python中的二维列表实例详解

    python中的二维列表实例详解

    这篇文章主要介绍了python中的二维列表实例详解,文中给大家介绍了python 二维列表按列取元素的方法,需要的朋友可以参考下
    2018-06-06
  • python读取视频流提取视频帧的两种方法

    python读取视频流提取视频帧的两种方法

    这篇文章主要为大家详细介绍了python读取视频流提取视频帧的两种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04
  • python之如何复制excel模板并保留表格样式

    python之如何复制excel模板并保留表格样式

    这篇文章主要介绍了python之如何复制excel模板并保留表格样式问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • Python检验用户输入密码的复杂度

    Python检验用户输入密码的复杂度

    这篇文章主要介绍了Python检验用户输入密码的复杂度,在用户设置密码的时候检测输入的密码大小写数字等,需要的朋友可以参考下
    2023-04-04

最新评论