1.4 資料處理與模型圖構建
-
transflow 搭建 : www.tensorflow.org/install/ins…
-
使用的是cifar-10 的資料集 www.cs.toronto.edu/~kriz/cifar…
The CIFAR-10 dataset consists of 60000 32x32 colour images in 10 classes, with 6000 images per class. There are 50000 training images and 10000 test images.
CIFAR-10 下載下來的python版本檔案目錄結構如下
(deeplearning-0jfGJJsa) ~/project/pycharm/deeplearning/01_nn ᐅ tree cifar-10-batches-py cifar-10-batches-py ├── batches.meta ├── data_batch_1 # 一共5個batch的訓練資料集,每個batch中有10000張圖片和對應的資料 ├── data_batch_2 ├── data_batch_3 ├── data_batch_4 ├── data_batch_5 ├── readme.html └── test_batch # 測試資料集 0 directories, 8 files 複製程式碼
-
關於jupyternotebook修改預設環境的文章:www.jianshu.com/p/f70ea020e…
-
直觀的顯示一張圖片
- 匯入資料集
import pickle import numpy as np import os CIFAR_DIR = './cifar-10-batches-py' print(os.listdir(CIFAR_DIR)) 複製程式碼
['data_batch_1', 'readme.html', 'batches.meta', 'data_batch_2', 'data_batch_5', 'test_batch', 'data_batch_4', 'data_batch_3'] 複製程式碼
with open(os.path.join(CIFAR_DIR, "data_batch_1"), 'rb') as f: data = pickle.load(f, encoding='bytes') print(type(data)) print(data.keys()) print(type(data[b'data'])) print(type(data[b'labels'])) print(type(data[b'batch_label'])) print(type(data[b'filenames'])) print(data[b'data'].shape) # 32 * 32 (畫素點)* 3(rbg三通道) # RR-GG-BB print(data[b'data'][0:2]) print(data[b'labels'][0:2]) print(data[b'batch_label']) print(data[b'filenames'][0:2]) 複製程式碼
<class 'dict'> dict_keys([b'batch_label', b'labels', b'data', b'filenames']) <class 'numpy.ndarray'> <class 'list'> <class 'bytes'> <class 'list'> (10000, 3072) [[ 59 43 50 ... 140 84 72] [154 126 105 ... 139 142 144]] [6, 9] b'training batch 1 of 5' [b'leptodactylus_pentadactylus_s_000004.png', b'camion_s_000148.png'] 複製程式碼
- 顯示一張圖片
image_arr = data[b'data'][100] image_arr = image_arr.reshape((3,32,32)) # 需要32,32,3 image_arr = image_arr.transpose((1,2,0)) from matplotlib.pyplot import imshow imshow(image_arr) 複製程式碼
<matplotlib.image.AxesImage at 0x12311e668> 複製程式碼
-
模型圖構建
import tensorflow as tf import pickle import numpy as np import os CIFAR_DIR = './cifar-10-batches-py' print(os.listdir(CIFAR_DIR)) def load_data(filename): """read data from data file.""" with open(filename, 'rb') as f: data = pickle.load(f, 'bytes') return data[b'data'], data[b'labels'] # placeholder 佔位符,可以代表一個變數,可以用來構建計算圖,有資料來的時候就把資料傳入 # None 代表輸入的樣本數目是不確定的,3072 是變數的維度 x = tf.placeholder(tf.float32, [None, 3072]) y = tf.placeholder(tf.int64, [None]) # 只有一個維度就省略了 # get_variable 如果已經定義了則使用,否則使用指定方式定義 # 'w':變數名 # [x.get_shape()[-1],1]: 指定變數的維度 # initializer: 初始化變數的方式 w = tf.get_variable('w', [x.get_shape()[-1],1], initializer = tf.random_normal_initializer(0, 1)) b = tf.get_variable('b', [1], initializer = tf.constant_initializer(0.0)) # matmul 矩陣乘法 # x (None,3072) w (3072,1) b(3072,1) # y_ = [None,3072]*[3072,1] + [3072,1] = [None,1] y_ = tf.matmul(x, w) + b # 啟用函式 (None,1) p_y_1 = tf.nn.sigmoid(y_) # (None,1) y_reshaped = tf.reshape(y, (-1,1)) y_reshaped_float = tf.cast(y_reshaped, tf.float32) loss = tf.reduce_mean(tf.square(y_reshaped_float - p_y_1)) # bool predict = p_y_1 > 0.5 # [1,0,1,1,0,0,0] correct_prediction = tf.equal(tf.cast(predict,tf.int64), y_reshaped) # 3/7 accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float64)) with tf.name_scope('train_op'): # AdamOptimizer 是梯度下降的一個變種,minimize指定在哪個變數上做 train_op = tf.train.AdamOptimizer(1e-3).minimize(loss) 複製程式碼