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 相关文章推荐
Pyramid将models.py文件的内容分布到多个文件的方法
Nov 27 Python
Python实现的ini文件操作类分享
Nov 20 Python
python使用wxpython开发简单记事本的方法
May 20 Python
Python变量和数据类型详解
Feb 15 Python
基于Django filter中用contains和icontains的区别(详解)
Dec 12 Python
浅谈python之高阶函数和匿名函数
Mar 21 Python
Python Django框架防御CSRF攻击的方法分析
Oct 18 Python
Python中Subprocess的不同函数解析
Dec 10 Python
Python之Matplotlib文字与注释的使用方法
Jun 18 Python
Django DRF APIView源码运行流程详解
Aug 17 Python
Django项目如何获得SSL证书与配置HTTPS
Apr 30 Python
python区块链持久化和命令行接口实现简版
May 25 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
超小PHP小马小结(方便查找后门的朋友)
2012/05/05 PHP
讲两件事:1.this指针的用法小探. 2.ie的attachEvent和firefox的addEventListener在事件处理上的区别
2007/04/12 Javascript
在js中判断checkboxlist(.net控件客户端id)是否有选中
2013/04/11 Javascript
js获取当前页面路径示例讲解
2014/01/08 Javascript
javascript时间函数大全
2014/06/30 Javascript
jQuery+PHP打造滑动开关效果
2014/12/16 Javascript
js的window.showModalDialog及window.open用法实例分析
2015/01/29 Javascript
JS网页在线获取鼠标坐标值的方法
2015/02/28 Javascript
实例代码详解jquery.slides.js
2015/11/16 Javascript
jquery拖拽效果完整实例(附demo源码下载)
2016/01/14 Javascript
javascript随机抽取0-100之间不重复的10个数
2016/02/25 Javascript
基于jquery实现图片放大功能
2016/05/07 Javascript
Bootstrap CSS组件之分页(pagination)和翻页(pager)
2016/12/17 Javascript
基于jQuery实现顶部导航栏功能
2016/12/27 Javascript
详解vue嵌套路由-query传递参数
2017/05/23 Javascript
JS监听事件的叠加和移除功能
2018/11/19 Javascript
微信接入之获取用户头像的方法步骤
2019/09/23 Javascript
BootStrap前端框架使用方法详解
2020/02/26 Javascript
vue开发移动端底部导航条功能
2020/04/08 Javascript
Electron 打包问题:electron-builder 下载各种依赖出错(推荐)
2020/07/09 Javascript
VScode编写第一个Python程序HelloWorld步骤
2018/04/06 Python
Diango + uwsgi + nginx项目部署的全过程(可外网访问)
2018/04/22 Python
python中安装django模块的方法
2020/03/12 Python
如何将json数据转换为python数据
2020/09/04 Python
巴西24小时在线药房:Drogasil
2020/06/20 全球购物
酒店开业庆典主持词
2014/03/21 职场文书
商铺租赁意向书
2014/04/01 职场文书
小学毕业寄语大全
2014/04/03 职场文书
宣传部部长竞选演讲稿
2014/04/26 职场文书
天地会口号
2014/06/17 职场文书
毕业生应聘求职信
2014/07/10 职场文书
环境保护与污染治理求职信
2014/07/16 职场文书
党支部考察意见范文
2015/06/02 职场文书
海洋天堂观后感
2015/06/05 职场文书
大学校园餐饮创业计划书
2019/08/07 职场文书
python周期任务调度工具Schedule使用详解
2021/11/23 Python