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中time模块与datetime模块在使用中的不同之处
Nov 24 Python
python编写Logistic逻辑回归
Dec 30 Python
Python使用wget实现下载网络文件功能示例
May 31 Python
Python魔法方法详解
Feb 13 Python
对python文件读写的缓冲行为详解
Feb 13 Python
Python实现一个数组除以一个数的例子
Jul 20 Python
安装docker-compose的两种最简方法
Jul 30 Python
python Jupyter运行时间实例过程解析
Dec 13 Python
Python代码中如何读取键盘录入的值
May 27 Python
python实现文件+参数发送request的实例代码
Jan 05 Python
python利用proxybroker构建爬虫免费IP代理池的实现
Feb 21 Python
详解Python 3.10 中的新功能和变化
Apr 28 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设计模式 Visitor 访问者模式
2011/06/28 PHP
php使用函数pathinfo()、parse_url()和basename()解析URL
2016/11/25 PHP
一段利用WSH获取登录时间的jscript代码
2008/05/11 Javascript
8个超棒的学习 jQuery 的网站 推荐收藏
2011/04/02 Javascript
jQuery 源码分析笔记(2) 变量列表
2011/05/28 Javascript
intro.js 页面引导简单用法 分享
2013/08/06 Javascript
jquery教程限制文本框只能输入数字和小数点示例分享
2014/01/13 Javascript
JavaScript设计模式之策略模式实例
2014/10/10 Javascript
10条建议帮助你创建更好的jQuery插件
2015/05/18 Javascript
JavaScript的面向对象编程基础
2015/08/13 Javascript
使用jquery插件qrcode生成二维码
2015/10/22 Javascript
Prototype框架详解
2015/11/25 Javascript
ThinkPHP+jquery实现“加载更多”功能代码
2017/03/11 Javascript
JavaScript 过滤关键字
2017/03/20 Javascript
Vue如何引入远程JS文件
2017/04/20 Javascript
详解vue2父组件传递props异步数据到子组件的问题
2017/06/29 Javascript
Angular实现类似博客评论的递归显示及获取回复评论的数据
2017/11/06 Javascript
Vue使用NProgress的操作过程解析
2019/10/10 Javascript
javascript canvas API内容整理
2020/02/16 Javascript
[03:07]2015国际邀请赛选手档案EHOME.rOtK 是什么让他落泪?
2015/07/31 DOTA
常用python数据类型转换函数总结
2014/03/11 Python
Python重新引入被覆盖的自带function
2014/07/16 Python
零基础写python爬虫之urllib2使用指南
2014/11/05 Python
python常见数制转换实例分析
2015/05/09 Python
详解Python中find()方法的使用
2015/05/18 Python
Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】
2018/12/05 Python
Pycharm配置远程调试的方法步骤
2018/12/17 Python
实例讲解Python3中abs()函数
2019/02/19 Python
利用python实现冒泡排序算法实例代码
2019/12/01 Python
Python打开文件、文件读写操作、with方式、文件常用函数实例分析
2020/01/07 Python
css3 给页面加个半圆形导航条主要利用旋转和倾斜样式
2014/02/10 HTML / CSS
HTML5之SVG 2D入门12—SVG DOM及DOM操作介绍
2013/01/30 HTML / CSS
英国文具、办公用品和科技商店:Ryman
2018/09/27 全球购物
攀岩、滑雪、徒步旅行装备:Black Diamond Equipment
2019/08/16 全球购物
测控技术与仪器个人求职信范文
2013/12/30 职场文书
行政文员岗位职责
2015/02/04 职场文书