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

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ù)轉換
    transform = transforms.Compose([
        transforms.ToTensor(),  # 將圖像轉換為張量
        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ù)轉換
    transform = transforms.Compose([
        transforms.ToTensor(),  # 將圖像轉換為張量
        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)

在這里插入圖片描述

到此這篇關于Python PyTorch 獲取 MNIST 數(shù)據(jù)的文章就介紹到這了,更多相關Python PyTorch 獲取 MNIST 數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

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

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

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

    Python猴子補丁知識點總結

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

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

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

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

    這篇文章主要介紹了關于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通信機制

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

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

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

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

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

    python常用模塊(math itertools functools sys 

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

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

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

最新評論

华安县| 迁西县| 高阳县| 普兰店市| 梅州市| 凉城县| 仁化县| 黄冈市| 长海县| 桃园县| 宾川县| 淮北市| 石景山区| 静安区| 邯郸县| 泰顺县| 通道| 枣强县| 聂荣县| 乐东| 武宁县| 普陀区| 方城县| 德兴市| 乐昌市| 邻水| 河西区| 临西县| 衢州市| 昌邑市| 兴文县| 临潭县| 禄劝| 马尔康县| 阳高县| 时尚| 贺兰县| 讷河市| 武山县| 岢岚县| 鸡西市|