
本節提示:
1、第一個dl例子;
2、tensor和tensor操作;
3、DL如何通過逆向傳播和梯度下降達到學習目的。
2.1 輸入資料集的格式
from keras.datasets import mnist
(train_images,train_labels),(test_images,test_labels) = mnist.load_data()
print('train_image_shape',train_images.shape)
print('train_labels_len',len(train_labels))
print('train_labels',train_labels)
train_image_shape (60000, 28, 28)
train_labels_len 60000
train_labels [5 0 4 ... 5 6 8]
2.2訓練網絡結構
network = models.Sequential()
network.add(layers.Dense(512,activation = 'relu',input_shape = (28*28,)))
network.add(layers.Dense(10,activation='softmax'))
layers(層)是訓練網絡的最基本組成部分,它讀入資料,輸出對于結果來說更有用的結果。
我們這裡建立的層是全連接配接層,注意這裡的input_shape用了(28*28,)這種表示方式。最後輸出的結果是10選1的結果。
2.3編譯模型
network.compile(optimizer='rmaprop',loss='categorical_crossentropy',metrics=['accuracy'])
下一步,為了讓DL能夠開始訓練,我們還需要額外的3個建構:
一個損失函數(a loss function)—用于計算系統訓練的準确效果;
一個優化函數(an optimizer)—用于定義優化方法;
在訓練的時候的度量—一般都是acc。
2.4處理資料集
在之前的讀入的資料中,圖檔儲存在類似(60000,28,28)這樣的結構中,不友善處理,這裡進行轉換
train_images = train_images.reshape((60000,28*28))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000,28*28))
test_images = test_images.astype('float32')/255
一方面是reshape,另一方面是轉換成float32結構。這兩個都是非常常見的操作。
2.5進行one_hot處理
from keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
變成這樣:
2.6 現在就嘗試訓練
network.fit(train_images,train_labels,epochs=5,batch_size=128)
回顧一下這裡輸入模型的資料
rain_images 是數量*(長*寬)
train_labes 是onehot格式。這兩個部分首先要清楚。
并且進行最後的驗證
test_loss,test_acc = network.evaluate(test_images,test_labels)
注意,即使是這裡的這些非常簡單的函數和資料,如果沒有GPU的支援,也可能是需要較長時間才能夠運作的。
2.7 什麼是tensor
經常看見的tensor這個詞,所謂tensor,就是資料的容器(a container for data)
比如
np.array(12)就是一個0D tensor
np.array([12,3,6,14])一個清單,就是一個1D tensor
np.array([12,3,6,14],
[6,79,35,1],
[7,80,4,36,2] )
一個清單的組合,就是一個2D tensor
再往上堆,就是3D tensor,也是比較好了解的
2.8 tensor的組成
主要包括3個部分
次元(rank)比如3D tensor顯而易見就是3d的 對應ndim
形狀(shape)也就是具體填充tensor裡面的具體内容 對于shape
種類(data type) float32 或者 float64之類 對于dtype
具體列印一個資料
import matplotlib.pyplot as plt
digit = train_images[3]
plt.imshow(digit,cmap=plt.cm.binary)
plt.show()
這裡這個兩次show令人印象深刻。
2.9實際情況下的圖像存儲
一套256*256的128 gray的圖檔集可以儲存在(128,256,256,1)的4d tensor中;而一套同樣大小的彩色圖像可以保持在(128,256,256,3)的tensor中。
如果是video data,往往必須是5維資料。
來自為知筆記(Wiz)附件清單
目前方向:圖像拼接融合、圖像識别
聯系方式:[email protected]