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

plotly分割顯示mnist的方法詳解

 更新時(shí)間:2022年03月17日 17:18:01   作者:Leibniz infinity sml  
這篇文章主要為大家詳細(xì)介紹了plotly分割顯示mnist的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

加載mnist

import numpy
def loadMnist() -> (numpy.ndarray,numpy.ndarray,numpy.ndarray,numpy.ndarray):
    """
    :return:  (xTrain,yTrain,xTest,yTest)
    """
    global _TRAIN_SAMPLE_CNT
    global PIC_H
    global PIC_W
    global _TEST_SAMPLE_CNT
    global PIC_HW
    from tensorflow import keras #修改點(diǎn): tensorflow:2.6.2,keras:2.6.0 此版本下,  import keras 換成 from tensorflow import keras
    import tensorflow
    print(f"keras.__version__:{keras.__version__}")#2.6.0
    print(f"tensorflow.__version__:{tensorflow.__version__}")#2.6.2
    # avatar_img_path = "/kaggle/working/data"

    import os
    import cv2
    xTrain:numpy.ndarray; label_train:numpy.ndarray; xTest:numpy.ndarray; label_test:numpy.ndarray
    yTrain:numpy.ndarray; yTest:numpy.ndarray
    #%userprofile%\.keras\datasets\mnist.npz
    (xTrain, label_train), (xTest, label_test) = keras.datasets.mnist.load_data()
    # x_train.shape,y_train.shape, x_test.shape, label_test.shape
    # (60000, 28, 28), (60000,), (10000, 28, 28), (10000,)
    _TRAIN_SAMPLE_CNT,PIC_H,PIC_W=xTrain.shape
    PIC_HW=PIC_H*PIC_W
    xTrain=xTrain.reshape((-1, PIC_H * PIC_W))
    xTest=xTest.reshape((-1, PIC_H * PIC_W))
    _TEST_SAMPLE_CNT=label_test.shape[0]

    from sklearn import preprocessing

    #pytorch 的 y 不需要 oneHot
    #_label_train是1列多行的樣子.  _label_train.shape : (60000, 1)
    yTrain=label_train
    # y_train.shape:(60000) ; y_train.dtype: dtype('int')
    CLASS_CNT=yTrain.shape[0]
    yTest=label_test
    # y_test.shape:(10000) ; y_test.dtype: dtype('int')
    xTrainMinMaxScaler:preprocessing.MinMaxScaler; xTestMinMaxScaler:preprocessing.MinMaxScaler
    xTrainMinMaxScaler=preprocessing.MinMaxScaler()
    xTestMinMaxScaler=preprocessing.MinMaxScaler()
    # x_train.dtype: dtype('uint8') -> dtype('float64')
    xTrain=xTrainMinMaxScaler.fit_transform(xTrain)
    # x_test.dtype: dtype('uint8') -> dtype('float64')
    xTest = xTestMinMaxScaler.fit_transform(xTest)
    return (xTrain,yTrain,xTest,yTest)
xTrain:torch.Tensor;yTrain:torch.Tensor; xTest:torch.Tensor; yTest:torch.Tensor(xTrain,yTrain,xTest,yTest)=loadMnist()

plotly 顯示多個(gè)mnist樣本

import plotly.express
import plotly.graph_objects
import plotly.subplots
import numpy
xTrain:numpy.ndarray=numpy.random.random((2,28,28))
#xTrain[0].shape:(28,28)
#fig:plotly.graph_objects.Figure=None
fig=plotly.subplots.make_subplots(rows=1,cols=2,shared_xaxes=True,shared_yaxes=True) #共1行2列
fig.add_trace(trace=plotly.express.imshow(img=xTrain[0]).data[0],row=1,col=1) #第1行第1列
fig.add_trace(trace=plotly.express.imshow(img=xTrain[1]).data[0],row=1,col=2) #第1行第2列
fig.show()
#參數(shù)row、col從1開始,  不是從0開始的

plotly 顯示單個(gè)圖片

