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实现抓取页面上链接的简单爬虫分享
Jan 21 Python
Python中使用装饰器时需要注意的一些问题
May 11 Python
Python制作爬虫采集小说
Oct 25 Python
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
Sep 21 Python
解决python3捕获cx_oracle抛出的异常错误问题
Oct 18 Python
Python中浅拷贝copy与深拷贝deepcopy的简单理解
Oct 26 Python
python里 super类的工作原理详解
Jun 19 Python
python中bs4.BeautifulSoup的基本用法
Jul 27 Python
在pycharm中配置Anaconda以及pip源配置详解
Sep 09 Python
Python使用QQ邮箱发送邮件实例与QQ邮箱设置详解
Feb 18 Python
Pycharm 2020.1 版配置优化的详细教程
Aug 07 Python
Elasticsearch 数据类型及管理
Apr 19 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
调频问题解答
2021/03/01 无线电
JS中encodeURIComponent函数用php解码的代码
2012/03/01 PHP
Thinkphp实现MySQL读写分离操作示例
2014/06/25 PHP
PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析
2014/08/18 PHP
php数组中删除元素之重新索引的方法
2014/09/16 PHP
php输出全球各个时区列表的方法
2015/03/31 PHP
thinkphp 框架数据库切换实现方法分析
2020/05/18 PHP
JavaScript中使用Substring删除字符串最后一个字符
2013/11/03 Javascript
javascript页面加载完执行事件代码
2014/02/11 Javascript
JS阻止用户多次提交示例代码
2014/03/26 Javascript
angularjs指令中的compile与link函数详解
2014/12/06 Javascript
D3.js实现雷达图的方法详解
2016/09/22 Javascript
扩展bootstrap的modal模态框-动态添加modal框-弹出多个modal框
2017/02/21 Javascript
JS实现课堂随机点名和顺序点名
2017/03/09 Javascript
JavaScript实现弹窗效果代码分析
2017/03/09 Javascript
JavaScript获取URL参数的方法之一
2017/03/24 Javascript
利用jQuery+localStorage实现一个简易的计时器示例代码
2017/12/25 jQuery
详解webpack的proxyTable无效的解决方案
2018/06/15 Javascript
微信小程序scroll-view隐藏滚动条的方法详解
2020/03/25 Javascript
基于Cesium绘制抛物弧线
2020/11/18 Javascript
[02:03]风行者至宝清风环佩外观展示
2020/09/05 DOTA
python进阶教程之文本文件的读取和写入
2014/08/29 Python
Python常用小技巧总结
2015/06/01 Python
详解Python 数据库 (sqlite3)应用
2016/12/07 Python
Python PyInstaller安装和使用教程详解
2020/01/08 Python
Python可视化工具如何实现动态图表
2020/10/23 Python
沙特阿拉伯电子产品和家用电器购物网站:Black Box
2019/07/24 全球购物
理工大学毕业生自荐信
2013/11/01 职场文书
商场中秋节广播稿
2014/01/17 职场文书
自我鉴定标准格式
2014/03/19 职场文书
初中新生军训方案
2014/05/13 职场文书
2015年七一建党节演讲稿
2015/03/19 职场文书
英雄儿女观后感
2015/06/09 职场文书
2015年教师国培感言
2015/08/01 职场文书
pytest进阶教程之fixture函数详解
2021/03/29 Python
Python语言中的数据类型-序列
2022/02/24 Python