Python PyTorch 如何獲取 MNIST 數(shù)據(jù)
更新時間:2024年04月27日 12:07:57 作者:深色風信子
這篇文章主要介紹了Python PyTorch 如何獲取 MNIST 數(shù)據(jù),通過示例代碼介紹了PyTorch 保存 MNIST 數(shù)據(jù),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)
# 訓練數(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)
# 訓練數(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)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
Python Pygame實戰(zhàn)之超級炸彈人游戲的實現(xiàn)
如今的玩家們在無聊的時候會玩些什么游戲呢?王者還是吃雞是最多的選擇。但在80、90年代的時候多是一些很簡單的游戲:《超級瑪麗》、《魂斗羅》等。本文將利用Pygame制作另一個經(jīng)典游戲—炸彈人,感興趣的可以了解一下2022-03-03
python?使用?with?open()?as?讀寫文件的操作方法
這篇文章主要介紹了python?使用?with?open()as?讀寫文件的操作代碼,寫文件和讀文件是一樣的,唯一區(qū)別是調(diào)用open()函數(shù)時,傳入標識符'w'或者'wb'表示寫文本文件或?qū)懚M制文件,需要的朋友可以參考下2022-11-11
pyecharts實現(xiàn)數(shù)據(jù)可視化
這篇文章主要介紹了pyecharts實現(xiàn)數(shù)據(jù)可視化,pyecharts 是百度開源的,適用于數(shù)據(jù)可視化的工具,配置靈活,展示圖表相對美觀,順滑,下面更多詳細內(nèi)容,需要的小伙伴可以參考一下2022-03-03
python中的decimal類型轉(zhuǎn)換實例詳解
decimal 模塊實現(xiàn)了定點和浮點算術(shù)運算符,使用的是大多數(shù)人所熟悉的模型,而不是程序員熟悉的模型,即大多數(shù)計算機硬件實現(xiàn)的 IEEE 浮點數(shù)運算。這篇文章主要介紹了python里的decimal類型轉(zhuǎn)換,需要的朋友可以參考下2019-06-06

