matplotlib.pyplot 多次保存图片重叠等问题


提示:以下是本篇文章正文内容,下面案例可供参考

一、

torch.set_printoptions(threshold=np.inf)

二、遇到的问题

1.Pytorch Tensor 写入 txt 文件(或输出)时出现了省略号

在写文件前或输出前添加下列语句:

torch.set_printoptions(threshold=np.inf)

2.将 Numpy array 转为 torch 中 Tensor

使用下列代码:

a = np.ones(3,3)
torch.from_numpy(a)

3.Torch的 conv2d 与 Tensorflow 的 conv2d 用法区别

  1. torch.nn.Conv2d
    注意这里 Conved 是大写,具体函数介绍如下:
torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
// 其中, padding 表示填充,默认是0填充
// dialation 是扩张操作,控制卷积核各元素的间距,默认为1,即不扩张
// group  值为int类型,控制是否分组,默认不分组为1
// bias 值为布尔类型,是偏置,默认为真,即输出结果有偏置
  1. tf.nn,conv2d
tf.nn.conv2d (input, filter, strides, padding, use_cudnn_on_gpu=None, data_format=None, name=None)
// input 是被卷积的张量,为Tensor,shape为[batch, in_height, in_weidth, in_channel]
// filter 是卷积核,也是张量, shape为[filter_height, filter_width, in_channe, out_channel]
// strides 卷积时卷积核在每一维的步长,是一个以为向量[1, strides, strides, 1], 第一位和最后以为固定是1,我也不知道为什么
// padding string 类型,取值Wie'SAME' or 'VALID' 用来确定是否加填充,加多少填充

4. torch 给定卷积核做卷积的代码

import torch

input = [
    3, 4, 6, 5, 7,
    2, 4, 6, 8, 2,
    1, 6, 7, 8, 4,
    9, 7, 4, 6, 2,
    3, 7, 5, 4, 1
]
# 输入数据设置
input = torch.Tensor(input).view(1, 1, 5, 5)  # batch  通道数, w 和 h
# 卷积层设置
conv_layer = torch.nn.Conv2d(1, 1, kernel_size=3, padding=1, bias=False)  # 输入通道,输出通道
# 卷积核设置
kernel = torch.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9]).view(1, 1, 3, 3)  # 输出通道数,输入通道数  w和h
# 做出来的张量给卷积层的权重   即初始化权重
conv_layer.weight.data = kernel.data
# 将输入值给卷积层去计算得到结果
out = conv_layer(input)
print(out)

得到输出:
tensor([[[[ 91., 168., 224., 215., 127.],
          [114., 211., 295., 262., 149.],
          [192., 259., 282., 214., 122.],
          [194., 251., 253., 169.,  86.],
          [ 96., 112., 110.,  68.,  31.]]]], grad_fn=<ThnnConv2DBackward0>)

5.解决pytorch当中RuntimeError: expected scalar type Double but found Float的问题

哪儿出错就在那儿加上

x = x.to(torch.float32)

6. matplotlib.pyplot 多次保存图片重叠问题

每次保存晚图片后加一个如下的一行代码就可以了

plt.close()

7.python 包 tqdm 学习

tqdm模块是python进度条库,下面是tqdm模块的定义

class tqdm(object):
  """
  Decorate an iterable object, returning an iterator which acts exactly
  like the original iterable, but prints a dynamically updating
  progressbar every time a value is requested.
  """

  def __init__(self, iterable=None, desc=None, total=None, leave=False,
               file=sys.stderr, ncols=None, mininterval=0.1,
               maxinterval=10.0, miniters=None, ascii=None,
               disable=False, unit='it', unit_scale=False,
               dynamic_ncols=False, smoothing=0.3, nested=False,
               bar_format=None, initial=0, gui=False):

参数说明

iterable: 可迭代的对象, 在手动更新时不需要进行设置
desc: 字符串, 左边进度条描述文字
total: 总的项目数
leave: bool值, 迭代完成后是否保留进度条
file: 输出指向位置, 默认是终端, 一般不需要设置
ncols: 调整进度条宽度, 默认是根据环境自动调节长度, 如果设置为0, 就没有进度条, 只有输出的信息
unit: 描述处理项目的文字, 默认是’it’, 例如: 100 it/s, 处理照片的话设置为’img’ ,则为 100 img/s
unit_scale: 自动根据国际标准进行项目处理速度单位的换算, 例如 100000 it/s >> 100k it/s
参考地址


参考博客

1.Tensor输出省略问题
2.Torch Numpy 转 Tensor
3.tf.nn.conv2d介绍
4.torch.nn.Conv2d介绍
5.torch类型不对更改
6.torch确定卷积核做计算

`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值