目标检测:手动实现(代码引自:《动手学深度学习》)
import torch
from torch import nn
def corr2d(X, K):
h, w = K.shape
Y = torch.zeros((X.shape[0] - h + 1, X.shape[1] - w + 1))
for i in range(Y.shape[0]):
for j in range(Y.shape[1]):
Y[i, j] = (X[i: i + h, j: j + w] * K).sum()
return Y
X = torch.ones(6, 6, dtype=torch.float)
X[:, 3:] = 0
K = torch.tensor([[1, 0,-1],[1, 0,-1],[1, 0,-1]], dtype=torch.float)
Y = corr2d(X, K)
print(Y)
遇到报错1、 总结: Y[i, j] = (X[i: i + h, j: j + w] * K).sum()
RuntimeError: expected type torch.FloatTensor but got torch.LongTensor
遇到报错2、RuntimeError: invalid argument 2: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Call .contiguous() before .view(). at d:\build\pytorch\pytorch-1.0.1\aten\src\th\generic\thtensor.cpp:213
自己搬代码的时候,写错了,粗心大意,实在是找不到原因,后来比对了代码才找到原因,看到问题之后,也明白了,别说后面的过程不能运算,Y的输入形态就有问题,Y的输入形态照我这么设置都变成负数了!所以会报张量的size 不可以处理!
自己总结了一些错误,希望可以帮到大家!学习AI的道路很辛苦,难免有的时候被困在疏忽大意上!