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中使用cookielib和urllib2配合PyQuery抓取网页信息
Apr 25 Python
Python中的super用法详解
May 28 Python
Python3之读取连接过的网络并定位的方法
Apr 22 Python
python list数据等间隔抽取并新建list存储的例子
Nov 27 Python
Python列表切片常用操作实例解析
Dec 16 Python
Python: 传递列表副本方式
Dec 19 Python
关于Pytorch的MNIST数据集的预处理详解
Jan 10 Python
PIL包中Image模块的convert()函数的具体使用
Feb 26 Python
Pymysql实现往表中插入数据过程解析
Jun 02 Python
Keras—embedding嵌入层的用法详解
Jun 10 Python
Python进行特征提取的示例代码
Oct 15 Python
基于PyQT5制作一个桌面摸鱼工具
Feb 15 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
《魔兽争霸3:重制版》翻车了?你想要的我们都没有
2019/11/07 魔兽争霸
利用Homestead快速运行一个Laravel项目的方法详解
2017/11/14 PHP
建议大家看下JavaScript重要知识更新
2007/07/08 Javascript
JavaScript下申明对象的几种方法小结
2008/10/02 Javascript
关于用Jquery的height()、width()计算动态插入的IMG标签的宽高的问题
2010/12/08 Javascript
深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
2014/06/10 Javascript
对Js OOP编程 创建对象的一些全面理解
2016/07/26 Javascript
Nodejs实现短信验证码功能
2017/02/09 NodeJs
AngularJS监听路由变化的方法
2017/03/07 Javascript
vue-prop父组件向子组件进行传值的方法
2018/03/01 Javascript
微信小程序自定义弹窗wcPop插件
2018/11/19 Javascript
nuxt中使用路由守卫的方法步骤
2019/01/27 Javascript
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
js比较两个单独的数组或对象是否相等的实例代码
2019/04/28 Javascript
详解vue-cli@2.x项目迁移日志
2019/06/06 Javascript
vue-cli3自动消除console.log()的调试信息方式
2020/10/21 Javascript
vue3弹出层V3Popup实例详解
2021/01/04 Vue.js
web.py在模板中输出美元符号的方法
2014/08/26 Python
Python中使用PyQt把网页转换成PDF操作代码实例
2015/04/23 Python
利用Pandas和Numpy按时间戳将数据以Groupby方式分组
2019/07/22 Python
selenium携带cookies模拟登陆CSDN的实现
2021/01/19 Python
HTML里显示pdf、word、xls、ppt的方法示例
2020/04/14 HTML / CSS
美体小铺美国官网:The Body Shop美国
2017/11/10 全球购物
本科毕业生自荐信
2014/05/26 职场文书
放飞理想演讲稿
2014/09/09 职场文书
干部竞争上岗演讲稿
2014/09/11 职场文书
中学生的1000字检讨书
2014/10/11 职场文书
贵阳市党的群众路线教育实践活动党(工)委领导班子整改方案
2014/10/26 职场文书
大一新生检讨书
2014/10/29 职场文书
处级干部考察材料
2014/12/24 职场文书
手术室护士个人总结
2015/02/13 职场文书
考研英语辞职信
2015/05/13 职场文书
红色经典电影观后感
2015/06/18 职场文书
锦旗赠语
2015/06/23 职场文书
2015年三好一满意工作总结
2015/07/24 职场文书
nginx sticky实现基于cookie负载均衡示例详解
2022/12/24 Servers