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字符串排序方法
Aug 29 Python
Python使用MySQLdb for Python操作数据库教程
Oct 11 Python
Python RuntimeError: thread.__init__() not called解决方法
Apr 28 Python
python使用append合并两个数组的方法
Apr 28 Python
详解字典树Trie结构及其Python代码实现
Jun 03 Python
python中安装模块包版本冲突问题的解决
May 02 Python
Python 从相对路径下import的方法
Dec 04 Python
基于python判断目录或者文件代码实例
Nov 29 Python
Python实现疫情通定时自动填写功能(附代码)
May 27 Python
Python3使用 GitLab API 进行批量合并分支
Oct 15 Python
python3代码输出嵌套式对象实例详解
Dec 03 Python
详解修改Anaconda中的Jupyter Notebook默认工作路径的三种方式
Jan 24 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
ThinkPHP中的create方法与自动令牌验证实例教程
2014/08/22 PHP
php自定义分页类完整实例
2015/12/25 PHP
基于php实现的验证码小程序
2016/12/13 PHP
Laravel 自带的Auth验证登录方法
2019/09/30 PHP
关于 byval 与 byref 的区别分析总结
2007/10/08 Javascript
基于JQuery实现CheckBox全选全不选
2011/06/27 Javascript
向JavaScript的数组中添加元素的方法小结
2015/10/24 Javascript
JavaScript获取function所有参数名的方法
2015/10/30 Javascript
Atitit.js的键盘按键事件捆绑and事件调度
2016/04/01 Javascript
js判断价格,必须为数字且不能为负数的实现方法
2016/10/07 Javascript
Vue.js中数据绑定的语法教程
2017/06/02 Javascript
jQuery Validate表单验证插件实现代码
2017/06/08 jQuery
深入理解nodejs搭建静态服务器(实现命令行)
2019/02/05 NodeJs
JavaScript实现多个物体同时运动
2020/03/12 Javascript
jquery实现拖拽小方块效果
2020/12/10 jQuery
[03:31]DOTA2英雄基础教程 大地之灵
2013/12/17 DOTA
[00:56]2014DOTA2国际邀请赛 DK、iG 赛前探访
2014/07/10 DOTA
Python中无限元素列表的实现方法
2014/08/18 Python
分析在Python中何种情况下需要使用断言
2015/04/01 Python
极简的Python入门指引
2015/04/01 Python
Python的re模块正则表达式操作
2016/05/25 Python
Python Nose框架编写测试用例方法
2017/10/26 Python
Python判断一个三位数是否为水仙花数的示例
2018/11/13 Python
树莓派使用python-librtmp实现rtmp推流h264的方法
2019/07/22 Python
Python selenium页面加载慢超时的解决方案
2020/03/18 Python
Django-rest-framework中过滤器的定制实例
2020/04/01 Python
美国的Eastbay旗下的运动款子品牌:Final-Score
2018/01/01 全球购物
TUMI香港官网:国际领先的行李箱、背囊品牌
2021/03/01 全球购物
通信工程专业女生个人求职信
2013/09/21 职场文书
文言文形式的学生求职信
2013/12/03 职场文书
个人自我评价和职业目标
2014/01/24 职场文书
十佳中学生事迹材料
2014/06/02 职场文书
消防工作实施方案
2014/06/09 职场文书
2014旅游局领导班子四风问题对照检查材料思想汇报
2014/09/19 职场文书
暑期社会实践个人总结
2015/03/06 职场文书
选对餐饮营销策略,营业额才会上涨
2019/08/27 职场文书