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实现简单socket程序在两台电脑之间传输消息的方法
Mar 13 Python
wxPython之解决闪烁的问题
Jan 15 Python
PyTorch快速搭建神经网络及其保存提取方法详解
Apr 28 Python
Python调用C++,通过Pybind11制作Python接口
Oct 16 Python
使用Python实现从各个子文件夹中复制指定文件的方法
Oct 25 Python
关于Pycharm无法debug问题的总结
Jan 19 Python
python实现飞机大战游戏(pygame版)
Oct 26 Python
python读取多层嵌套文件夹中的文件实例
Feb 27 Python
OpenCV+python实现膨胀和腐蚀的示例
Dec 21 Python
python 写一个水果忍者游戏
Jan 13 Python
Django cookie和session的应用场景及如何使用
Apr 29 Python
Python基础之元编程知识总结
May 23 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与MySQL交互使用详解
2006/10/09 PHP
浅析PHP 按位与或 (^ 、&)
2013/06/21 PHP
thinkphp中session和cookie无效的解决方法
2014/12/19 PHP
PHP获取远程图片并保存到本地的方法
2015/05/12 PHP
跨域表单提交状态的变相判断代码
2009/11/12 Javascript
JS中如何设置readOnly的值
2013/12/25 Javascript
jquery中post方法用法实例
2014/10/21 Javascript
Javascript的闭包详解
2014/12/26 Javascript
jquery实现页面关键词高亮显示的方法
2015/03/12 Javascript
JavaScript的事件代理和委托实例分析
2015/03/25 Javascript
jquery+ajax请求且带返回值的代码
2015/08/12 Javascript
jQuery siblings()用法实例详解
2016/04/26 Javascript
JavaScript toUpperCase()方法使用详解
2016/08/26 Javascript
layer弹出层中H5播放器全屏出错的解决方法
2017/02/21 Javascript
JS设计模式之命令模式概念与用法分析
2018/02/06 Javascript
JS简单获取并修改input文本框内容的方法示例
2018/04/08 Javascript
vue配置请求本地json数据的方法
2018/04/11 Javascript
浅谈ECMAScript 中的Array类型
2019/06/10 Javascript
从零使用TypeScript开发项目打包发布到npm
2020/02/14 Javascript
JavaScript中的各种宽高属性的实现
2020/05/08 Javascript
vue移动端弹起蒙层滑动禁止底部滑动操作
2020/07/22 Javascript
[36:45]TNC vs VGJ.S 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
Python实现桶排序与快速排序算法结合应用示例
2017/11/22 Python
Python3随机漫步生成数据并绘制
2018/08/27 Python
Python django框架应用中实现获取访问者ip地址示例
2019/05/17 Python
python函数修饰符@的使用方法解析
2019/09/02 Python
Python如何基于smtplib发不同格式的邮件
2019/12/30 Python
Django更新models数据库结构步骤
2020/04/01 Python
keras 读取多标签图像数据方式
2020/06/12 Python
Scrapy项目实战之爬取某社区用户详情
2020/09/17 Python
测试工程师程序员求职信范文
2014/02/20 职场文书
2014年药店店长工作总结
2014/11/17 职场文书
青岛海底世界导游词
2015/02/11 职场文书
网络销售员岗位职责
2015/04/11 职场文书
2015年四年级班主任工作总结
2015/10/22 职场文书
死磕 java同步系列之synchronized解析
2021/06/28 Java/Android