浅谈keras中的后端backend及其相关函数(K.prod,K.cast)
作者:C小C 发布时间:2021-07-04 08:53:54
一、K.prod
prod
keras.backend.prod(x, axis=None, keepdims=False)
功能:在某一指定轴,计算张量中的值的乘积。
参数
x: 张量或变量。
axis: 一个整数需要计算乘积的轴。
keepdims: 布尔值,是否保留原尺寸。 如果 keepdims 为 False,则张量的秩减 1。 如果 keepdims 为 True,缩小的维度保留为长度 1。
返回
x 的元素的乘积的张量。
Numpy 实现
def prod(x, axis=None, keepdims=False):
if isinstance(axis, list):
axis = tuple(axis)
return np.prod(x, axis=axis, keepdims=keepdims)
具体例子:
import numpy as np
x=np.array([[2,4,6],[2,4,6]])
scaling = np.prod(x, axis=1, keepdims=False)
print(x)
print(scaling)
【运行结果】
二、K.cast
cast
keras.backend.cast(x, dtype)
功能:将张量转换到不同的 dtype 并返回。
你可以转换一个 Keras 变量,但它仍然返回一个 Keras 张量。
参数
x: Keras 张量(或变量)。
dtype: 字符串, ('float16', 'float32' 或 'float64')。
返回
Keras 张量,类型为 dtype。
例子
>>> from keras import backend as K
>>> input = K.placeholder((2, 3), dtype='float32')
>>> input
<tf.Tensor 'Placeholder_2:0' shape=(2, 3) dtype=float32>
# It doesn't work in-place as below.
>>> K.cast(input, dtype='float16')
<tf.Tensor 'Cast_1:0' shape=(2, 3) dtype=float16>
>>> input
<tf.Tensor 'Placeholder_2:0' shape=(2, 3) dtype=float32>
# you need to assign it.
>>> input = K.cast(input, dtype='float16')
>>> input
<tf.Tensor 'Cast_2:0' shape=(2, 3) dtype=float16>
补充知识:keras源码之backend库目录
backend库目录
先看common.py
一上来是一些说明
# the type of float to use throughout the session. 整个模块都是用浮点型数据
_FLOATX = 'float32' # 数据类型为32位浮点型
_EPSILON = 1e-7 # 很小的常数
_IMAGE_DATA_FORMAT = 'channels_last' # 图像数据格式 最后显示通道,tensorflow格式
接下来看里面的一些函数
def epsilon():
"""Returns the value of the fuzz factor used in numeric expressions.
返回数值表达式中使用的模糊因子的值
# Returns
A float.
# Example
```python
>>> keras.backend.epsilon()
1e-07
```
"""
return _EPSILON
该函数定义了一个常量,值为1e-07,在终端可以直接输出,如下:
def set_epsilon(e):
"""Sets the value of the fuzz factor used in numeric expressions.
# Arguments
e: float. New value of epsilon.
# Example
```python
>>> from keras import backend as K
>>> K.epsilon()
1e-07
>>> K.set_epsilon(1e-05)
>>> K.epsilon()
1e-05
```
"""
global _EPSILON
_EPSILON = e
该函数允许自定义值
以string的形式返回默认的浮点类型:
def floatx():
"""Returns the default float type, as a string.
(e.g. 'float16', 'float32', 'float64').
# Returns
String, the current default float type.
# Example
```python
>>> keras.backend.floatx()
'float32'
```
"""
return _FLOATX
把numpy数组投影到默认的浮点类型:
def cast_to_floatx(x):
"""Cast a Numpy array to the default Keras float type.把numpy数组投影到默认的浮点类型
# Arguments
x: Numpy array.
# Returns
The same Numpy array, cast to its new type.
# Example
```python
>>> from keras import backend as K
>>> K.floatx()
'float32'
>>> arr = numpy.array([1.0, 2.0], dtype='float64')
>>> arr.dtype
dtype('float64')
>>> new_arr = K.cast_to_floatx(arr)
>>> new_arr
array([ 1., 2.], dtype=float32)
>>> new_arr.dtype
dtype('float32')
```
"""
return np.asarray(x, dtype=_FLOATX)
默认数据格式、自定义数据格式和检查数据格式:
def image_data_format():
"""Returns the default image data format convention ('channels_first' or 'channels_last').
# Returns
A string, either `'channels_first'` or `'channels_last'`
# Example
```python
>>> keras.backend.image_data_format()
'channels_first'
```
"""
return _IMAGE_DATA_FORMAT
def set_image_data_format(data_format):
"""Sets the value of the data format convention.
# Arguments
data_format: string. `'channels_first'` or `'channels_last'`.
# Example
```python
>>> from keras import backend as K
>>> K.image_data_format()
'channels_first'
>>> K.set_image_data_format('channels_last')
>>> K.image_data_format()
'channels_last'
```
"""
global _IMAGE_DATA_FORMAT
if data_format not in {'channels_last', 'channels_first'}:
raise ValueError('Unknown data_format:', data_format)
_IMAGE_DATA_FORMAT = str(data_format)
def normalize_data_format(value):
"""Checks that the value correspond to a valid data format.
# Arguments
value: String or None. `'channels_first'` or `'channels_last'`.
# Returns
A string, either `'channels_first'` or `'channels_last'`
# Example
```python
>>> from keras import backend as K
>>> K.normalize_data_format(None)
'channels_first'
>>> K.normalize_data_format('channels_last')
'channels_last'
```
# Raises
ValueError: if `value` or the global `data_format` invalid.
"""
if value is None:
value = image_data_format()
data_format = value.lower()
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('The `data_format` argument must be one of '
'"channels_first", "channels_last". Received: ' +
str(value))
return data_format
剩余的关于维度顺序和数据格式的方法:
def set_image_dim_ordering(dim_ordering):
"""Legacy setter for `image_data_format`.
# Arguments
dim_ordering: string. `tf` or `th`.
# Example
```python
>>> from keras import backend as K
>>> K.image_data_format()
'channels_first'
>>> K.set_image_data_format('channels_last')
>>> K.image_data_format()
'channels_last'
```
# Raises
ValueError: if `dim_ordering` is invalid.
"""
global _IMAGE_DATA_FORMAT
if dim_ordering not in {'tf', 'th'}:
raise ValueError('Unknown dim_ordering:', dim_ordering)
if dim_ordering == 'th':
data_format = 'channels_first'
else:
data_format = 'channels_last'
_IMAGE_DATA_FORMAT = data_format
def image_dim_ordering():
"""Legacy getter for `image_data_format`.
# Returns
string, one of `'th'`, `'tf'`
"""
if _IMAGE_DATA_FORMAT == 'channels_first':
return 'th'
else:
return 'tf'
在common.py之后有三个backend,分别是cntk,tensorflow和theano。
__init__.py
首先从common.py中引入了所有需要的东西
from .common import epsilon
from .common import floatx
from .common import set_epsilon
from .common import set_floatx
from .common import cast_to_floatx
from .common import image_data_format
from .common import set_image_data_format
from .common import normalize_data_format
接下来是检查环境变量与配置文件,设置backend和format,默认的backend是tensorflow。
# Set Keras base dir path given KERAS_HOME env variable, if applicable.
# Otherwise either ~/.keras or /tmp.
if 'KERAS_HOME' in os.environ: # 环境变量
_keras_dir = os.environ.get('KERAS_HOME')
else:
_keras_base_dir = os.path.expanduser('~')
if not os.access(_keras_base_dir, os.W_OK):
_keras_base_dir = '/tmp'
_keras_dir = os.path.join(_keras_base_dir, '.keras')
# Default backend: TensorFlow. 默认后台是TensorFlow
_BACKEND = 'tensorflow'
# Attempt to read Keras config file.读取keras配置文件
_config_path = os.path.expanduser(os.path.join(_keras_dir, 'keras.json'))
if os.path.exists(_config_path):
try:
with open(_config_path) as f:
_config = json.load(f)
except ValueError:
_config = {}
_floatx = _config.get('floatx', floatx())
assert _floatx in {'float16', 'float32', 'float64'}
_epsilon = _config.get('epsilon', epsilon())
assert isinstance(_epsilon, float)
_backend = _config.get('backend', _BACKEND)
_image_data_format = _config.get('image_data_format',
image_data_format())
assert _image_data_format in {'channels_last', 'channels_first'}
set_floatx(_floatx)
set_epsilon(_epsilon)
set_image_data_format(_image_data_format)
_BACKEND = _backend
之后的tensorflow_backend.py文件是一些tensorflow中的函数说明,详细内容请参考tensorflow有关资料。
来源:https://blog.csdn.net/C_chuxin/article/details/87919432