import numpy
xTrain:numpy.ndarray=numpy.random.random((2,28,28))
#xTrain[0].shape:(28,28)
import plotly.express
import plotly.graph_objects
plotly.express.imshow(img=xTrain[0]).show()
#其中plotly.express.imshow(img=xTrain[0]) 的類型是 plotly.graph_objects.Figure

xTrain[0]顯示如下:

請(qǐng)?zhí)砑訄D片描述

mnist單樣本分拆顯示

#mnist單樣本分割 分割成4*4小格子顯示出來, 以確認(rèn)分割的對(duì)不對(duì)。 以下代碼是正確的分割。 主要邏輯是:   (7,4,7,4)   [h, :, w, :] 
fig:plotly.graph_objects.Figure=plotly.subplots.make_subplots(rows=7,cols=7,shared_xaxes=True,shared_yaxes=True,vertical_spacing=0,horizontal_spacing=0)
xTrain0Img:torch.Tensor=xTrain[0].reshape((PIC_H,PIC_W))
plotly.express.imshow(img=xTrain0Img).show()
xTrain0ImgCells:torch.Tensor=xTrain0Img.reshape((7,4,7,4))
for h in range(7):
    for w in range(7):
        print(f"h,w:{h},{w}")
        fig.add_trace(trace=plotly.express.imshow(xTrain0ImgCells[h,:,w,:]).data[0],col=h+1,row=w+1)
fig.show()

mnist單樣本分拆顯示結(jié)果: 由此圖可知 (7,4,7,4) [h, :, w, :] 是正常的取相鄰的像素點(diǎn)出而形成的4*4的小方格 ,這正是所需要的

請(qǐng)?zhí)砑訄D片描述

上圖顯示 的 橫坐標(biāo)拉伸比例大于縱坐標(biāo) 所以看起來像一個(gè)被拉橫了的手寫數(shù)字5 ,如果能讓plotly把橫縱拉伸比例設(shè)為相等 上圖會(huì)更像手寫數(shù)字5

可以用torch.swapdim進(jìn)一步改成以下代碼

    """
    mnist單樣本分割 分割成4*4小格子顯示出來, 重點(diǎn)邏輯是: (7, 4, 7, 4)  [h, :, w, :]
    :param xTrain:
    :return:
    """
    fig: plotly.graph_objects.Figure = plotly.subplots.make_subplots(rows=7, cols=7, shared_xaxes=True,  shared_yaxes=True, vertical_spacing=0,  horizontal_spacing=0)
    xTrain0Img: torch.Tensor = xTrain[0].reshape((PIC_H, PIC_W))
    plotly.express.imshow(img=xTrain0Img).show()
    xTrain0ImgCells: torch.Tensor = xTrain0Img.reshape((7, 4, 7, 4))
    xTrain0ImgCells=torch.swapdims(input=xTrain0ImgCells,dim0=1,dim1=2)#交換 (7, 4, 7, 4) 維度1、維度2 即 (0:7, 1:4, 2:7, 3:4)
    for h in range(7):
        for w in range(7):
            print(f"h,w:{h},{w}")
            fig.add_trace(trace=plotly.express.imshow(xTrain0ImgCells[h, w]).data[0], col=h + 1, row=w + 1) # [h, w, :, :] 或 [h, w]
    fig.show()

mnist單樣本錯(cuò)誤的分拆顯示

以下 mnist單樣本錯(cuò)誤的分拆顯示:

# mnist單樣本錯(cuò)誤的分拆顯示:
    fig: plotly.graph_objects.Figure = plotly.subplots.make_subplots(rows=7, cols=7, shared_xaxes=True,  shared_yaxes=True, vertical_spacing=0,  horizontal_spacing=0)
    xTrain0Img: torch.Tensor = xTrain[0].reshape((PIC_H, PIC_W))
    plotly.express.imshow(img=xTrain0Img).show()
    xTrain0ImgCells: torch.Tensor = xTrain0Img.reshape((4,7, 4, 7))  #原本是: (7,4,7,4)
    for h in range(7):
        for w in range(7):
            print(f"h,w:{h},{w}")
            fig.add_trace(trace=plotly.express.imshow(xTrain0ImgCells[:, h,  :, w]).data[0], col=h + 1, row=w + 1)  #原本是: [h,:,w,:]
    fig.show()

