激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - pytorch中的自定義反向傳播,求導實例

pytorch中的自定義反向傳播,求導實例

2020-05-07 10:52xuxiaoyuxuxiaoyu Python

今天小編就為大家分享一篇pytorch中的自定義反向傳播,求導實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

pytorch中自定義backward()函數。在圖像處理過程中,我們有時候會使用自己定義的算法處理圖像,這些算法多是基于numpy或者scipy等包。

那么如何將自定義算法的梯度加入到pytorch的計算圖中,能使用Loss.backward()操作自動求導并優化呢。下面的代碼展示了這個功能`

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import torch
import numpy as np
from PIL import Image
from torch.autograd import gradcheck
class Bicubic(torch.autograd.Function):
def basis_function(self, x, a=-1):
  x_abs = np.abs(x)
  if x_abs < 1 and x_abs >= 0:
    y = (a + 2) * np.power(x_abs, 3) - (a + 3) * np.power(x_abs, 2) + 1
  elif x_abs > 1 and x_abs < 2:
    y = a * np.power(x_abs, 3) - 5 * a * np.power(x_abs, 2) + 8 * a * x_abs - 4 * a
  else:
    y = 0
  return y
def bicubic_interpolate(self,data_in, scale=1 / 4, mode='edge'):
  # data_in = data_in.detach().numpy()
  self.grad = np.zeros(data_in.shape,dtype=np.float32)
  obj_shape = (int(data_in.shape[0] * scale), int(data_in.shape[1] * scale), data_in.shape[2])
  data_tmp = data_in.copy()
  data_obj = np.zeros(shape=obj_shape, dtype=np.float32)
  data_in = np.pad(data_in, pad_width=((2, 2), (2, 2), (0, 0)), mode=mode)
  print(data_tmp.shape)
  for axis0 in range(obj_shape[0]):
    f_0 = float(axis0) / scale - np.floor(axis0 / scale)
    int_0 = int(axis0 / scale) + 2
    axis0_weight = np.array(
      [[self.basis_function(1 + f_0), self.basis_function(f_0), self.basis_function(1 - f_0), self.basis_function(2 - f_0)]])
    for axis1 in range(obj_shape[1]):
      f_1 = float(axis1) / scale - np.floor(axis1 / scale)
      int_1 = int(axis1 / scale) + 2
      axis1_weight = np.array(
        [[self.basis_function(1 + f_1), self.basis_function(f_1), self.basis_function(1 - f_1), self.basis_function(2 - f_1)]])
      nbr_pixel = np.zeros(shape=(obj_shape[2], 4, 4), dtype=np.float32)
      grad_point = np.matmul(np.transpose(axis0_weight, (1, 0)), axis1_weight)
      for i in range(4):
        for j in range(4):
          nbr_pixel[:, i, j] = data_in[int_0 + i - 1, int_1 + j - 1, :]
          for ii in range(data_in.shape[2]):
            self.grad[int_0 - 2 + i - 1, int_1 - 2 + j - 1, ii] = grad_point[i,j]
      tmp = np.matmul(axis0_weight, nbr_pixel)
      data_obj[axis0, axis1, :] = np.matmul(tmp, np.transpose(axis1_weight, (1, 0)))[:, 0, 0]
      # img = np.transpose(img[0, :, :, :], [1, 2, 0])
  return data_obj
 
def forward(self,input):
  print(type(input))
  input_ = input.detach().numpy()
  output = self.bicubic_interpolate(input_)
  # return input.new(output)
  return torch.Tensor(output)
 
def backward(self,grad_output):
  print(self.grad.shape,grad_output.shape)
  grad_output.detach().numpy()
  grad_output_tmp = np.zeros(self.grad.shape,dtype=np.float32)
  for i in range(self.grad.shape[0]):
    for j in range(self.grad.shape[1]):
      grad_output_tmp[i,j,:] = grad_output[int(i/4),int(j/4),:]
  grad_input = grad_output_tmp*self.grad
  print(type(grad_input))
  # return grad_output.new(grad_input)
  return torch.Tensor(grad_input)
 
def bicubic(input):
return Bicubic()(input)
 
def main():
    hr = Image.open('./baboon/baboon_hr.png').convert('L')
    hr = torch.Tensor(np.expand_dims(np.array(hr), axis=2))
    hr.requires_grad = True
    lr = bicubic(hr)
    print(lr.is_leaf)
    loss=torch.mean(lr)
    loss.backward()
if __name__ =='__main__':
    main()

要想實現自動求導,必須同時實現forward(),backward()兩個函數。

1、從代碼中可以看出來,forward()函數是針對numpy數據操作,返回值再重新指定為torch.Tensor類型。因此就有這個問題出現了:forward輸入input被轉換為numpy類型,輸出轉換為tensor類型,那么輸出output的grad_fn參數是如何指定的呢。調試發現,當main()中hr的requires_grad被指定為True,即hr被指定為需要求導的葉子節點。只要Bicubic類繼承自torch.autograd.Function,那么output也就是代碼中的lr的grad_fn就會被指定為<main.Bicubic object at 0x000001DD5A280D68>,即Bicubic這個類。

2、backward()為求導的函數,gard_output是鏈式求導法則的上一級的梯度,grad_input即為我們想要得到的梯度。只需要在輸入指定grad_output,在調用loss.backward()過程中的某一步會執行到Bicubic的backwward()函數

以上這篇pytorch中的自定義反向傳播,求導實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/xuxiaoyuxuxiaoyu/article/details/86737492

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人小视频免费在线观看 | 深夜影院a| 国产毛片视频 | 午夜精品成人一区二区 | v11av在线播放| 国产三级影院 | 中国洗澡偷拍在线播放 | 一级尻逼视频 | 欧美成人三级大全 | 刘亦菲一区二区三区免费看 | 91福利免费视频 | 久久久久免费精品国产小说色大师 | 国产成人强伦免费视频网站 | 91在线免费观看 | 一区二区三区欧美在线观看 | 久久久久se | 亚洲国产精品久久久久久久久久久 | 久久久成人999亚洲区美女 | 91久久夜色精品国产网站 | 欧美韩国日本在线 | 精品成人国产在线观看男人呻吟 | chinese军人gay呻吟| 日韩毛片毛片久久精品 | 精品久久久久久久久久久久包黑料 | av中文在线观看 | 国产宾馆3p国语对白 | 国产免费专区 | 一区二区三区黄色 | 69性欧美高清影院 | 久久国产精品91 | 久久亚洲精品视频 | 热@国产| 手机免费看一级片 | 久久精品小短片 | 91久久久久久久久久 | 亚洲成人黄色片 | 精品亚洲二区 | 羞羞视频免费入口网站 | 精品国产乱码久久久久久久 | av电影在线观看网站 | 久久99久久98精品免观看软件 |