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 相关文章推荐
Python3基础之函数用法
Aug 13 Python
Python删除空文件和空文件夹的方法
Jul 14 Python
Python中pygame的mouse鼠标事件用法实例
Nov 11 Python
Python实现定时执行任务的三种方式简单示例
Mar 30 Python
详解【python】str与json类型转换
Apr 29 Python
完美解决python3.7 pip升级 拒绝访问问题
Jul 12 Python
python爬虫 2019中国好声音评论爬取过程解析
Aug 26 Python
Django配置MySQL数据库的完整步骤
Sep 07 Python
python系统指定文件的查找只输出目录下所有文件及文件夹
Jan 19 Python
Django 自定义权限管理系统详解(通过中间件认证)
Mar 11 Python
python collections模块的使用
Oct 16 Python
Python使用pandas导入csv文件内容的示例代码
Dec 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
如何提高MYSQL数据库的查询统计速度 select 索引应用
2007/04/11 PHP
php中用foreach来操作数组的代码
2011/07/17 PHP
PHP序列号生成函数和字符串替换函数代码
2012/06/07 PHP
PHP版 汉字转码的实现详解
2013/06/09 PHP
Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法
2018/08/16 PHP
php实例化一个类的具体方法
2019/09/19 PHP
jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
2010/06/28 Javascript
myFocus slide3D v1.1.0 使用方法与下载
2011/01/12 Javascript
javascript中注册和移除事件的4种方式
2013/03/20 Javascript
jquery中常用的SET和GET$(”#msg”).html循环介绍
2013/10/09 Javascript
javascript生成随机颜色示例代码
2014/05/05 Javascript
JavaScript基础重点(必看)
2016/07/09 Javascript
AngularJs Injecting Services Into Controllers详解
2016/09/02 Javascript
使用 Vue 绑定单个或多个 Class 名的实例代码
2018/01/08 Javascript
vue实现简单loading进度条
2018/06/06 Javascript
Vue表单及表单绑定方法
2018/09/04 Javascript
js实现掷骰子小游戏
2019/10/24 Javascript
深入理解python中函数传递参数是值传递还是引用传递
2017/11/07 Python
python之matplotlib学习绘制动态更新图实例代码
2018/01/23 Python
python jieba分词并统计词频后输出结果到Excel和txt文档方法
2018/02/11 Python
Python3+django2.0+apache2+ubuntu14部署网站上线的方法
2018/07/07 Python
django框架模板中定义变量(set variable in django template)的方法分析
2019/06/24 Python
Python closure闭包解释及其注意点详解
2019/08/28 Python
使用pandas实现连续数据的离散化处理方式(分箱操作)
2019/11/22 Python
对python中 math模块下 atan 和 atan2的区别详解
2020/01/17 Python
tensorflow之并行读入数据详解
2020/02/05 Python
Python数据正态性检验实现过程
2020/04/18 Python
使用python+poco+夜神模拟器进行自动化测试实例
2020/04/23 Python
德国运动营养和健身网上商店:Myprotein.de
2018/07/18 全球购物
加拿大在线眼镜零售商:SmartBuyGlasses加拿大
2019/05/25 全球购物
公司庆典活动邀请函
2014/01/09 职场文书
小学三八妇女节活动方案
2014/03/16 职场文书
《穷人》教学反思
2014/04/08 职场文书
地球一小时宣传标语
2014/06/24 职场文书
改作风抓落实促发展心得体会
2014/09/10 职场文书
特此通知格式
2015/04/27 职场文书