keras.layer.input()用法说明


Posted in Python onJune 16, 2020

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()用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
编写Python脚本把sqlAlchemy对象转换成dict的教程
May 29 Python
python django事务transaction源码分析详解
Mar 17 Python
Python对列表中的各项进行关联详解
Aug 15 Python
python sys,os,time模块的使用(包括时间格式的各种转换)
Apr 27 Python
python实现Zabbix-API监控
Sep 17 Python
Python判断一个文件夹内哪些文件是图片的实例
Dec 07 Python
python的sorted用法详解
Jun 25 Python
python实现人工智能Ai抠图功能
Sep 05 Python
python FTP批量下载/删除/上传实例
Dec 22 Python
Python3实现建造者模式的示例代码
Jun 28 Python
基于Python3读写INI配置文件过程解析
Jul 23 Python
Python爬虫之Selenium库的使用方法
Jan 03 Python
python适合做数据挖掘吗
Jun 16 #Python
Python+PyQt5+MySQL实现天气管理系统
Jun 16 #Python
Python实现SMTP邮件发送
Jun 16 #Python
python语言中有算法吗
Jun 16 #Python
python爬虫可以爬什么
Jun 16 #Python
通过cmd进入python的步骤
Jun 16 #Python
解决Keras 自定义层时遇到版本的问题
Jun 16 #Python
You might like
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
PHP中将网页导出为Word文档的代码
2012/05/25 PHP
Yii调试SQL的常用方法
2014/07/09 PHP
WordPress中is_singular()函数简介
2015/02/05 PHP
php链表用法实例分析
2015/07/09 PHP
jquery连缀语法如何实现
2012/11/29 Javascript
jQuery UI 实现email输入提示实例
2013/08/15 Javascript
jQuery性能优化的38个建议
2014/03/04 Javascript
jQuery插件jPaginate实现无刷新分页
2015/05/04 Javascript
jQuery实现自定义checkbox和radio样式
2015/07/13 Javascript
jQuery实现网页抖动的菜单抖动效果
2015/08/07 Javascript
有关Promises异步问题详解
2015/11/13 Javascript
深入理解javascript作用域第二篇之词法作用域和动态作用域
2016/07/24 Javascript
百度地图API之百度地图退拽标记点获取经纬度的实现代码
2017/01/12 Javascript
详解在vue-cli中引用jQuery、bootstrap以及使用sass、less编写css
2017/11/08 jQuery
基于ionic实现下拉刷新功能
2018/05/10 Javascript
vue实现跨域的方法分析
2019/05/21 Javascript
react 移动端实现列表左滑删除的示例代码
2019/07/04 Javascript
微信小程序获取复选框全选反选选中的值(实例代码)
2019/12/17 Javascript
使用Vue 自定义文件选择器组件的实例代码
2020/03/04 Javascript
python 中random模块的常用方法总结
2017/07/08 Python
python移位运算的实现
2019/07/15 Python
Python通用唯一标识符uuid模块使用案例
2020/09/10 Python
酒店实习个人鉴定
2013/12/07 职场文书
高职教师岗位职责
2013/12/24 职场文书
初三化学教学反思
2014/01/23 职场文书
幼儿园英语教学反思
2014/01/30 职场文书
商场消防演习方案
2014/02/12 职场文书
保护环境倡议书500字
2014/05/19 职场文书
供用电专业求职信
2014/07/07 职场文书
2014领导干部学习焦裕禄同志先进事迹思想汇报
2014/09/19 职场文书
师德自我剖析材料范文
2014/10/06 职场文书
2014年学生会生活部工作总结
2014/11/07 职场文书
会议简讯范文
2015/07/20 职场文书
2016大学生形势与政策心得体会
2016/01/12 职场文书
PHP中->和=>的意思
2021/03/31 PHP