最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 實現(xiàn)倒排索引的方法

    python 實現(xiàn)倒排索引的方法

    今天小編就為大家分享一篇python 實現(xiàn)倒排索引的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Python猴子補丁知識點總結(jié)

    Python猴子補丁知識點總結(jié)

    在本篇文章里小編給大家分享的是關(guān)于Python猴子補丁知識點總結(jié),需要的朋友們學(xué)習(xí)下。
    2020-01-01
  • python使用正則表達(dá)式匹配字符串開頭并打印示例

    python使用正則表達(dá)式匹配字符串開頭并打印示例

    這篇文章主要介紹了python使用正則表達(dá)式匹配字符串開頭并打印的方法,結(jié)合實例形式分析了Python基于正則表達(dá)式操作字符串的相關(guān)技巧,需要的朋友可以參考下
    2017-01-01
  • 關(guān)于python中range()的參數(shù)問題

    關(guān)于python中range()的參數(shù)問題

    這篇文章主要介紹了關(guān)于python中range()的參數(shù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python TKinter獲取文本框內(nèi)容的方法

    python TKinter獲取文本框內(nèi)容的方法

    今天小編就為大家分享一篇python TKinter獲取文本框內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • 一文帶你探索Python中的eventlet通信機制

    一文帶你探索Python中的eventlet通信機制

    這篇文章主要為大家詳細(xì)介紹了Python中的eventlet通信機制的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們深入了解Python有一定幫助,需要的可以參考一下
    2023-06-06
  • python文本處理的方案(結(jié)巴分詞并去除符號)

    python文本處理的方案(結(jié)巴分詞并去除符號)

    這篇文章主要介紹了python文本處理的方案(結(jié)巴分詞并去除符號),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python實現(xiàn)最小二乘法的方法詳解

    python實現(xiàn)最小二乘法的方法詳解

    這篇文章主要介紹了如何基于python實現(xiàn)最小二乘法的方法,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2024-02-02
  • python常用模塊(math itertools functools sys shutil)使用講解

    python常用模塊(math itertools functools sys 

    這篇文章主要介紹了python常用模塊之math itertools functools sys shutil的使用示例講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 詳解appium自動化測試工具(monitor、uiautomatorviewer)

    詳解appium自動化測試工具(monitor、uiautomatorviewer)

    這篇文章主要介紹了詳解appium自動化測試工具(monitor、uiautomatorviewer),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論

理塘县| 绥化市| 灌南县| 曲周县| 永新县| 蛟河市| 额敏县| 浮山县| 蛟河市| 讷河市| 东至县| 泰州市| 海盐县| 铁岭县| 巴林左旗| 金乡县| 祁东县| 深圳市| 禹城市| 新昌县| 左权县| 集安市| 崇信县| 康保县| 银川市| 甘泉县| 鹿邑县| 张家港市| 连江县| 武功县| 宜阳县| 平顺县| 昂仁县| 沂南县| 昭苏县| 太原市| 临邑县| 定日县| 饶河县| 上林县| 云龙县|