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

tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進行相互轉(zhuǎn)化

 更新時間:2023年06月25日 10:36:21   作者:無敵右腦  
這篇文章主要介紹了tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進行相互轉(zhuǎn)化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進行相互轉(zhuǎn)化

tensorflow自帶one_hot標(biāo)簽函數(shù)

tensorflow 有封裝好的函數(shù)可以直接將整數(shù)標(biāo)簽轉(zhuǎn)化為one_hot標(biāo)簽

import tensorflow as tf?
label = [1,0,2,3,0]
y = tf.one_hot(label, depth=4).numpy() ? ?
#使用ont_hot返回的是tensor張量,再用numpy取出數(shù)組
print(y)

得到:

[[0. 1. 0. 0.]
[1. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]
[1. 0. 0. 0.]]

將one-hot轉(zhuǎn)化為數(shù)字標(biāo)簽

將one-hot轉(zhuǎn)化為數(shù)字標(biāo)簽沒有封裝好的函數(shù),可以遍歷并使用soft argmax取出整數(shù):

label = [np.argmax(i) for i in y]?
print(label)

得到:

[1, 0, 2, 3, 0]

tensorflow中one_hot講解以及多分類標(biāo)簽與one-hot轉(zhuǎn)換

TensorFlow的one-hot函數(shù)講解

import tensorflow as tf
tf.one_hot(indices, depth, on_value, off_value, axis)

indices是一個列表,指定張量中獨熱向量的獨熱位置,或者說indeces是非負(fù)整數(shù)表示的標(biāo)簽列表。len(indices)就是分類的類別數(shù)。

tf.one_hot返回的張量的階數(shù)為indeces的階數(shù)+1。

當(dāng)indices的某個分量取-1時,即對應(yīng)的向量沒有獨熱值。

  • depth是每個獨熱向量的維度
  • on_value是獨熱值
  • off_value是非獨熱值

axis指定第幾階為depth維獨熱向量,默認(rèn)為-1,即,指定張量的最后一維為獨熱向量

例如:對于一個2階張量而言,axis=0時,即,每個列向量是一個獨熱的depth維向量

axis=1時,即,每個行向量是一個獨熱的depth維向量。axis=-1,等價于axis=1

import tensorflow as tf
# 得到4個5維獨熱行向量向量,
# ? ?其中第1個向量的第0個分量是獨熱1,
# ? ?第2個向量的第2個分量是獨熱,
# ? ?第3個向量沒有獨熱,因為指定為-1
# ? ?第4個向量的第1個分量為獨熱
# labels向targets的轉(zhuǎn)變
labels = [0, 2, -1, 1]
# labels是shape=(4,)的張量。則返回的targets是shape=(len(labels), depth)張量。
# 且這種情況下,axis=-1等價于axis=1
targets = tf.one_hot(indices=labels, depth=5, on_value=1.0, off_value=0.0, axis=-1)
with tf.Session() as sess:
? ? print(sess.run(targets))
[[ 1. ?0. ?0. ?0. ?0.]
?[ 0. ?0. ?1. ?0. ?0.]
?[ 0. ?0. ?0. ?0. ?0.]
?[ 0. ?1. ?0. ?0. ?0.]]
# 得到1個5維獨熱行向量。
targets = tf.one_hot(indices=3, depth=5, on_value=1.0, off_value=0.0, axis=0)
with tf.Session() as sess:
? ? ?print(sess.run(targets))
[ 0. ?0. ?0. ?1. ?0.]
# 得到1個5維獨熱列向量
targets = tf.one_hot(indices=[3], depth=5, on_value=1.0, off_value=0.0, axis=0)
with tf.Session() as sess:
? ? ?print(sess.run(targets))
[[ 0.]
[ 0.]
[ 0.]
[ 1.]
[ 0.]]
targets = tf.one_hot(indices=[[0,1],[1,0]], depth=3)
with tf.Session() as sess:
? ? print(sess.run(targets))
[[[ 1. ?0. ?0.]
? [ 0. ?1. ?0.]]
?[[ 0. ?1. ?0.]
? [ 1. ?0. ?0.]]]

注:indices如果是n階張量,則返回的one-hot張量則為n+1階張量

在實際神經(jīng)網(wǎng)絡(luò)的設(shè)計應(yīng)用中,給定的labels通常是數(shù)字列表,以標(biāo)識樣本屬于哪一個分類。類別數(shù)則是獨熱向量的維數(shù)。

# 得到分類的獨熱向量
targets = tf.one_hot(labels, num_classes)

TensorFlow 多分類標(biāo)簽轉(zhuǎn)換成One-hot

在處理多分類問題時,將多分類標(biāo)簽轉(zhuǎn)成One-hot編碼是一種很常見的手段,以下即為Tensorflow將標(biāo)簽轉(zhuǎn)成One-hot的tensor。以Mnist為例,如果標(biāo)簽為“3”,則One-hot編碼為[0,0,0,1,0,0,0,0,0,0].

import tensorflow as tf ?# version : '1.12.0'
NUM_CLASSES = 10 # 10分類
labels = [0,1,2,3] # sample label
batch_size = tf.size(labels) # get size of labels : 4
labels = tf.expand_dims(labels, 1) # 增加一個維度
indices = tf.expand_dims(tf.range(0, batch_size,1), 1) #生成索引
concated = tf.concat([indices, labels] , 1) #作為拼接
onehot_labels = tf.sparse_to_dense(concated, tf.stack([batch_size, NUM_CLASSES]), 1.0, 0.0) # 生成one-hot編碼的標(biāo)簽

將稀疏矩陣轉(zhuǎn)換成密集矩陣,其中索引在concated中,值為1.其他位置的值為默認(rèn)值0.

方法1:

