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使用TensorFlow进行图像处理的方法
Feb 28 Python
不管你的Python报什么错,用这个模块就能正常运行
Sep 14 Python
用Python实现大文本文件切割的方法
Jan 12 Python
Python2和Python3的共存和切换使用
Apr 12 Python
python tkinter canvas 显示图片的示例
Jun 13 Python
python画图的函数用法以及技巧
Jun 28 Python
Python 实现将数组/矩阵转换成Image类
Jan 09 Python
详解Python3 中的字符串格式化语法
Jan 15 Python
Django中的模型类设计及展示示例详解
May 29 Python
基于pycharm实现批量修改变量名
Jun 02 Python
解决Python安装cryptography报错问题
Sep 03 Python
如何利用pygame实现打飞机小游戏
May 30 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/03 咖啡文化
用PHP发电子邮件
2006/10/09 PHP
完美解决:Apache启动问题―(OS 10022)提供了一个无效的参数
2013/06/08 PHP
PHP错误提示的关闭方法详解
2013/06/23 PHP
让ThinkPHP支持大小写url地址访问的方法
2014/10/31 PHP
Yii的Srbac插件用法详解
2016/07/14 PHP
Laravel框架Request、Response及Session操作示例
2019/05/06 PHP
jquery form表单提交插件asp.net后台中文解码
2010/06/12 Javascript
Json字符串转换为JS对象的高效方法实例
2013/05/01 Javascript
JS小功能(操作Table--动态添加删除表格及数据)实现代码
2013/11/28 Javascript
jquery修改网页背景颜色通过css方法实现
2014/06/06 Javascript
javascript实现的HashMap类代码
2014/06/27 Javascript
javascript中call apply 的应用场景
2015/04/16 Javascript
jQuery实现遍历复选框的方法示例
2017/03/06 Javascript
理顺8个版本vue的区别(小结)
2018/09/17 Javascript
webpack dll打包重复问题优化的解决
2018/10/10 Javascript
jQuery实现图片下载代码
2019/07/18 jQuery
layuiAdmin循环遍历展示商品图片列表的方法
2019/09/16 Javascript
JS实现打砖块游戏
2020/02/14 Javascript
[44:40]KG vs LGD 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/16 DOTA
Python抓取框架 Scrapy的架构
2016/08/12 Python
python中numpy基础学习及进行数组和矢量计算
2017/02/12 Python
Python正则捕获操作示例
2017/08/19 Python
使用python 3实现发送邮件功能
2018/06/15 Python
python使用pandas处理大数据节省内存技巧(推荐)
2019/05/05 Python
Pytorch 实现权重初始化
2019/12/31 Python
python等差数列求和公式前 100 项的和实例
2020/02/25 Python
Python3 操作 MySQL 插入一条数据并返回主键 id的实例
2020/03/02 Python
英国最大的运动营养公司之一:LA Muscle
2018/07/02 全球购物
Ajax请求总共有多少种Callback
2016/07/17 面试题
道德大讲堂实施方案
2014/05/14 职场文书
松材线虫病防治方案
2014/06/15 职场文书
环境科学专业教师求职信
2014/07/12 职场文书
小学生禁毒教育心得体会
2016/01/15 职场文书
Python趣味挑战之实现简易版音乐播放器
2021/05/28 Python
Nginx配置根据url参数重定向
2022/04/11 Servers