plotly怎么分割显示mnist
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章主要介绍了plotly怎么分割显示mnist的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇plotly怎么分割显示mnist文章都会有所收获,下面我们一起来看看吧
千家信息网最后更新 2025年02月01日plotly怎么分割显示mnist
这篇文章主要介绍了plotly怎么分割显示mnist的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇plotly怎么分割显示mnist文章都会有所收获,下面我们一起来看看吧。
加载mnist
import 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 #修改点: 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 显示多个mnist样本
import plotly.expressimport plotly.graph_objectsimport plotly.subplotsimport numpyxTrain:numpy.ndarray=numpy.random.random((2,28,28))#xTrain[0].shape:(28,28)#fig:plotly.graph_objects.Figure=Nonefig=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()#参数row、col从1开始, 不是从0开始的
plotly 显示单个图片
import numpyxTrain:numpy.ndarray=numpy.random.random((2,28,28))#xTrain[0].shape:(28,28)import plotly.expressimport plotly.graph_objectsplotly.express.imshow(img=xTrain[0]).show()#其中plotly.express.imshow(img=xTrain[0]) 的类型是 plotly.graph_objects.Figure
xTrain[0]显示如下:
mnist单样本分拆显示
#mnist单样本分割 分割成4*4小格子显示出来, 以确认分割的对不对。 以下代码是正确的分割。 主要逻辑是: (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单样本分拆显示结果: 由此图可知 (7,4,7,4) [h, :, w, :] 是正常的取相邻的像素点出而形成的4*4的小方格 ,这正是所需要的
上图显示 的 横坐标拉伸比例大于纵坐标 所以看起来像一个被拉横了的手写数字5 ,如果能让plotly把横纵拉伸比例设为相等 上图会更像手写数字5
可以用torch.swapdim进一步改成以下代码
""" mnist单样本分割 分割成4*4小格子显示出来, 重点逻辑是: (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单样本错误的分拆显示
以下 mnist单样本错误的分拆显示:
# mnist单样本错误的分拆显示: 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()
其结果为: 由此图可知 (4,7, 4, 7) [:, h, :, w] 是间隔的取出而形成的4*4的小方格
关于"plotly怎么分割显示mnist"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"plotly怎么分割显示mnist"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
样本
知识
错误
上图
代码
内容
原本
数字
方格
格子
比例
由此
篇文章
结果
维度
逻辑
不对
价值
像素
单个
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
换服务器 seo
网络安全法制日宣传标语
网络安全法规定成立领导小组
出行不忘网络安全
dell服务器加显卡
中国网络安全50强 华途
新罗区寄长枫网络技术服务部电话
互联网科技公司怎么开发票
真实服务器虚拟外汇账户
清橙互联网科技
简述系统数据库
自动备份数据库压缩脚本
邮箱服务器怎么接受邮件
点点数据库
数据库开发技术指标响应时间
税务系统网络安全手册
银河麒麟服务器v5
浙江无线网络技术电话
武汉戴尔服务器型号参数
实时数据服务器哪些比较好
大功率服务器 电源接线
服务器生产厂商
邯郸互联网软件开发价格
网络安全法小助手
全县网络安全宣传方案范文
大连城市大脑网络技术有限公司
机电一体化和网络技术
电子商务网络技术基础第二版
软件开发试卷
游戏服务器型号