博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
keras用法
阅读量:7027 次
发布时间:2019-06-28

本文共 2536 字,大约阅读时间需要 8 分钟。

关于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.

 

转载于:https://www.cnblogs.com/lucky466/p/10259492.html

你可能感兴趣的文章
磁盘清理无法删除DUMP文件手工删
查看>>
Java线程:创建与启动
查看>>
ES配置文件中文版
查看>>
[IE&FireFox]JS兼容
查看>>
人生如牌
查看>>
Nodejs操作MongoDB数据库示例
查看>>
从算法原理,看推荐策略
查看>>
学习笔记TF060:图像语音结合,看图说话
查看>>
自定义控件 --- 电池icon
查看>>
嘻哈说:设计模式之工厂方法模式
查看>>
JS原生Ajax基本操作
查看>>
JS == 操作符的隐式转换,翻译ecma-262/5.1/#sec-11.6.1
查看>>
大三学生的第二个基于 React 框架的轮播图组件。
查看>>
工程实践:给函数取一个"好"的名字
查看>>
小猿圈python之九九乘法表、金字塔和杨辉三角
查看>>
说说如何使用 vue-router 插件
查看>>
警告忽略
查看>>
Java Bean + 注册验证
查看>>
通过mysql 插入一句话***
查看>>
centos 分区扩容
查看>>