Fix long time step() detach
This commit is contained in:
parent
1ccf9a6c8d
commit
838af9cf4c
7 changed files with 1142 additions and 1 deletions
Binary file not shown.
Binary file not shown.
|
|
@ -1,5 +1,6 @@
|
|||
from ..optimizer import Optimizer
|
||||
from norch.tensor import Tensor
|
||||
import time
|
||||
|
||||
class SGD(Optimizer):
|
||||
def __init__(self, parameters, lr=1e-1, momentum=0):
|
||||
|
|
@ -21,3 +22,6 @@ class SGD(Optimizer):
|
|||
setattr(module, name, updated_parameter)
|
||||
|
||||
self._cache['velocity'][i] = velocity
|
||||
|
||||
parameter.detach()
|
||||
velocity.detach()
|
||||
|
|
|
|||
|
|
@ -167,6 +167,10 @@ class Tensor:
|
|||
def zero_grad(self):
|
||||
self.grad = None
|
||||
|
||||
def detach(self):
|
||||
self.grad = None
|
||||
self.grad_fn = None
|
||||
|
||||
def __getitem__(self, indices):
|
||||
if len(indices) != self.ndim:
|
||||
raise ValueError("Number of indices must match the number of dimensions")
|
||||
|
|
|
|||
1132
profile8.txt
Normal file
1132
profile8.txt
Normal file
File diff suppressed because it is too large
Load diff
0
profile9sem.txt
Normal file
0
profile9sem.txt
Normal file
3
test.py
3
test.py
|
|
@ -97,7 +97,7 @@ if __name__ == "__main__":
|
|||
input_list = [[0.5 for _ in range(100)]]
|
||||
input = norch.Tensor(input_list).T
|
||||
criterion = nn.MSELoss()
|
||||
optimizer = norch.optim.SGD(modelo.parameters(), lr=0.5)
|
||||
optimizer = norch.optim.SGD(modelo.parameters(), lr=0.05)
|
||||
|
||||
target_list = [[random.random() for _ in range(2)]]
|
||||
target = norch.Tensor(target_list).T
|
||||
|
|
@ -113,6 +113,7 @@ if __name__ == "__main__":
|
|||
loss.backward()
|
||||
#print('fora grad', modelo.layer1.weight.grad, "\n\n")
|
||||
optimizer.step()
|
||||
print(loss)
|
||||
|
||||
fim = time.time()
|
||||
print(fim - ini)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue