keras用法
关于Keras的“层”(Layer)
所有的Keras层对象都有如下方法:
layer.get_weights():返回层的权重(numpy array)layer.set_weights(weights):从numpy array中将权重加载到该层中,要求numpy array的形状与*layer.get_weights()的形状相同layer.get_config():返回当前层配置信息的字典,层也可以借由配置信息重构:
Input(shape=None,batch_shape=None,name=None,dtype=K.floatx(),sparse=False,tensor=None)
Input():用来实例化一个keras张量
keras张量是来自底层后端(Theano或Tensorflow)的张量对象,我们增加了某些属性,使我们通过知道模型的输入和输出来构建keras模型。
添加的keras属性有:1)._keras_shape:整型的形状元组通过keras-side 形状推理传播 2)._keras_history: 最后一层应用于张量,整个图层的图可以从那个层,递归地检索出来。
#参数:
shape: 形状元组(整型),不包括batch size。for instance, shape=(32,) 表示了预期的输入将是一批32维的向量。
batch_shape: 形状元组(整型),包括了batch size。for instance, batch_shape=(10,32)表示了预期的输入将是10个32维向量的批次。
name: 对于该层是可选的名字字符串。在一个模型中是独一无二的(同一个名字不能复用2次)。如果name没有被特指将会自动生成。
dtype: 预期的输入数据类型
sparse: 特定的布尔值,占位符是否为sparse
tensor: 可选的存在的向量包装到Input层,如果设置了,该层将不会创建一个占位张量。
#返回
一个张量
#例子
x=Input(shape=(32,))
y=Dense(16,activation='softmax')(x)
model=Model(x,y)
keras里面一些常用的单元:
import keras.layers as KL
二维卷积:
KL.Conv2d()
KL.Activation()
Definition : Activation(self, activation, **kwargs)
举例: x = KL.Activation('relu')(x)
KL.Add()
举例:
x = KL.Add()(shortcut,x)
ayer that adds a list of inputs.
It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).
Definition : KL.ZeroPadding2D(padding=(1, 1), data_format=None, **kwargs)
class BatchNorm(KL.BatchNormalization):
"""Extends the Keras BatchNormalization class to allow a central place
to make changes if needed. Batch normalization has a negative effect on training if batches are small
so this layer is often frozen (via setting in Config class) and functions
as linear layer.
"""
def call(self, inputs, training=None):
"""
Note about training values:
None: Train BN layers. This is the normal mode
False: Freeze BN layers. Good when batch size is small
True: (don't use). Set layer in training mode even when making inferences
"""
return super(self.__class__, self).call(inputs, training=training)
代码:
对super(self.__calss__,self)中的self.__class__
self、 superclass 、 super
self : 当前方法的调用者
class:获取方法调用者的类对象
superclass:获取方法调用者的父类对象
class BatchNorm(BatchNormalization):
Extends the Keras BatchNormalization class to allow a central place to make changes if needed.
Batch normalization has a negative effect on training if batches are small so this layer is often frozen (via setting in Config class) and functions as linear layer.
keras用法的更多相关文章
- 100天搞定机器学习|day39 Tensorflow Keras手写数字识别
提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...
- 我的Keras使用总结(5)——Keras指定显卡且限制显存用量,常见函数的用法及其习题练习
Keras 是一个高层神经网络API,Keras是由纯Python编写而成并基于TensorFlow,Theano以及CNTK后端.Keras为支持快速实验而生,能够将我们的idea迅速转换为结果.好 ...
- TensorFlow Keras API用法
TensorFlow Keras API用法 Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,可以编译和拟 ...
- Keras class_weight和sample_weight用法
搬运: https://stackoverflow.com/questions/57610804/when-is-the-timing-to-use-sample-weights-in-keras i ...
- TensorFlow从0到1之TensorFlow Keras及其用法(25)
Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,你可以编译和拟合模型.之后,它可以用于预测.变量声明.占位 ...
- keras中TimeDistributed的用法
TimeDistributed这个层还是比较难理解的.事实上通过这个层我们可以实现从二维像三维的过渡,甚至通过这个层的包装,我们可以实现图像分类视频分类的转化. 考虑一批32个样本,其中每个样本是一个 ...
- Keras 学习之旅(一)
软件环境(Windows): Visual Studio Anaconda CUDA MinGW-w64 conda install -c anaconda mingw libpython CNTK ...
- Keras官方中文文档:函数式模型API
\ 函数式模型接口 为什么叫"函数式模型",请查看"Keras新手指南"的相关部分 Keras的函数式模型为Model,即广义的拥有输入和输出的模型,我们使用M ...
- Keras官方中文文档:序贯模型API
Sequential模型接口 如果刚开始学习Sequential模型,请首先移步这里阅读文档,本节内容是Sequential的API和参数介绍. 常用Sequential属性 model.layers ...
随机推荐
- idea创建的java web项目打包发布到tomcat
File-> Project Structure... 点击Artifacts->+号 点击Web Application:Archive->选择与你的项目对应的那一项Artifac ...
- 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'goodsController' defined in file [D:\eclipse\eclipse-space\pinyougou_parent\pinyou
由于错误太宽,没法截取完整的,所以不怎么连贯,但是不影响错误的解决. 这个错误是因为service无法自动注入.显示嵌套状态异常. 我就查看了一下我的坐标和配置文件,配置文件的路径和访问地址都是正确的 ...
- Linux下批量修改后缀名
1.用find和xargs添加后缀名 [root@node99 yum.repos.d]# ls -ltr total 32 -rw-r--r--. 1 root root 5701 Nov 23 2 ...
- box-sizing (摘录)
//http://www.jianshu.com/p/e2eb0d8c9de6 box-sizing其它的值 content-box 描述:在宽度和高度之外绘制元素的内边距和边框. border-bo ...
- 是否能设计一种DNN的特定网络结构来改善DNN,使得其学习起来更加高效
小结: 1. 是否能设计一种DNN的特定网络结构来改善DNN,使得其学习起来更加高效 https://mp.weixin.qq.com/s/lF_WLAn6JyQqf10076hsjA Deep &a ...
- 6. Scala面向对象编程(基础部分)
6.1 基本介绍 6.1.1 Scala语言是面向对象的 1) Java时面向对象的编程语言,由于历史原因,Java中海存在着非面向对象的内容:基本类型,null,静态方法等 2) Scala语言来自 ...
- Leetcode: Max Consecutive Ones II(unsolved locked problem)
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- 北京大学Cousera学习笔记--2-计算导论与C语言基础-第一讲.计算机的基本原理-图灵机
有限状态读写头从一个初始状态开始,对存储器上的输入数据进行读或写操作,经过有限步操作之后停机,此时存储器上的输出数据就是计算结果 (1) 图灵机的构成: 1.一条存储带:双向无限延长:上有一个个的小方 ...
- 安装Oracle Grid的过程中用到的几个小技巧
1.利用文件来模拟块设备 在grid的安装教程中有一步是 provision the disk devices for use with ASM Filter Driver.但是如果我们没有多个磁盘怎 ...
- vim中自动格式化代码
1,gg 跳转到第一行 2,shift+v 转到可视模式 3,shift+g 全选 4,按下神奇的 =