其結(jié)果為: 由此圖可知 (4,7, 4, 7) [:, h, :, w] 是間隔的取出而形成的4*4的小方格 

請(qǐng)?zhí)砑訄D片描述

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!    

相關(guān)文章

  • Python基于wordcloud及jieba實(shí)現(xiàn)中國地圖詞云圖

    Python基于wordcloud及jieba實(shí)現(xiàn)中國地圖詞云圖

    這篇文章主要介紹了Python基于wordcloud及jieba實(shí)現(xiàn)中國地圖詞云圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • pandas行和列的獲取的實(shí)現(xiàn)

    pandas行和列的獲取的實(shí)現(xiàn)

    本文主要介紹了pandas行和列的獲取的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 如何使用Python基于接口編程的方法實(shí)現(xiàn)

    如何使用Python基于接口編程的方法實(shí)現(xiàn)

    本文主要介紹了如何使用Python基于接口編程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Python通過RabbitMQ服務(wù)器實(shí)現(xiàn)交換機(jī)功能的實(shí)例教程

    Python通過RabbitMQ服務(wù)器實(shí)現(xiàn)交換機(jī)功能的實(shí)例教程

    RabbitMQ是一個(gè)基于消息隊(duì)列的服務(wù)器程序,Python可以通過Pika庫來驅(qū)動(dòng)它,這里我們將來看一個(gè)Python通過RabbitMQ服務(wù)器實(shí)現(xiàn)交換機(jī)功能的實(shí)例教程:
    2016-06-06
  • Python利用pip安裝tar.gz格式的離線資源包

    Python利用pip安裝tar.gz格式的離線資源包

    這篇文章主要給大家介紹了關(guān)于Python利用pip安裝tar.gz格式的離線資源包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python語言實(shí)現(xiàn)科學(xué)計(jì)算器

    Python語言實(shí)現(xiàn)科學(xué)計(jì)算器

    這篇文章主要為大家詳細(xì)介紹了Python語言實(shí)現(xiàn)科學(xué)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 淺談Python pygame繪制機(jī)制

    淺談Python pygame繪制機(jī)制

    今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識(shí),文章圍繞著Python pygame繪制機(jī)制展開,文中有非常詳細(xì)的介紹及圖文示例,需要的朋友可以參考下
    2021-06-06
  • PyTorch 普通卷積和空洞卷積實(shí)例

    PyTorch 普通卷積和空洞卷積實(shí)例

    今天小編就為大家分享一篇PyTorch 普通卷積和空洞卷積實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • 使用Python實(shí)現(xiàn)分別輸出每個(gè)數(shù)組

    使用Python實(shí)現(xiàn)分別輸出每個(gè)數(shù)組

    今天小編就為大家分享一篇使用Python實(shí)現(xiàn)分別輸出每個(gè)數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • python 對(duì)xml解析的示例

    python 對(duì)xml解析的示例

    這篇文章主要介紹了python 如何對(duì)xml解析,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-02-02

最新評(píng)論

盐城市| 武威市| 泾川县| 长治市| 兴国县| 洛隆县| 平山县| 周宁县| 四川省| 莲花县| 乃东县| 甘孜县| 汉中市| 伊通| 防城港市| 夏河县| 吉安市| 宣汉县| 永福县| 多伦县| 鹤壁市| 湖州市| 剑阁县| 包头市| 永善县| 霍城县| 本溪市| 博爱县| 通渭县| 固始县| 湖南省| 喜德县| 大英县| 南汇区| 穆棱市| 赫章县| 平远县| 崇信县| 阿巴嘎旗| 芦溪县| 惠水县|