Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to split and display mnist in plotly

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the plotly how to split the display of mnist related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this plotly how to split and display mnist article will have a harvest, let's take a look.

Load mnistimport numpydef 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 # Modification Point: tensorflow:2.6.2,keras:2.6.0 in this version Replace import keras with 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_SAMPLE_CNT,PIC_H. Shape, x_test.shape, label_test.shape # (60000, 28, 28), (60000,), (10000, 28), (10000,) The y of 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 does not need oneHot # _ label_train to look like one column with multiple rows. _ 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 displays multiple mnist samples import plotly.expressimport plotly.graph_objectsimport plotly.subplotsimport numpyxTrain:numpy.ndarray=numpy.random.random ((2je 28)) # xTrain [0] .shape: (28 Eng 28) # fig:plotly.graph_objects.Figure=Nonefig=plotly.subplots.make_subplots (rows=1,cols=2,shared_xaxes=True,shared_yaxes=True) # 1 row and 2 columns fig.add_trace (trace=plotly.express.imshow (img=xTrain [0]). Data [0], row=1 Col=1) # Line 1, column 1, fig.add_trace (trace=plotly.express.imshow (img=xTrain [1]). Data [0], row=1,col=2) # Row 1, column 2, fig.show () # parameters row, col start at 1 Plotly, which does not start at 0, displays a single picture import numpyxTrain:numpy.ndarray=numpy.random.random ((2p28)) # xTrain [0] .shape: (2828) import plotly.expressimport plotly.graph_objectsplotly.express.imshow (img=xTrain [0]). Show () # where the type of plotly.express.imshow (img=xTrain [0]) is plotly.graph_objects.Figure

The xTrain [0] is shown as follows:

Mnist single sample split shows # MNist single sample split into 4'4 small squares to confirm whether the segmentation is correct. The following code is the correct segmentation. The main logic is: (7 show 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) for h in range (7): for w in range (7): print (f "hpenny w: {h}, {w}") fig.add_trace (trace=plotly.express.imshow (xTrain0ImgCells [hpime pizzagol w]:]). Data [0], col=h+1,row=w+1) fig.show ()

Mnist single sample split display result: it can be seen from this figure that [h,:, w,:] is a normal 4-4 small square formed by taking adjacent pixels, which is exactly what is needed.

The Abscissa stretching scale shown above is larger than the ordinate, so it looks like a handwritten number 5 that has been stretched horizontally. If you can let plotly set the horizontal and vertical stretching scale to equal, it will be more like the handwritten number 5.

You can further change it to the following code with torch.swapdim

"" mnist single sample is divided into 4-4 small squares to display. The key logic is: (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) # Exchange (7,4,7,4) Dimensions 1 and 2 are (0:7, 1:4, 2:7) 3:4) for h in range (7): for w in range (7): print (f "xTrain0ImgCells w: {h}, {w}") fig.add_trace (trace=plotly.express.imshow (xTrain0ImgCells [h]). Data [0], col=h + 1, row=w + 1) # [hForce w,:,:] or [HMagnew] fig.show () mnist split display of single sample error

The following mnist single sample error split display:

# the split of mnist single sample error shows: 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 (4pd7, 4jin7)) # originally: (7pc4) 7 for h in range 4) for h in range (7): for w in range (7): print (f "hlegory w: {h}, {w}") fig.add_trace (trace=plotly.express.imshow (xTrain0ImgCells [:, hpene lave dagger w]). Data [0], col=h + 1, row=w + 1) # was originally: [hMagazine Magazine WJ:] fig.show ()

The result is as follows: it can be seen from this graph that (4, 7, 4) [:, h,:, w] is a small square of 4 formed by taking out the interval.

This is the end of the article on "how to split plotly to display mnist". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to divide plotly to display mnist". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report