Python PyTorch 如何獲取 MNIST 數(shù)據(jù)
1 PyTorch 獲取 MNIST 數(shù)據(jù)
import torch import numpy as np import matplotlib.pyplot as plt # type: ignore from torchvision import datasets, transforms def mnist_get(): print(torch.__version__) # 定義數(shù)據(jù)轉(zhuǎn)換 transform = transforms.Compose([ transforms.ToTensor(), # 將圖像轉(zhuǎn)換為張量 transforms.Normalize((0.5,), (0.5,)) # 歸一化圖像數(shù)據(jù) ]) # 獲取數(shù)據(jù) train_data = datasets.MNIST(root='./data', train=False, download=True, transform=transform) test_data = datasets.MNIST(root='./data', train=False, download=True, transform=transform) # 訓(xùn)練數(shù)據(jù) train_image = train_data.data.numpy() train_label = train_data.targets.numpy() # 測試數(shù)據(jù) test_image = test_data.data.numpy() test_label = test_data.targets.numpy()
2 PyTorch 保存 MNIST 數(shù)據(jù)
import torch import numpy as np import matplotlib.pyplot as plt # type: ignore from torchvision import datasets, transforms def mnist_save(mnist_path): print(torch.__version__) # 定義數(shù)據(jù)轉(zhuǎn)換 transform = transforms.Compose([ transforms.ToTensor(), # 將圖像轉(zhuǎn)換為張量 transforms.Normalize((0.5,), (0.5,)) # 歸一化圖像數(shù)據(jù) ]) # 獲取數(shù)據(jù) train_data = datasets.MNIST(root='./data', train=False, download=True, transform=transform) test_data = datasets.MNIST(root='./data', train=False, download=True, transform=transform) # 訓(xùn)練數(shù)據(jù) train_image = train_data.data.numpy() train_label = train_data.targets.numpy() # 測試數(shù)據(jù) test_image = test_data.data.numpy() test_label = test_data.targets.numpy() np.savez(mnist_path, train_data=train_image, train_label=train_label, test_data=test_image, test_label=test_label) mnist_path = 'C:\\Users\\Hyacinth\\Desktop\\mnist.npz' mnist_save(mnist_path)
3 PyTorch 顯示 MNIST 數(shù)據(jù)
import torch import numpy as np import matplotlib.pyplot as plt # type: ignore from torchvision import datasets, transforms def mnist_show(mnist_path): data = np.load(mnist_path) image = data['train_data'][0:100] label = data['train_label'].reshape(-1, ) plt.figure(figsize = (10, 10)) for i in range(100): print('%f, %f' % (i, label[i])) plt.subplot(10, 10, i + 1) plt.imshow(image[i]) plt.show() mnist_path = 'C:\\Users\\Hyacinth\\Desktop\\mnist.npz' mnist_show(mnist_path)
到此這篇關(guān)于Python PyTorch 獲取 MNIST 數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Python PyTorch 獲取 MNIST 數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- pytorch實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存
- 關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解
- pytorch:實(shí)現(xiàn)簡單的GAN示例(MNIST數(shù)據(jù)集)
- 使用 PyTorch 實(shí)現(xiàn) MLP 并在 MNIST 數(shù)據(jù)集上驗(yàn)證方式
- 用Pytorch訓(xùn)練CNN(數(shù)據(jù)集MNIST,使用GPU的方法)
- 詳解PyTorch手寫數(shù)字識別(MNIST數(shù)據(jù)集)
- pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法
相關(guān)文章
詳解Django之a(chǎn)uth模塊(用戶認(rèn)證)
這篇文章主要介紹了詳解Django之a(chǎn)uth模塊(用戶認(rèn)證),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Python Pygame實(shí)戰(zhàn)之超級炸彈人游戲的實(shí)現(xiàn)
如今的玩家們在無聊的時候會玩些什么游戲呢?王者還是吃雞是最多的選擇。但在80、90年代的時候多是一些很簡單的游戲:《超級瑪麗》、《魂斗羅》等。本文將利用Pygame制作另一個經(jīng)典游戲—炸彈人,感興趣的可以了解一下2022-03-03python?使用?with?open()?as?讀寫文件的操作方法
這篇文章主要介紹了python?使用?with?open()as?讀寫文件的操作代碼,寫文件和讀文件是一樣的,唯一區(qū)別是調(diào)用open()函數(shù)時,傳入標(biāo)識符'w'或者'wb'表示寫文本文件或?qū)懚M(jìn)制文件,需要的朋友可以參考下2022-11-11pyecharts實(shí)現(xiàn)數(shù)據(jù)可視化
這篇文章主要介紹了pyecharts實(shí)現(xiàn)數(shù)據(jù)可視化,pyecharts 是百度開源的,適用于數(shù)據(jù)可視化的工具,配置靈活,展示圖表相對美觀,順滑,下面更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下2022-03-03python中的decimal類型轉(zhuǎn)換實(shí)例詳解
decimal 模塊實(shí)現(xiàn)了定點(diǎn)和浮點(diǎn)算術(shù)運(yùn)算符,使用的是大多數(shù)人所熟悉的模型,而不是程序員熟悉的模型,即大多數(shù)計(jì)算機(jī)硬件實(shí)現(xiàn)的 IEEE 浮點(diǎn)數(shù)運(yùn)算。這篇文章主要介紹了python里的decimal類型轉(zhuǎn)換,需要的朋友可以參考下2019-06-06python實(shí)現(xiàn)健康碼查驗(yàn)系統(tǒng)
這篇文章主要介紹了?python實(shí)現(xiàn)健康碼查驗(yàn)系統(tǒng),主要用到的是python用了opencv庫和pyzbar庫,文中給大家提供一段代碼判斷是否綠碼,需要的朋友可以參考下2022-04-04