最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談keras中的keras.utils.to_categorical用法

 更新時間:2020年07月02日 09:45:28   作者:李上花開  
這篇文章主要介紹了淺談keras中的keras.utils.to_categorical用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

to_categorical(y, num_classes=None, dtype='float32')

將整型標簽轉(zhuǎn)為onehot。y為int數(shù)組,num_classes為標簽類別總數(shù),大于max(y)(標簽從0開始的)。

返回:如果num_classes=None,返回len(y) * [max(y)+1](維度,m*n表示m行n列矩陣,下同),否則為len(y) * num_classes。說出來顯得復雜,請看下面實例。

import keras

ohl=keras.utils.to_categorical([1,3])
# ohl=keras.utils.to_categorical([[1],[3]])
print(ohl)
"""
[[0. 1. 0. 0.]
 [0. 0. 0. 1.]]
"""
ohl=keras.utils.to_categorical([1,3],num_classes=5)
print(ohl)
"""
[[0. 1. 0. 0. 0.]
 [0. 0. 0. 1. 0.]]
"""

該部分keras源碼如下:

def to_categorical(y, num_classes=None, dtype='float32'):
  """Converts a class vector (integers) to binary class matrix.

  E.g. for use with categorical_crossentropy.

  # Arguments
    y: class vector to be converted into a matrix
      (integers from 0 to num_classes).
    num_classes: total number of classes.
    dtype: The data type expected by the input, as a string
      (`float32`, `float64`, `int32`...)

  # Returns
    A binary matrix representation of the input. The classes axis
    is placed last.
  """
  y = np.array(y, dtype='int')
  input_shape = y.shape
  if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
    input_shape = tuple(input_shape[:-1])
  y = y.ravel()
  if not num_classes:
    num_classes = np.max(y) + 1
  n = y.shape[0]
  categorical = np.zeros((n, num_classes), dtype=dtype)
  categorical[np.arange(n), y] = 1
  output_shape = input_shape + (num_classes,)
  categorical = np.reshape(categorical, output_shape)
  return categorical

補充知識:keras筆記——keras.utils.to_categoracal()函數(shù)

keras.utils.to_categoracal (y, num_classes=None, dtype='float32')

將整形標簽轉(zhuǎn)為onehot,y為int數(shù)組,num_classes為標簽類別總數(shù),大于max (y),(標簽從0開始的)。

返回:

如果num_classes=None, 返回 len(y)*[max(y)+1] (維度,m*n表示m行n列矩陣),否則為len(y)*num_classes。

以上這篇淺談keras中的keras.utils.to_categorical用法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

剑阁县| 晋江市| 麦盖提县| 玉山县| 罗城| 内乡县| 惠东县| 梧州市| 腾冲县| 泸水县| 财经| 健康| 明光市| 黑龙江省| 温州市| 浦城县| 温泉县| 独山县| 舟曲县| 甘孜县| 长汀县| 五寨县| 扶风县| 崇仁县| 乌海市| 盐源县| 云和县| 奎屯市| 诸暨市| 贵港市| 大安市| 金堂县| 崇礼县| 大渡口区| 拜泉县| 九龙县| 喜德县| 保亭| 聊城市| 延川县| 尉氏县|