猜你喜欢
- 今天分享一下Django实现的简单的文件上传的小例子。步骤 •创建Django项目,创建Django应用 •设计模型&n
- 本文实例讲述了Python打印scrapy蜘蛛抓取树结构的方法。分享给大家供大家参考。具体如下:通过下面这段代码可以一目了然的知道scrap
- auto-push-oss Action虽然在 Github 市场有推送 OSS 相关的 Action,但是我还是选择改造我运行了好多年的脚
- 背景大家好,我是J哥。我们常常面临着大量的重复性工作,通过人工方式处理往往耗时耗力易出错。而Python在办公自动化方面具有天然优势,分分钟
- 代码如下import unittestdir = "D:\\work_doc\\pycharm2\\python_Basics&q
- 简介到目前为止,我们查阅anaconda的官网可发现,由于目前Anaconda没有支持arm架构的版本,在M1芯片Mac上安装的Anacon
- 属性多层数组数据的添加修改为什么需要使用Vue.set?vue中不能检测到数组的一些变化比如一下两种:1、数组长度的变化 vm.arr.le
- 今天在写一个linux下自动备份指定目录下的所有目录的脚本时,遇到了一个问题,由于我是需要备份目录,所以,需要判断扫描的文件是否为目录,当我
- django实现多种支付方式'''#思路我们希望,通过插拔的方式来实现多方式登录,比如新增一种支付方式,那么只要在项
- 本文针对Python time模块进行分类学习,希望对大家的学习有所帮助。一.壁挂钟时间1.time()time模块的核心函数time(),
- 本文实例讲述了MySQL 的启动和连接方式。分享给大家供大家参考,具体如下:MySQL运行包括两部分,一部分是服务器端程序mysqld,另外
- 前言Helium工具是对Selenium的封装,将Selenium工具的使用变得更加简单。Selenium虽然好,但是在它的使用过程中元素的
- 想大家都做过遮罩层这种常见的功能,css或jquery实现,实现方式多样化,这里http://我介绍我在项目中实现的方式,全屏遮罩,部分区域
- 问题你有50枚金币,需要分配给以下几个人:Matthew,Sarah,Augustus,Heidi,Emilie,Peter,Giana,A
- 踩坑记录:用pandas来做csv的缺失值处理时候发现奇怪BUG,就是excel打开csv文件,明明有的格子没有任何东西,当然,我就想到用p
- 一、pyqt5中动画的继承关系图二、关于QAbstractAnimation父类的认识1、主要作用继承此类, 实现一些自定义动画所有动画共享
- 例如数据 列Namename abcd最后的结果a*b*c*d*declare @test table( namevarchar(10))&
- 一、if语句if 语句让你能够检查程序的当前状态,并据此采取相应的措施。if语句可应用于列表,以另一种方式处理列表中的大多数元素,以及特定值
- 一、前言我们在使用pycharm写代码时可能出现过下面这种情况,不小心点到了ignore ....:这样会导致整个代码都没有错误提示了,类似
- 工作中需要从一个数据库中的表GIS_WEICHAI_DATA_1S中的数据导入到另个一数据库的表GIS_WEICHAI_DATA_1S中,数