欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python PyTorch 如何獲取 MNIST 數(shù)據(jù)

 更新時間:2024年04月27日 12:07:57   作者:深色風(fēng)信子  
這篇文章主要介紹了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)
    # 訓(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 聊聊Python中的@符號是什么意思

    聊聊Python中的@符號是什么意思

    @符號用做函數(shù)的修飾符,可以在模塊或者類的定義層內(nèi)對函數(shù)進(jìn)行修飾,下面這篇文章主要給大家介紹了關(guān)于Python中@符號是什么意思的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • 詳解Django之a(chǎn)uth模塊(用戶認(rèn)證)

    詳解Django之a(chǎn)uth模塊(用戶認(rèn)證)

    這篇文章主要介紹了詳解Django之a(chǎn)uth模塊(用戶認(rèn)證),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • pytorch 如何在GPU上訓(xùn)練

    pytorch 如何在GPU上訓(xùn)練

    這篇文章主要介紹了pytorch 如何在GPU上訓(xùn)練的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Python Pygame實(shí)戰(zhàn)之超級炸彈人游戲的實(shí)現(xiàn)

    Python Pygame實(shí)戰(zhàn)之超級炸彈人游戲的實(shí)現(xiàn)

    如今的玩家們在無聊的時候會玩些什么游戲呢?王者還是吃雞是最多的選擇。但在80、90年代的時候多是一些很簡單的游戲:《超級瑪麗》、《魂斗羅》等。本文將利用Pygame制作另一個經(jīng)典游戲—炸彈人,感興趣的可以了解一下
    2022-03-03
  • Python小程序之在圖片上加入數(shù)字的代碼

    Python小程序之在圖片上加入數(shù)字的代碼

    這篇文章主要介紹了Python小程序之在圖片上加入數(shù)字的代碼,這個是小編今天練手的小程序,代碼簡單易懂,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • python?使用?with?open()?as?讀寫文件的操作方法

    python?使用?with?open()?as?讀寫文件的操作方法

    這篇文章主要介紹了python?使用?with?open()as?讀寫文件的操作代碼,寫文件和讀文件是一樣的,唯一區(qū)別是調(diào)用open()函數(shù)時,傳入標(biāo)識符'w'或者'wb'表示寫文本文件或?qū)懚M(jìn)制文件,需要的朋友可以參考下
    2022-11-11
  • pyecharts實(shí)現(xiàn)數(shù)據(jù)可視化

    pyecharts實(shí)現(xiàn)數(shù)據(jù)可視化

    這篇文章主要介紹了pyecharts實(shí)現(xiàn)數(shù)據(jù)可視化,pyecharts 是百度開源的,適用于數(shù)據(jù)可視化的工具,配置靈活,展示圖表相對美觀,順滑,下面更多詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • python中的decimal類型轉(zhuǎn)換實(shí)例詳解

    python中的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-06
  • python實(shí)現(xiàn)健康碼查驗(yàn)系統(tǒng)

    python實(shí)現(xiàn)健康碼查驗(yàn)系統(tǒng)

    這篇文章主要介紹了?python實(shí)現(xiàn)健康碼查驗(yàn)系統(tǒng),主要用到的是python用了opencv庫和pyzbar庫,文中給大家提供一段代碼判斷是否綠碼,需要的朋友可以參考下
    2022-04-04
  • Python使用minidom讀寫xml的方法

    Python使用minidom讀寫xml的方法

    這篇文章主要介紹了Python使用minidom讀寫xml的方法,實(shí)例分析了使用minidom模塊操作XML文件的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評論