網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
加載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開(kāi)始, 不是從0開(kāi)始的
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]顯示如下:
mnist單樣本分拆顯示
#mnist單樣本分割 分割成4*4小格子顯示出來(lái), 以確認(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的小方格 ,這正是所需要的
上圖顯示 的 橫坐標(biāo)拉伸比例大于縱坐標(biāo) 所以看起來(lái)像一個(gè)被拉橫了的手寫(xiě)數(shù)字5 ,如果能讓plotly把橫縱拉伸比例設(shè)為相等 上圖會(huì)更像手寫(xiě)數(shù)字5
可以用torch.swapdim進(jìn)一步改成以下代碼
""" mnist單樣本分割 分割成4*4小格子顯示出來(lái), 重點(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的小方格?
總結(jié)
原文鏈接:https://blog.csdn.net/hfcaoguilin/article/details/123519043
相關(guān)推薦
- 2023-07-16 oracle 創(chuàng)建定時(shí)任務(wù)
- 2022-07-01 c++超細(xì)致講解引用_C 語(yǔ)言
- 2021-12-03 關(guān)于Redis數(shù)據(jù)庫(kù)入門(mén)詳細(xì)介紹_Redis
- 2021-11-12 C/C++?Qt?StatusBar底部狀態(tài)欄應(yīng)用教程_C 語(yǔ)言
- 2022-05-21 python中的變量命名規(guī)則詳情_(kāi)python
- 2022-04-05 vxe-table中vxe-grid的使用
- 2023-05-24 Pytorch:Conv2d卷積前后尺寸詳解_python
- 2022-11-23 詳解如何使用Pytorch進(jìn)行多卡訓(xùn)練_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支