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是非負整數(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維獨熱向量,默認為-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.其他位置的值為默認值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實現(xiàn)獲取單向鏈表倒數(shù)第k個結(jié)點的值示例
這篇文章主要介紹了python實現(xiàn)獲取單向鏈表倒數(shù)第k個結(jié)點的值,結(jié)合實例形式分析了Python針對單向鏈表的定義、遍歷、傳值、判斷等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10Python實現(xiàn)windows自動關(guān)機功能
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)windows自動關(guān)機功能,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考一下2025-01-01使用GitHub和Python實現(xiàn)持續(xù)部署的方法
這篇文章主要介紹了使用GitHub和Python實現(xiàn)持續(xù)部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05Python實現(xiàn)手寫一個類似django的web框架示例
這篇文章主要介紹了Python實現(xiàn)手寫一個類似django的web框架,結(jié)合具體實例形式分析了Python自定義簡單控制器、URL路由、視圖模型等功能,實現(xiàn)類似Django框架的web應(yīng)用相關(guān)操作技巧,需要的朋友可以參考下2018-07-07跟老齊學(xué)Python之使用Python操作數(shù)據(jù)庫(1)
本文詳細講述了使用python操作數(shù)據(jù)庫所需要了解的知識以及準(zhǔn)備工作,十分的詳盡,這里推薦給想學(xué)習(xí)python的小伙伴。2014-11-11利用Python進行數(shù)據(jù)可視化常見的9種方法!超實用!
這篇文章主要給大家介紹了關(guān)于利用Python進行數(shù)據(jù)可視化常見的9種方法!文中介紹的方法真的超實用!對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07弄懂這56個Python使用技巧(輕松掌握Python高效開發(fā))
這篇文章主要介紹了弄懂這56個Python使用技巧(輕松掌握Python高效開發(fā)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-09-09