国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 淺談keras中的后端backend及其相關函數(K.prod,K.cast)

淺談keras中的后端backend及其相關函數(K.prod,K.cast)

2020-06-29 12:05C小C Python

這篇文章主要介紹了淺談keras中的后端backend及其相關函數(K.prod,K.cast),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看

一、K.prod

prod

keras.backend.prod(x, axis=None, keepdims=False)

功能:在某一指定軸,計算張量中的值的乘積。

參數

x: 張量或變量。

axis: 一個整數需要計算乘積的軸。

keepdims: 布爾值,是否保留原尺寸。 如果 keepdims 為 False,則張量的秩減 1。 如果 keepdims 為 True,縮小的維度保留為長度 1。

返回

x 的元素的乘積的張量。

Numpy 實現

?
1
2
3
4
def prod(x, axis=None, keepdims=False):
  if isinstance(axis, list):
    axis = tuple(axis)
  return np.prod(x, axis=axis, keepdims=keepdims)

具體例子:

?
1
2
3
4
5
6
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)

【運行結果】

淺談keras中的后端backend及其相關函數(K.prod,K.cast)

二、K.cast

cast

keras.backend.cast(x, dtype)

功能:將張量轉換到不同的 dtype 并返回。

你可以轉換一個 Keras 變量,但它仍然返回一個 Keras 張量。

參數

x: Keras 張量(或變量)。

dtype: 字符串, ('float16', 'float32' 或 'float64')。

返回

Keras 張量,類型為 dtype。

例子

?
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> 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

一上來是一些說明

?
1
2
3
4
# the type of float to use throughout the session. 整個模塊都是用浮點型數據
_FLOATX = 'float32' # 數據類型為32位浮點型
_EPSILON = 1e-7 # 很小的常數
_IMAGE_DATA_FORMAT = 'channels_last' # 圖像數據格式 最后顯示通道,tensorflow格式

接下來看里面的一些函數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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,在終端可以直接輸出,如下:

淺談keras中的后端backend及其相關函數(K.prod,K.cast)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

該函數允許自定義值

淺談keras中的后端backend及其相關函數(K.prod,K.cast)

以string的形式返回默認的浮點類型:

?
1
2
3
4
5
6
7
8
9
10
11
12
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
淺談keras中的后端backend及其相關函數(K.prod,K.cast)

把numpy數組投影到默認的浮點類型:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)

默認數據格式、自定義數據格式和檢查數據格式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

剩余的關于維度順序和數據格式的方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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中引入了所有需要的東西

?
1
2
3
4
5
6
7
8
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。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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有關資料。

以上這篇淺談keras中的后端backend及其相關函數(K.prod,K.cast)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/C_chuxin/article/details/87919432

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品视频国产 | 精品一区二区三区中文字幕老牛 | 国产成人精品一区二区三区四区 | 国产成人精品免费视频 | 日韩欧美在线观看 | 91欧美激情一区二区三区成人 | 一区二区三区在线观看视频 | 一级片免费视频 | 亚洲国产免费 | 国产精品一区二区三区四区五区 | 成人看片免费 | 希岛爱理一区二区三区av高清 | 午夜伦理影院 | 热久久国产 | 国产精品不卡一区二区三区 | 日韩精品久久久久久 | 美女高潮久久久 | 欧洲一级毛片 | 国产精品久久久久久吹潮 | av久久| 久久综合五月 | 国产v日产∨综合v精品视频 | 日韩成人av电影 | 99精品热视频 | 亚洲国产一区二区在线观看 | 成人免费在线观看视频 | 久久精视频 | 日韩中文一区二区三区 | 中国黄色三级毛片 | 91亚洲国产| 欧美女人性| 91免费在线 | 成年人在线免费观看视频网站 | 欧美国产综合 | 91精品蜜臀在线一区尤物 | 久久精品国产久精国产 | 亚洲一区精品在线 | 亚洲国产成人久久 | 天堂资源网 | 日韩在线视频一区 | 亚洲欧美视频在线播放 |