大家好,盡管大多數(shù)關(guān)于神經(jīng)網(wǎng)絡(luò)的文章都強調(diào)數(shù)學(xué),而TensorFlow文檔則強調(diào)使用現(xiàn)成數(shù)據(jù)集進行快速實現(xiàn),但將這些資源應(yīng)用于真實世界數(shù)據(jù)集是很有挑戰(zhàn)性的,很難將數(shù)學(xué)概念和現(xiàn)成數(shù)據(jù)集與我的具體用例聯(lián)系起來。本文旨在提供一個實用的、逐步的教程,介紹如何使用TensorFlow訓(xùn)練深度學(xué)習模型,并重點介紹如何將數(shù)據(jù)集重塑為TensorFlow對象,以便TensorFlow框架能夠識別。
本文主要內(nèi)容包括:
-
將DataFrame轉(zhuǎn)換為TensorFlow對象
-
從頭開始訓(xùn)練深度學(xué)習模型
-
使用預(yù)訓(xùn)練的模型訓(xùn)練深度學(xué)習模型
-
評估、預(yù)測和繪制訓(xùn)練后的模型。
安裝TensorFlow和其他必需的庫?
首先,你需要安裝TensorFlow。你可以通過在終端或Anaconda中運行以下命令來完成:
# 安裝所需的軟件包
!pip install tensorflow
!pip install tensorflow-datasets
安裝TensorFlow之后,導(dǎo)入其他必需的庫,如Numpy、Matplotlib和Sklearn。
import os
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
加載數(shù)據(jù)集
一旦導(dǎo)入了所有必需的庫,下一步是獲取數(shù)據(jù)集來搭建模型。TensorFlow允許使用各種輸入格式,包括CSV、TXT和圖像文件,有些數(shù)據(jù)集可以從TensorFlow-dataset中導(dǎo)入,這些數(shù)據(jù)集已準備好用作深度學(xué)習模型的輸入。然而在許多情況下,數(shù)據(jù)集是以DataFrame格式而不是TensorFlow對象格式存在的。本文我們將使用Sklearn中的MNIST數(shù)據(jù)集,其格式為Pandas DataFrame。MNIST數(shù)據(jù)集廣泛用于圖像分類任務(wù),包括70000個手寫數(shù)字的灰度圖像,每個圖像大小為28x28像素。該數(shù)據(jù)集被分為60000個訓(xùn)練圖像和10000個測試圖像。
from sklearn.datasets import fetch_openml
# 加載MNIST數(shù)據(jù)集
# mnist = fetch_openml('mnist_784')
# 輸出MNIST數(shù)據(jù)集
print('Dataset type:', type(mnist.data))
# 瀏覽一下加載的數(shù)據(jù)集
mnist.data.head()
?通過輸出DataFrame的前部,我們可以觀察到它包含784列,每列代表一個像素。
?將DataFrame轉(zhuǎn)換為TensorFlow數(shù)據(jù)集對象
加載了Pandas DataFrame,注意到TensorFlow不支持Pandas DataFrame作為模型的輸入,因此必須將DataFrame轉(zhuǎn)換為可以用于訓(xùn)練或評估模型的張量。這個轉(zhuǎn)換過程確保數(shù)據(jù)以與TensorFlow API兼容的格式存在,為了將MNIST數(shù)據(jù)集從DataFrame轉(zhuǎn)換為tf.data.Dataset
對象,可以執(zhí)行以下步驟:
-
將數(shù)據(jù)和目標轉(zhuǎn)換為NumPy數(shù)組并對數(shù)據(jù)進行歸一化處理
-
使用scikit-learn中的
train_test_split
將數(shù)據(jù)集拆分為訓(xùn)練集和測試集 -
將訓(xùn)練和測試數(shù)據(jù)重塑為28x28x1的圖像
-
使用
from_tensor_slices
為訓(xùn)練集和測試集創(chuàng)建tf.data.Dataset
對象
def get_dataset(mnist):
# 加載MNIST數(shù)據(jù)集
# mnist = fetch_openml('mnist_784')
# 將數(shù)據(jù)和目標轉(zhuǎn)換成numpy數(shù)組
X = mnist.data.astype('float32')
y = mnist.target.astype('int32')
# 將數(shù)據(jù)歸一化,使其數(shù)值在0和1之間
X /= 255.0
# 將數(shù)據(jù)集分成訓(xùn)練集和測試集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 將訓(xùn)練數(shù)據(jù)重塑為28x28x1的圖像
X_train = X_train.values.reshape((-1, 28, 28, 1))
X_test = X_test.values.reshape((-1, 28, 28, 1))
# 為訓(xùn)練和測試集創(chuàng)建TensorFlow數(shù)據(jù)集對象
train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
test_dataset = tf.data.Dataset.from_tensor_slices((X_test, y_test))
# 輸出訓(xùn)練和測試集的形狀
print('Training data shape:', X_train.shape)
print('Training labels shape:', y_train.shape)
print('Testing data shape:', X_test.shape)
print('Testing labels shape:', y_test.shape)
return X_test, y_test, X_train, y_train
?再來看一下我們的訓(xùn)練和測試TensorFlow對象:
?經(jīng)過這個過程,原始數(shù)據(jù)集已經(jīng)成功轉(zhuǎn)換為形狀為(5600,28,28,1)的TensorFlow對象。文章來源:http://www.zghlxwxcb.cn/news/detail-615573.html
經(jīng)過以上的步驟我們已經(jīng)完成了實戰(zhàn)的前半部分,后文將繼續(xù)講解有關(guān)定義深度學(xué)習模型、訓(xùn)練模型和評估模型的內(nèi)容。文章來源地址http://www.zghlxwxcb.cn/news/detail-615573.html
到了這里,關(guān)于使用TensorFlow訓(xùn)練深度學(xué)習模型實戰(zhàn)(上)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!