- pytorch的输入一般为batch,所以对于单独的输入(只有单个的输入!)来说,需要加上一维,具体操作为
a = torch.from_numpy(a).unsqueeze(dim = 0)
需要注意的是,此时可能会报网络结构的错误,error提示如下:
TypeError: conv2d() received an invalid combination of arguments - got (numpy.ndarray, Parameter, Parameter, tuple, tuple, tuple, int), but expected one of:
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
* (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)
didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !Parameter!, !Parameter!, !tuple!, !tuple!, !tuple!, int)
2.对于多分类任务采用损失函数:
loss_func = torch.nn.CrossEntropyLoss()
loss = loss_func(output,label)
#其中output为 softmax tensor[[0.1233,0.211,...],[],[]],label为输出类别的list=[2,3,4,1....]
3.NN进行训练时,输入loss的output必须是模型的直接输出,不要对这个输出做数值运算,否则会无法正常反向传播!我们应该让label去适应output,而不要改变output(但是类型转换是被允许的)。
4.图片的读取与存放应该是两个互逆过程,不要读取是用pil,而存放则用cv2.
给出简单实例:
#存放
im = Image.fromarray(obs)
im.save('imgs/' + str(step) + '.jpeg')
#读取
img = Image.open(path)
img = np.array(img)