我想通過以下函數來理解一個熱表示。我已經打印了這個函數的每一行。但是,我只是在索引超出限制的最後一行發生錯誤。我無法理解原因。我該如何解決這個問題?給出錯誤的索引
def dense_to_one_hot(labels_dense, num_classes):
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.arange(475000)
print labels_dense.shape
num_classes=10
num_labels = labels_dense.shape[0]
print num_labels
index_offset = np.arange(num_labels) * num_classes
print index_offset
print len(index_offset)
print 'index offset is '+str(index_offset.shape)
labels_one_hot = np.zeros((num_labels, num_classes))
print labels_one_hot.shape
x= labels_dense.ravel()
print x
print x.shape
plus= index_offset+x
print plus
print plus.shape
labels_one_hot.flat[plus]=1
謝謝你這麼多這麼詳細的解釋。現在我得到了代碼中的錯誤,這就是爲什麼我的算法在訓練和測試測試中效率低下的原因。你能解釋一下如何將我的label_dense [0,1,... 474999]轉換成你提到的標籤合成文件。在我的代碼中,我開始從1 uptil 24開始標記,但是當我將數據同時洗牌和標籤時。我把他們的行索引換成sample.shape [0],這就是我得到錯誤的原因。 –
@ R.ali,我不認爲我明白你想要什麼。 「我如何將我的label_dense轉換爲您提到的標籤格式?」 –
行= sample.shape [0] 標記= np.array([在範圍1對於i(行)]) 爲i的範圍(1100): \t k4_label = i + 1的 \t label_file = np.array ([k4_label for i in range(row)]) \t label = np.append(label,label_file,axis = 0)sample.shape [0] == label.shape [0] shuffled_sample.shape [0] = = shuffled_label.shape [0] –