tensorflow如何將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(zhuǎn)化
將one_hot標(biāo)簽和數(shù)字(整數(shù))標(biāo)簽進(jìn)行相互轉(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)簽沒(méi)有封裝好的函數(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是一個(gè)列表,指定張量中獨(dú)熱向量的獨(dú)熱位置,或者說(shuō)indeces是非負(fù)整數(shù)表示的標(biāo)簽列表。len(indices)就是分類的類別數(shù)。
tf.one_hot返回的張量的階數(shù)為indeces的階數(shù)+1。
當(dāng)indices的某個(gè)分量取-1時(shí),即對(duì)應(yīng)的向量沒(méi)有獨(dú)熱值。
depth
是每個(gè)獨(dú)熱向量的維度on_value
是獨(dú)熱值off_value
是非獨(dú)熱值
axis指定第幾階為depth維獨(dú)熱向量,默認(rèn)為-1,即,指定張量的最后一維為獨(dú)熱向量
例如:對(duì)于一個(gè)2階張量而言,axis=0時(shí),即,每個(gè)列向量是一個(gè)獨(dú)熱的depth維向量
axis=1時(shí),即,每個(gè)行向量是一個(gè)獨(dú)熱的depth維向量。axis=-1,等價(jià)于axis=1
import tensorflow as tf # 得到4個(gè)5維獨(dú)熱行向量向量, # ? ?其中第1個(gè)向量的第0個(gè)分量是獨(dú)熱1, # ? ?第2個(gè)向量的第2個(gè)分量是獨(dú)熱, # ? ?第3個(gè)向量沒(méi)有獨(dú)熱,因?yàn)橹付?1 # ? ?第4個(gè)向量的第1個(gè)分量為獨(dú)熱 # labels向targets的轉(zhuǎn)變 labels = [0, 2, -1, 1] # labels是shape=(4,)的張量。則返回的targets是shape=(len(labels), depth)張量。 # 且這種情況下,axis=-1等價(jià)于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個(gè)5維獨(dú)熱行向量。 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個(gè)5維獨(dú)熱列向量 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階張量
在實(shí)際神經(jīng)網(wǎng)絡(luò)的設(shè)計(jì)應(yīng)用中,給定的labels通常是數(shù)字列表,以標(biāo)識(shí)樣本屬于哪一個(gè)分類。類別數(shù)則是獨(dú)熱向量的維數(shù)。
# 得到分類的獨(dú)熱向量 targets = tf.one_hot(labels, num_classes)
TensorFlow 多分類標(biāo)簽轉(zhuǎn)換成One-hot
在處理多分類問(wèn)題時(shí),將多分類標(biāo)簽轉(zhuǎn)成One-hot編碼是一種很常見(jiàn)的手段,以下即為T(mén)ensorflow將標(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) # 增加一個(gè)維度 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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)獲取單向鏈表倒數(shù)第k個(gè)結(jié)點(diǎn)的值示例
這篇文章主要介紹了python實(shí)現(xiàn)獲取單向鏈表倒數(shù)第k個(gè)結(jié)點(diǎn)的值,結(jié)合實(shí)例形式分析了Python針對(duì)單向鏈表的定義、遍歷、傳值、判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10Python實(shí)現(xiàn)windows自動(dòng)關(guān)機(jī)功能
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)windows自動(dòng)關(guān)機(jī)功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下2025-01-01使用GitHub和Python實(shí)現(xiàn)持續(xù)部署的方法
這篇文章主要介紹了使用GitHub和Python實(shí)現(xiàn)持續(xù)部署的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05Python實(shí)現(xiàn)手寫(xiě)一個(gè)類似django的web框架示例
這篇文章主要介紹了Python實(shí)現(xiàn)手寫(xiě)一個(gè)類似django的web框架,結(jié)合具體實(shí)例形式分析了Python自定義簡(jiǎn)單控制器、URL路由、視圖模型等功能,實(shí)現(xiàn)類似Django框架的web應(yīng)用相關(guān)操作技巧,需要的朋友可以參考下2018-07-07跟老齊學(xué)Python之使用Python操作數(shù)據(jù)庫(kù)(1)
本文詳細(xì)講述了使用python操作數(shù)據(jù)庫(kù)所需要了解的知識(shí)以及準(zhǔn)備工作,十分的詳盡,這里推薦給想學(xué)習(xí)python的小伙伴。2014-11-11利用Python進(jìn)行數(shù)據(jù)可視化常見(jiàn)的9種方法!超實(shí)用!
這篇文章主要給大家介紹了關(guān)于利用Python進(jìn)行數(shù)據(jù)可視化常見(jiàn)的9種方法!文中介紹的方法真的超實(shí)用!對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07弄懂這56個(gè)Python使用技巧(輕松掌握Python高效開(kāi)發(fā))
這篇文章主要介紹了弄懂這56個(gè)Python使用技巧(輕松掌握Python高效開(kāi)發(fā)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-09-09