欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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是非負整數(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)文章

最新評論