如何在pytorch訓練模型的過程中,對於某一層的權重最大最小值進行限制?

時間 2021-06-30 12:33:43

1樓:sakuraiii

import

torch

from

torch

import

nnclass

Model(nn

.Module

):def

__init__

(self

):super

(Model

,self).

__init__

()self

.lin1=nn

.Linear(5

,10,bias

=False

)self

.lin2=nn

.Linear(10

,1,bias

=False

)def

forward

(self,x

):x=self

.lin1(x

)x=nn

.ReLU

()(x)x

=self

.lin2(x

)return

xforiin

model

.parameters

():print(i

)"""

Parameter containing:

tensor([[-0.0738, 0.2768, -0.0058, -0.2006, 0.3398],

0.0670, 0.1187, -0.3379, 0.3860, 0.0244],

0.2905, -0.0174, 0.2992, 0.1722, 0.2650],

0.1038, 0.0634, -0.2930, 0.3882, -0.1347],

0.1065, -0.4059, -0.0657, 0.2319, 0.0326],

0.3399, 0.2837, -0.3248, -0.2958, -0.0177],

0.0062, 0.3990, -0.0854, 0.4165, 0.0854],

0.1123, 0.1679, 0.3711, 0.3948, -0.3657],

0.3785, 0.0234, -0.0895, 0.0362, -0.1233],

0.0392, 0.0838, -0.3936, 0.0288, 0.0674]], requires_grad=True)

Parameter containing:

tensor([[ 0.2865, 0.2579, 0.

2902, -0.2191, 0.1160, 0.

1924, 0.1859, -0.2275,

0.0895, -0.0477]], requires_grad=True)

"""foriin

model

.parameters

():i

.data

=torch

.clamp(i

,0,1

)foriin

model

.parameters

():print(i

)"""

Parameter containing:

tensor([[0.0000, 0.2768, 0.0000, 0.0000, 0.3398],

[0.0670, 0.1187, 0.0000, 0.3860, 0.0244],

[0.2905, 0.0000, 0.2992, 0.1722, 0.2650],

[0.1038, 0.0634, 0.0000, 0.3882, 0.0000],

[0.1065, 0.0000, 0.0000, 0.2319, 0.0326],

[0.3399, 0.2837, 0.0000, 0.0000, 0.0000],

[0.0000, 0.3990, 0.0000, 0.4165, 0.0854],

[0.1123, 0.1679, 0.3711, 0.3948, 0.0000],

[0.3785, 0.0234, 0.0000, 0.0362, 0.0000],

[0.0392, 0.0838, 0.0000, 0.0288, 0.0674]], requires_grad=True)

Parameter containing:

tensor([[0.2865, 0.2579, 0.

2902, 0.0000, 0.1160, 0.

1924, 0.1859, 0.0000, 0.

0000,

0.0000]], requires_grad=True)

"""所以用

fori

inmodel

.parameters

():i

.data

=torch

.clamp(i

,0,1

)可以裁剪權重。同理可對具體某一層裁剪。

用Flask部署Pytorch模型如何處理併發問題?

在PyTorch文件https pytorch.org tutorials intermediate flask rest api tutorial.html 裡提到了關於實際生產中使用的幾個建議 參考Flask給出的一些建議,這裡要注意,這裡提到的一些工具可能和你使用的系統平台有關,要自己選擇合適...

在PyTorch中如何儲存和恢復模型並檢視引數

lilopop 可以先去日本考察看看,畢竟貓咖啡這個是在日本開始流行的,我也想開,但是我覺得前期投入會很多錢,首先你要選個地段,不能太嘈雜也不能太偏遠,在大學城附近最好,然後租店鋪肯定是月租不低於兩萬了,然後裝修不能太簡單,至少得花二十萬,然後引進貓可以田園品種,品種推薦布偶,美短,英短,店裡根據周...

同樣的模型與引數,PyTorch實現的效能比Tensorflow低了很多,有可能是什麼原因呢?

只是看看 手寫影象分割模型,在tensorflow上效果很好,在pytorch上很差。後來發現是損失函式的引數問題,tensorflow中自己寫的多分類focal loss用作分割任務 alpha用0.25 pytorch中重寫了該函式 但是alpha要用1.5至1.75才有效果 有點懵。 嬉嬉皮 ...