from sklearn.preprocessing import OneHotEncoder,LabelEncoder
a = ['A','B','A','C']
label_encoder=LabelEncoder()
label_value = label_encoder.fit_transform(a)
enc = OneHotEncoder()
one_hot=enc.fit_transform(label_value.reshape(-1,1))
one_hot.toarray()
array([[1., 0., 0.],
? ? ? ?[0., 1., 0.],
? ? ? ?[1., 0., 0.],
? ? ? ?[0., 0., 1.]])

方法2:

from sklearn.preprocessing import LabelBinarizer
encoder = LabelBinarizer()
one_hot = encoder.fit_transform(a)
print(one_hot)
array([[1, 0, 0],
? ? ? ?[0, 1, 0],
? ? ? ?[1, 0, 0],
? ? ? ?[0, 0, 1]])

方法3:

import numpy as np
def dense_to_one_hot(labels_dense, num_classes):
? ? """Convert class labels from scalars to one-hot vectors."""
? ? num_labels = labels_dense.shape[0]
? ? index_offset = np.arange(num_labels) * num_classes
? ? labels_one_hot = np.zeros((num_labels, num_classes))
? ? labels_one_hot.flat[index_offset+labels_dense.ravel()] = 1
? ? return labels_one_hot
labels_dense = np.array([0,1,2,3,4])?
num_classes ?= 5
one_hot = dense_to_one_hot(labels_dense,num_classes)
print(one_hot)
[[1. 0. 0. 0. 0.]
?[0. 1. 0. 0. 0.]
?[0. 0. 1. 0. 0.]
?[0. 0. 0. 1. 0.]
?[0. 0. 0. 0. 1.]]

keras中多分類標(biāo)簽轉(zhuǎn)換成One-hot

import numpy as np
from keras.utils import to_categorical
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 7]
data = array(data)
print(data)
# [1 2 3 4 5 6 7 8 9 7]
#有普通np數(shù)組轉(zhuǎn)換為one-hot
one_hots = to_categorical(data)
print(one_hots)
# [[ 0. ?1. ?0. ?0. ?0. ?0. ?0. ?0. ?0. ?0.]
# ?[ 0. ?0. ?1. ?0. ?0. ?0. ?0. ?0. ?0. ?0.]
# ?[ 0. ?0. ?0. ?1. ?0. ?0. ?0. ?0. ?0. ?0.]
# ?[ 0. ?0. ?0. ?0. ?1. ?0. ?0. ?0. ?0. ?0.]
# ?[ 0. ?0. ?0. ?0. ?0. ?1. ?0. ?0. ?0. ?0.]
# ?[ 0. ?0. ?0. ?0. ?0. ?0. ?1. ?0. ?0. ?0.]
# ?[ 0. ?0. ?0. ?0. ?0. ?0. ?0. ?1. ?0. ?0.]
# ?[ 0. ?0. ?0. ?0. ?0. ?0. ?0. ?0. ?1. ?0.]
# ?[ 0. ?0. ?0. ?0. ?0. ?0. ?0. ?0. ?0. ?1.]
# ?[ 0. ?0. ?0. ?0. ?0. ?0. ?0. ?1. ?0. ?0.]]

one_hot 轉(zhuǎn)數(shù)組

# 由one-hot轉(zhuǎn)換為普通np數(shù)組
data = [argmax(one_hot)for one_hot in one_hots]
print(data)
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 7]

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python字符串str和字節(jié)數(shù)組相互轉(zhuǎn)化方法

    python字符串str和字節(jié)數(shù)組相互轉(zhuǎn)化方法

    下面小編就為大家?guī)硪黄猵ython字符串str和字節(jié)數(shù)組相互轉(zhuǎn)化方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Python生成requirements.txt的兩種最新方法

    Python生成requirements.txt的兩種最新方法

    在Python項目開發(fā)中requirements.txt文件扮演著至關(guān)重要的角色,它記錄了項目所需的所有依賴包及其精確版本號,這篇文章主要介紹了Python生成requirements.txt的兩種最新方法,需要的朋友可以參考下
    2025-04-04
  • python的一些加密方法及python 加密模塊

    python的一些加密方法及python 加密模塊

    這篇文章主要介紹了python的一些加密方法及python加密模塊,本文通過實例文字相結(jié)合的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2019-07-07
  • Python處理JSON文件的完整流程(讀取、解析、修改和保存)

    Python處理JSON文件的完整流程(讀取、解析、修改和保存)

    JSON是一種輕量級的數(shù)據(jù)交換格式,易于人閱讀和編寫,也易于機器解析和生成,以下是 JSON 文件的完整定義方法和規(guī)范,本文還介紹了使用Python處理JSON文件的完整流程,包括讀取、解析、修改和保存JSON數(shù)據(jù)的方法,需要的朋友可以參考下
    2025-11-11
  • Python編寫車票訂購系統(tǒng)?Python實現(xiàn)快遞收費系統(tǒng)

    Python編寫車票訂購系統(tǒng)?Python實現(xiàn)快遞收費系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Python編寫車票訂購系統(tǒng),Python實現(xiàn)快遞收費系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 最新評論

    陵川县| 锦屏县| 同德县| 横峰县| 安仁县| 德阳市| 保定市| 准格尔旗| 抚远县| 上栗县| 沽源县| 攀枝花市| 德格县| 上栗县| 湄潭县| 绿春县| 灌阳县| 泾川县| 张家界市| 枞阳县| 吉木萨尔县| 柳林县| 普陀区| 渝中区| 武冈市| 阳西县| 桦甸市| 屏东县| 隆子县| 尉犁县| 重庆市| 枣庄市| 同江市| 页游| 迭部县| 衡水市| 山东省| 青海省| 措勤县| 南江县| 西平县|