keras分類模型中的輸入數(shù)據(jù)與標(biāo)簽的維度實例
在《python深度學(xué)習(xí)》這本書中。
一、21頁mnist十分類
導(dǎo)入數(shù)據(jù)集 from keras.datasets import mnist (train_images, train_labels), (test_images, test_labels) = mnist.load_data() 初始數(shù)據(jù)維度: >>> train_images.shape (60000, 28, 28) >>> len(train_labels) 60000 >>> train_labels array([5, 0, 4, ..., 5, 6, 8], dtype=uint8) 數(shù)據(jù)預(yù)處理: train_images = train_images.reshape((60000, 28 * 28)) train_images = train_images.astype('float32') / 255 train_labels = to_categorical(train_labels) 之后: print(train_images, type(train_images), train_images.shape, train_images.dtype) print(train_labels, type(train_labels), train_labels.shape, train_labels.dtype) 結(jié)果: [[0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.]] <class 'numpy.ndarray'> (60000, 784) float32 [[0. 0. 0. ... 0. 0. 0.] [1. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] ... [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 0. 0.] [0. 0. 0. ... 0. 1. 0.]] <class 'numpy.ndarray'> (60000, 10) float32
二、51頁IMDB二分類
導(dǎo)入數(shù)據(jù):
from keras.datasets import imdb (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
參數(shù) num_words=10000 的意思是僅保留訓(xùn)練數(shù)據(jù)中前 10 000 個最常出現(xiàn)的單詞。
train_data和test_data都是numpy.ndarray類型,都是一維的(共25000個元素,相當(dāng)于25000個list),其中每個list代表一條評論,每個list中的每個元素的值范圍在0-9999 ,代表10000個最常見單詞的每個單詞的索引,每個list長度不一,因為每條評論的長度不一,例如train_data中的list最短的為11,最長的為189。
train_labels和test_labels都是含25000個元素(元素的值要不0或者1,代表兩類)的list。
數(shù)據(jù)預(yù)處理:
# 將整數(shù)序列編碼為二進(jìn)制矩陣 def vectorize_sequences(sequences, dimension=10000): # Create an all-zero matrix of shape (len(sequences), dimension) results = np.zeros((len(sequences), dimension)) for i, sequence in enumerate(sequences): results[i, sequence] = 1. # set specific indices of results[i] to 1s return results x_train = vectorize_sequences(train_data) x_test = vectorize_sequences(test_data) 第一種方式:shape為(25000,) y_train = np.asarray(train_labels).astype('float32') #就用這種方式就行了 y_test = np.asarray(test_labels).astype('float32') 第二種方式:shape為(25000,1) y_train = np.asarray(train_labels).astype('float32').reshape(25000, 1) y_test = np.asarray(test_labels).astype('float32').reshape(25000, 1) 第三種方式:shape為(25000,2) y_train = to_categorical(train_labels) #變成one-hot向量 y_test = to_categorical(test_labels)
第三種方式,相當(dāng)于把二分類看成了多分類,所以網(wǎng)絡(luò)的結(jié)構(gòu)同時需要更改,
最后輸出的維度:1->2
最后的激活函數(shù):sigmoid->softmax
損失函數(shù):binary_crossentropy->categorical_crossentropy
預(yù)處理之后,train_data和test_data變成了shape為(25000,10000),dtype為float32的ndarray(one-hot向量),train_labels和test_labels變成了shape為(25000,)的一維ndarray,或者(25000,1)的二維ndarray,或者shape為(25000,2)的one-hot向量。
注:
1.sigmoid對應(yīng)binary_crossentropy,softmax對應(yīng)categorical_crossentropy
2.網(wǎng)絡(luò)的所有輸入和目標(biāo)都必須是浮點數(shù)張量
補(bǔ)充知識:keras輸入數(shù)據(jù)的方法:model.fit和model.fit_generator
1.第一種,普通的不用數(shù)據(jù)增強(qiáng)的
from keras.datasets import mnist,cifar10,cifar100 (X_train, y_train), (X_valid, Y_valid) = cifar10.load_data() model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, shuffle=True, verbose=1, validation_data=(X_valid, Y_valid), )
2.第二種,帶數(shù)據(jù)增強(qiáng)的 ImageDataGenerator,可以旋轉(zhuǎn)角度、平移等操作。
from keras.preprocessing.image import ImageDataGenerator (trainX, trainY), (testX, testY) = cifar100.load_data() trainX = trainX.astype('float32') testX = testX.astype('float32') trainX /= 255. testX /= 255. Y_train = np_utils.to_categorical(trainY, nb_classes) Y_test = np_utils.to_categorical(testY, nb_classes) generator = ImageDataGenerator(rotation_range=15, width_shift_range=5./32, height_shift_range=5./32) generator.fit(trainX, seed=0) model.fit_generator(generator.flow(trainX, Y_train, batch_size=batch_size), steps_per_epoch=len(trainX) // batch_size, epochs=nb_epoch, callbacks=callbacks, validation_data=(testX, Y_test), validation_steps=testX.shape[0] // batch_size, verbose=1)
以上這篇keras分類模型中的輸入數(shù)據(jù)與標(biāo)簽的維度實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python使用sorted函數(shù)對列表進(jìn)行排序的方法
這篇文章主要介紹了python使用sorted函數(shù)對列表進(jìn)行排序的方法,涉及Python使用sorted函數(shù)的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04Python?pandas數(shù)據(jù)合并merge函數(shù)用法詳解
這篇文章主要給大家介紹了關(guān)于Python?pandas數(shù)據(jù)合并merge函數(shù)用法的相關(guān)資料,數(shù)據(jù)分析中經(jīng)常會遇到數(shù)據(jù)合并的基本問題,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07Python編程實現(xiàn)兩個文件夾里文件的對比功能示例【包含內(nèi)容的對比】
這篇文章主要介紹了Python編程實現(xiàn)兩個文件夾里文件的對比功能,包含內(nèi)容的對比操作,涉及Python文件與目錄的遍歷、比較、運算等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06python實現(xiàn)FTP服務(wù)器服務(wù)的方法
本篇文章主要介紹了python實現(xiàn)FTP服務(wù)器的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04python執(zhí)行js腳本報錯CryptoJS is not defined問題
這篇文章主要介紹了python執(zhí)行js腳本報錯CryptoJS is not defined問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05OpenCV實現(xiàn)機(jī)器人對物體進(jìn)行移動跟隨的方法實例
這篇文章主要給大家介紹了關(guān)于OpenCV實現(xiàn)機(jī)器人對物體進(jìn)行移動跟隨的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11OpenCV結(jié)合selenium實現(xiàn)滑塊驗證碼
本文主要介紹了OpenCV結(jié)合selenium實現(xiàn)滑塊驗證碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08