# Onwaier's Blog

0%

## 简介

pdb是python的内置模块，类似c++中的gdb的存在，可用于在命令行中对代码进行调试。

## 进入调试

1. 修改代码

The typical usage to break into the debugger from a running program is to insert at the location you want to break into the debugger.

1. 脚本执行调试

pdb.py can also be invoked as a script to debug other scripts

## 常用命令

_断点有关的命令_

b(break)

b no

b filename:no

b funcname

tbreak(break)

tbreak no

tbeak filename:no

tbreak funcname

disable bno

enable bno

cl

cl bno1[bno2 ……]

cl filename:no

_调试相关的命令_

s(step)

n(next)

r(return)

c(continue)

unt(until)

unt no

_查看代码或变量值_

l(list)

l no

l no1 no2

ll

p exp

pp exp

a

what is var

_其它_

restart

run

q(quit)

interact

## 冻结参数

1. 将需要固定的那部分参数的requires_grad置为False.
2. 在优化器中加入filter根据requires_grad进行过滤.

ps: 解决AttributeError: ‘NoneType’ object has no attribute ‘data’问题的一种思路就是冻结参数，参考博客 代码如下：

# 5341. 最后 K 个数的乘积

## 题目描述

• 将数字 num 添加到当前数字列表的最后面。
1. getProduct(int k)
• 返回当前数字列表中，最后 k 个数字的乘积。
• 你可以假设当前列表中始终 至少 包含 k 个数字。

[[],[3],[0],[2],[5],[4],[2],[3],[4],[8],[2]]

[null,null,null,null,null,null,20,40,0,null,32]

ProductOfNumbers productOfNumbers = new ProductOfNumbers();
productOfNumbers.getProduct(2); // 返回 20 。最后 2 个数字的乘积是 5 * 4 = 20
productOfNumbers.getProduct(3); // 返回 40 。最后 3 个数字的乘积是 2 * 5 * 4 = 40
productOfNumbers.getProduct(4); // 返回 0 。最后 4 个数字的乘积是 0 * 2 * 5 * 4 = 0
productOfNumbers.getProduct(2); // 返回 32 。最后 2 个数字的乘积是 4 * 8 = 32

# 5343. 多次求和构造目标数组

## 题目描述

• x 为你数组里所有元素的和
• 选择满足 0 <= i < target.size 的任意下标 i ，并让 A 数组里下标为 i 处的值为 x
• 你可以重复该过程任意次

[1, 1, 1], 和为 3 ，选择下标 1
[1, 3, 1], 和为 5， 选择下标 2
[1, 3, 5], 和为 9， 选择下标 0
[9, 3, 5] 完成

• 散装

• 统装

## 解决问题

data_x与data_y的数据是直接添加的，而data_z是经过if判断的，False时，就不会添加。这样就导致某一条记录里面没有data_z这个标签，为了解决这个问题，需将data_x和data_y的数据添加移到if条件中，实现data_x，data_y和data_z三个同步添加。即

## 算法解读

Floyd判圈算法(Floyd Cycle Detection Algorithm)，又称龟兔赛跑算法(Tortoise and Hare Algorithm)，是一个可以在有限状态机、迭代函数或者链表上判断是否存在环，求出该环的起点与长度的算法。该算法据高德纳称由美国科学家罗伯特·弗洛伊德发明。

• 形象化解释

• 判断是否有环

• 环的长度

• 环的起点

• 伪代码

• c++代码

• 空间复杂度

• 时间复杂度

## Brent的移动的兔子和传送的乌龟

### 简介

How do you determine if your singly-linked list has a cycle? In 1980, Brent invented an algorithm that not only worked in linear time, but required less stepping than Floyd’s Tortoise and the Hare algorithm (however it is slightly more complex). Although stepping through a ‘regular’ linked list is computationally easy, these algorithms are also used for factorization and pseudorandom number generators, linked lists are implicit and finding the next member is computationally difficult.

### 算法思路

Brent’s algorithm features a moving rabbit and a stationary, then teleporting, turtle. Both turtle and rabbit start at the top of the list. The rabbit takes one step per iteration. If it is then at the same position as the stationary turtle, there is obviously a loop. If it reaches the end of the list, there is no loop. Of course, this by itself will take infinite time if there is a loop. So every once in a while, we teleport the turtle to the rabbit’s position, and let the rabbit continue moving. We start out waiting just 2 steps before teleportation, and we double that each time we move the turtle.

### 算法效率

Note that like Floyd’s Tortoise and Hare algorithm, this one runs in O(N). However you’re doing less stepping than with Floyd’s (in fact the upper bound for steps is the number you would do with Floyd’s algorithm). According to Brent’s research, his algorithm is 24-36% faster on average for implicit linked list algorithms.

• 伪代码

• c++代码

## 解决问题

All images in torchvision have to be represented as 3-dimensional tensors of the form [Channel, Height, Width]. I’m guessing your float tensor is a 2d tensor (height x width). For example, this works:

## 问题解决

The error states that the DataLoader receives a PIL image. This is because there are no transforms made (transform=None) on the image. The getitem method of MyDataset passes an unprocessed PIL image to the DataLoader, whereas it should receive a tensor. You can add a transform that creates a tensor from the PIL image by adding transform:

PIL image交给DataLoader，必须要将PIL image先进行处理，即将其转为tensor，所以对图片仍需进行transform，将其它随机裁剪注释掉，仅保留PIP image转为tensor的代码。