人工智能之核心技術(shù) 深度學(xué)習(xí) 第九章(PyTorch / TensorFlow)

<p>?</p><p/><h1>人工智能之核心技術(shù) 深度學(xué)習(xí)</h1><p/><p>第九章 框架實(shí)操(PyTorch / TensorFlow)</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/30827302-1055da4b32126e90.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p>


<h1>前言:框架實(shí)操(PyTorch / TensorFlow)—— 從理論到落地</h1>
<p>掌握深度學(xué)習(xí)理論后,<strong>動(dòng)手實(shí)現(xiàn)</strong>是檢驗(yàn)理解、積累經(jīng)驗(yàn)的關(guān)鍵一步。本章將系統(tǒng)對(duì)比兩大主流框架?<strong>PyTorch</strong>?與?<strong>TensorFlow/Keras</strong>,并通過四大經(jīng)典項(xiàng)目帶你完成端到端實(shí)戰(zhàn),并完整呈現(xiàn)?<strong>TensorFlow/Keras 的標(biāo)準(zhǔn)化開發(fā)流程</strong>。</p>
<p><strong>一、PyTorch 實(shí)戰(zhàn)</strong></p><p>PyTorch 以?<strong>“Pythonic” 風(fēng)格</strong>?和?<strong>動(dòng)態(tài)計(jì)算圖</strong>?著稱,深受研究者喜愛。</p><p><strong>1.1 張量操作與自動(dòng)求導(dǎo)</strong></p><pre>import?torch#?創(chuàng)建張量(支持?GPU)x?=?torch.randn(3,?requires_grad=True)??#?requires_grad=True?啟用自動(dòng)求導(dǎo)y?=?x??2z?=?y.sum()#?反向傳播z.backward()??#?自動(dòng)計(jì)算?dz/dxprint(x.grad)??#?輸出梯度</pre><blockquote><p>??<strong>核心機(jī)制</strong>:</p><ul><li><p>??<strong>requires_grad=True</strong>:標(biāo)記需計(jì)算梯度</p></li><li><p>??<strong>.backward()</strong>:觸發(fā)反向傳播</p></li><li><p>??<strong>.grad</strong>:存儲(chǔ)梯度值</p></li></ul></blockquote>
<p><strong>1.2 模型定義</strong></p><p><strong>方法1:</strong></p><pre>model?=?nn.Sequential(????nn.Linear(784,?128),????nn.ReLU(),????nn.Linear(128,?10))</pre><p><strong>方法2:繼承?</strong></p><pre>class?CNN(nn.Module):????def?init(self):????????super().init()????????self.conv1?=?nn.Conv2d(1,?32,?3)????????self.fc1?=?nn.Linear(32?
?26??26,?10)????def?forward(self,?x):????????x?=?torch.relu(self.conv1(x))????????x?=?x.view(x.size(0),?-1)??#?flatten????????x?=?self.fc1(x)????????return?x</pre>
<p><strong>1.3 數(shù)據(jù)加載:</strong></p><pre>from?torch.utils.data?import?Dataset,?DataLoaderfrom?torchvision?import?datasets,?transforms#?自定義?Dataset(通常用現(xiàn)成的)transform?=?transforms.ToTensor()train_data?=?datasets.MNIST('data',?train=True,?download=True,?transform=transform)#?DataLoader?批處理?+?多線程train_loader?=?DataLoader(train_data,?batch_size=64,?shuffle=True,?num_workers=4)</pre>
<p><strong>1.4 訓(xùn)練與驗(yàn)證流程</strong></p><pre>初始化模型/優(yōu)化器/損失for?epoch?in?epochs訓(xùn)練模式?model.train()for?batch?in?train_loader前向傳播?→?loss反向傳播?optimizer.zero_grad();?loss.backward();?optimizer.step()記錄訓(xùn)練指標(biāo)驗(yàn)證模式?model.eval()with?torch.no_grad():?for?batch?in?val_loader計(jì)算驗(yàn)證?loss/acc保存最佳模型</pre><pre>#?完整訓(xùn)練循環(huán)model?=?CNN().to(device)optimizer?=?torch.optim.Adam(model.parameters(),?lr=1e-3)criterion?=?nn.CrossEntropyLoss()for?epoch?inrange(10):????model.train()????for?images,?labels?in?train_loader:????????images,?labels?=?images.to(device),?labels.to(device)????????outputs?=?model(images)????????loss?=?criterion(outputs,?labels)????????????????optimizer.zero_grad()????????loss.backward()????????optimizer.step()????#?驗(yàn)證????model.eval()????correct?=?0????with?torch.no_grad():????????for?images,?labels?in?val_loader:????????????images,?labels?=?images.to(device),?labels.to(device)????????????outputs?=?model(images)????????????pred?=?outputs.argmax(dim=1)????????????correct?+=?pred.eq(labels).sum().item()????acc?=?correct?/?len(val_loader.dataset)????print(f"Epoch?{epoch},?Val?Acc:?{acc:.4f}")</pre>
<p><strong>二、TensorFlow / Keras 實(shí)戰(zhàn)(完整標(biāo)準(zhǔn)化流程)</strong></p><p>TensorFlow 以?<strong>生產(chǎn)部署友好</strong>?和?<strong>靜態(tài)圖優(yōu)化</strong>?見長,Keras 提供簡(jiǎn)潔高級(jí) API。以下是?<strong>工業(yè)級(jí)開發(fā)標(biāo)準(zhǔn)流程</strong>。</p><p><strong>2.1 整體開發(fā)流程</strong></p><p/><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/30827302-78c3d6070147eab2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p>
<p><strong>2.2 分步詳解(以 Fashion-MNIST 圖像分類為例)</strong></p><p><strong>步驟 1??:數(shù)據(jù)準(zhǔn)備</strong></p><pre>import?tensorflow?as?tffrom?tensorflow.keras?import?datasets,?layers,?modelsimport?numpy?as?np#?加載數(shù)據(jù)(x_train,?y_train),?(x_test,?y_test)?=?datasets.fashion_mnist.load_data()#?歸一化到?[0,1]x_train?=?x_train.astype('float32')?/?255.0x_test?=?x_test.astype('float32')?/?255.0#?添加通道維度?(28,28)?→?(28,28,1)x_train?=?np.expand_dims(x_train,?-1)x_test?=?np.expand_dims(x_test,?-1)#?劃分驗(yàn)證集(10%)val_split?=?int(0.1?
?len(x_train))x_val?=?x_train[:val_split]y_val?=?y_train[:val_split]x_train?=?x_train[val_split:]y_train?=?y_train[val_split:]</pre><blockquote><p>??<strong>關(guān)鍵點(diǎn)</strong>:</p><ul><li><p>? 圖像需歸一化(CNN 對(duì)尺度敏感)</p></li><li><p>??<strong>expand_dims</strong>?添加 channel 維度(Keras 要求 NHWC 格式)</p></li></ul></blockquote>
<p><strong>步驟 2??:模型構(gòu)建</strong></p><p><strong>方式 A:Sequential API(簡(jiǎn)單線性模型)</strong></p><pre>model?=?models.Sequential([????layers.Conv2D(32,?(3,3),?activation='relu',?input_shape=(28,28,1)),????layers.MaxPooling2D((2,2)),????layers.Conv2D(64,?(3,3),?activation='relu'),????layers.MaxPooling2D((2,2)),????layers.Flatten(),????layers.Dense(64,?activation='relu'),????layers.Dense(10,?activation='softmax')])</pre><p><strong>方式 B:Functional API(支持復(fù)雜結(jié)構(gòu))</strong></p><pre>inputs?=?layers.Input(shape=(28,28,1))x?=?layers.Conv2D(32,?3,?activation='relu')(inputs)x?=?layers.MaxPooling2D(2)(x)x?=?layers.Conv2D(64,?3,?activation='relu')(x)x?=?layers.MaxPooling2D(2)(x)x?=?layers.Flatten()(x)x?=?layers.Dense(64,?activation='relu')(x)outputs?=?layers.Dense(10,?activation='softmax')(x)model?=?models.Model(inputs=inputs,?outputs=outputs)</pre><blockquote><p>???<strong>何時(shí)用 Functional API?</strong></p><ul><li><p>? 多輸入/多輸出(如圖像+文本)</p></li><li><p>? 殘差連接(ResNet)</p></li><li><p>? 共享層</p></li></ul></blockquote>
<p><strong>步驟 3??:模型編譯</strong></p><pre>model.compile(????optimizer='adam',????loss='sparse_categorical_crossentropy',???#?標(biāo)簽為整數(shù)(0~9)????metrics=['accuracy'])</pre><blockquote><p>???<strong>注意損失函數(shù)選擇</strong>:</p><ul><li><p>??<strong>sparse_categorical_crossentropy</strong>:標(biāo)簽是整數(shù)</p></li><li><p>??<strong>categorical_crossentropy</strong>:標(biāo)簽是 one-hot 編碼</p></li></ul></blockquote>
<p><strong>步驟 4??:模型訓(xùn)練(含回調(diào))</strong></p><pre>from?tensorflow.keras.callbacks?import?EarlyStopping,?ModelCheckpointcallbacks?=?[????EarlyStopping(monitor='val_loss',?patience=3,?verbose=1),????ModelCheckpoint('best_fashion_cnn.h5',?save_best_only=True,?verbose=1)]history?=?model.fit(????x_train,?y_train,????batch_size=64,????epochs=20,????validation_data=(x_val,?y_val),????callbacks=callbacks)</pre><blockquote><p>??<strong>回調(diào)函數(shù)作用</strong>:</p><ul><li><p>??<strong>EarlyStopping</strong>:防止過擬合</p></li><li><p>??<strong>ModelCheckpoint</strong>:自動(dòng)保存最佳模型</p></li></ul></blockquote>
<p><strong>步驟 5??:模型評(píng)估與可視化</strong></p><pre>#?評(píng)估測(cè)試集test_loss,?test_acc?=?model.evaluate(x_test,?y_test,?verbose=0)print(f"Test?Accuracy:?{test_acc:.4f}")#?可視化訓(xùn)練過程import?matplotlib.pyplot?as?pltplt.plot(history.history['accuracy'],?label='Train?Acc')plt.plot(history.history['val_accuracy'],?label='Val?Acc')plt.xlabel('Epoch')plt.ylabel('Accuracy')plt.legend()plt.show()</pre>
<p><strong>步驟 6??:模型保存與部署</strong></p><pre>#?保存為?HDF5(輕量)model.save('fashion_cnn.h5')#?保存為?SavedModel(推薦,含完整計(jì)算圖)model.save('saved_model/fashion_cnn')#?轉(zhuǎn)?TFLite(移動(dòng)端)converter?=?tf.lite.TFLiteConverter.from_keras_model(model)tflite_model?=?converter.convert()with?open('model.tflite',?'wb')?as?f:????f.write(tflite_model)</pre><blockquote><p>???<strong>部署選項(xiàng)</strong>:</p><ul><li><p>??<strong>服務(wù)器</strong>:TensorFlow Serving</p></li><li><p>??<strong>移動(dòng)端</strong>:TensorFlow Lite</p></li><li><p>??<strong>Web</strong>:TensorFlow.js</p></li><li><p>??<strong>嵌入式</strong>:TensorFlow Lite Micro</p></li></ul></blockquote>
<p><strong>2.3 遷移學(xué)習(xí)實(shí)戰(zhàn)(ImageNet 預(yù)訓(xùn)練)</strong></p><pre>#?加載?ResNet50(不含頂層)base_model?=?tf.keras.applications.ResNet50(????weights='imagenet',????include_top=False,????input_shape=(224,?224,?3))base_model.trainable?=?False#?凍結(jié)特征提取層#?添加自定義分類頭model?=?models.Sequential([????base_model,????layers.GlobalAveragePooling2D(),????layers.Dropout(0.2),????layers.Dense(10,?activation='softmax')])#?編譯(小學(xué)習(xí)率)model.compile(optimizer=tf.keras.optimizers.Adam(1e-4),??????????????loss='sparse_categorical_crossentropy',??????????????metrics=['accuracy'])#?訓(xùn)練model.fit(train_ds,?validation_data=val_ds,?epochs=10)</pre><blockquote><p>???<strong>進(jìn)階技巧</strong>:第一階段凍結(jié) backbone → 保存頂層;第二階段解凍部分層 → 微調(diào)(fine-tune)</p></blockquote>
<p><strong>三、經(jīng)典項(xiàng)目實(shí)戰(zhàn)</strong></p><p><strong>項(xiàng)目1:圖像分類(MNIST / Fashion-MNIST)</strong></p><p><strong>PyTorch 實(shí)現(xiàn)(CNN)</strong></p><pre>class?SimpleCNN(nn.Module):????def__init__(self):????????super().init()????????self.conv1?=?nn.Conv2d(1,?32,?3,?padding=1)????????self.pool?=?nn.MaxPool2d(2)????????self.fc1?=?nn.Linear(32??14??14,?128)????????self.fc2?=?nn.Linear(128,?10)????defforward(self,?x):????????x?=?self.pool(torch.relu(self.conv1(x)))????????x?=?x.view(-1,?32??14??14)????????x?=?torch.relu(self.fc1(x))????????x?=?self.fc2(x)????????return?x</pre><blockquote><p>???<strong>結(jié)果</strong>:準(zhǔn)確率 > 98%(MNIST),> 90%(Fashion-MNIST)</p></blockquote>
<p><strong>項(xiàng)目2:文本情感分析(IMDB 電影評(píng)論)</strong></p><p><strong>方案A:LSTM(PyTorch)</strong></p><pre>class?LSTMClassifier(nn.Module):????def?init(self,?vocab_size,?embed_dim,?hidden_dim):????????super().init()????????self.embedding?=?nn.Embedding(vocab_size,?embed_dim)????????self.lstm?=?nn.LSTM(embed_dim,?hidden_dim,?batch_first=True)????????self.fc?=?nn.Linear(hidden_dim,?1)????def?forward(self,?x):????????x?=?self.embedding(x)????????,?(h_n,?)?=?self.lstm(x)????????return?self.fc(h_n[-1])</pre><p><strong>方案B:BERT(Hugging Face)</strong></p><pre>from?transformers?import?BertTokenizer,?BertForSequenceClassificationtokenizer?=?BertTokenizer.from_pretrained('bert-base-uncased')model?=?BertForSequenceClassification.from_pretrained('bert-base-uncased',?num_labels=2)</pre><blockquote><p>??<strong>效果</strong>:BERT 準(zhǔn)確率 ≈ 94%,LSTM ≈ 88%</p></blockquote>
<p><strong>項(xiàng)目3:圖像生成(GAN)</strong></p><p><strong>DCGAN 生成 MNIST(PyTorch)</strong></p><pre>#?生成器classGenerator(nn.Module):????def__init__(self):????????super().init()????????self.model?=?nn.Sequential(????????????nn.Linear(100,?256),????????????nn.ReLU(),????????????nn.Linear(256,?784),????????????nn.Tanh()????????)????defforward(self,?z):?returnself.model(z)#?判別器classDiscriminator(nn.Module):????def__init__(self):????????super().init()????????self.model?=?nn.Sequential(????????????nn.Linear(784,?256),????????????nn.LeakyReLU(0.2),????????????nn.Linear(256,?1),????????????nn.Sigmoid()????????)????defforward(self,?x):?returnself.model(x)</pre><blockquote><p>????<strong>輸出</strong>:可生成模糊但可辨的手寫數(shù)字</p></blockquote>
<p><strong>項(xiàng)目4:文生圖入門(Stable Diffusion)</strong></p><blockquote><p>???<strong>需 GPU(≥8GB 顯存)</strong></p></blockquote><pre>from?diffusers?import?StableDiffusionPipelineimport?torchpipe?=?StableDiffusionPipeline.from_pretrained(????"runwayml/stable-diffusion-v1-5",????torch_dtype=torch.float16).to("cuda")image?=?pipe("a?cyberpunk?cat?wearing?sunglasses").images[0]image.save("cat.png")</pre><pre>文本提示CLIP?TokenizerCLIP?Text?Encoder文本嵌入隨機(jī)噪聲UNet時(shí)間步去噪?latentVAE?Decoder生成圖像</pre>
<p><strong>四、PyTorch vs TensorFlow 對(duì)比總結(jié)</strong></p><p/><p/><blockquote><p>???<strong>選擇建議</strong>:</p><ul><li><p>??<strong>學(xué)術(shù)研究 / 快速原型</strong>?→?<strong>PyTorch</strong></p></li><li><p>??<strong>工業(yè)部署 / 移動(dòng)端</strong>?→?<strong>TensorFlow</strong></p></li></ul></blockquote>
<p><strong>五、總結(jié)</strong></p><p>通過本章,掌握:</p><ul><li><p>? ? PyTorch/TensorFlow 核心 API</p></li><li><p>? ??<strong>TensorFlow/Keras 工業(yè)級(jí)六步開發(fā)流程</strong></p></li><li><p>? ? 四大經(jīng)典任務(wù)完整實(shí)現(xiàn)</p></li><li><p>? ? 從數(shù)據(jù)加載到模型部署的全流程</p></li></ul><blockquote><p>???<strong>結(jié)語</strong>:深度學(xué)習(xí)不僅是算法,更是<strong>工程藝術(shù)</strong>。愿你以理論為帆,以代碼為槳,在 AI 的海洋中揚(yáng)帆遠(yuǎn)航!</p></blockquote>
<p><strong>附錄:環(huán)境配置建議</strong></p><pre>#?PyTorchpip?install?torch?torchvision?torchaudio#?TensorFlowpip?install?tensorflow#?Hugging?Face?生態(tài)pip?install?transformers?datasets?accelerate?diffusers``</pre><h1>資料關(guān)注</h1><p>公眾號(hào):咚咚王gitee:https://gitee.com/wy18585051844/ai_learning</p><p/><p>《Python編程:從入門到實(shí)踐》《利用Python進(jìn)行數(shù)據(jù)分析》《算法導(dǎo)論中文第三版》《概率論與數(shù)理統(tǒng)計(jì)(第四版) (盛驟) 》《程序員的數(shù)學(xué)》《線性代數(shù)應(yīng)該這樣學(xué)第3版》《微積分和數(shù)學(xué)分析引論》《(西瓜書)周志華-機(jī)器學(xué)習(xí)》《TensorFlow機(jī)器學(xué)習(xí)實(shí)戰(zhàn)指南》《Sklearn與TensorFlow機(jī)器學(xué)習(xí)實(shí)用指南》《模式識(shí)別(第四版)》《深度學(xué)習(xí) deep learning》伊恩·古德費(fèi)洛著 花書《Python深度學(xué)習(xí)第二版(中文版)【純文本】 (登封大數(shù)據(jù) (Francois Choliet)) (Z-Library)》《深入淺出神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)+(邁克爾·尼爾森(Michael+Nielsen)》《自然語言處理綜論 第2版》《Natural-Language-Processing-with-PyTorch》《計(jì)算機(jī)視覺-算法與應(yīng)用(中文版)》《Learning OpenCV 4》《AIGC:智能創(chuàng)作時(shí)代》杜雨+&+張孜銘《AIGC原理與實(shí)踐:零基礎(chǔ)學(xué)大語言模型、擴(kuò)散模型和多模態(tài)模型》《從零構(gòu)建大語言模型(中文版)》《實(shí)戰(zhàn)AI大模型》《AI 3.0》</p><p>?</p><p/><p/><p/><p/><p/>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容