keras.layer.input()用法说明
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()用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【DB宝14】在Docker中只需2步即可拥有Oracle 11g企业版环境(11.2.0.4)
- 别忘了给gcc编译器工具链加上-fno-common选项
- 轻量安全的部署方案
- 算法集锦(34) | 强化学习| 出租车载客问题
- 前端测试题:(解析)关于ajax跨域的说法,下面错误的是?
- 什么才是定制化 IDE 的核心价值?
- RTOS内功修炼记(八)— CMSIS RTOS API,内核通用API接口
- 从零搭建Spring Boot脚手架(1):开篇以及技术选型
- 盘点 Python 10 大常用数据结构(下篇)
- 「干货」你需要了解的六种渲染模式
- 分享两个操作Java枚举的实用方法
- 从零开始强化学习:在Python笔记本中设计和解决一个任务
- 基于深度学习的新闻推荐算法(1)
- 以复现图表的方式来学习一篇文章
- 盘点 Python 10 大常用数据结构(上篇)