How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]
How can i just calculate a part of grad using make_functional in PyTorch?
func_model, func_params = make_functional(self.model) def fm(x, func_params): fx = func_model(func_params, x) return fx.squeeze(0).squeeze(0) def floss(func_params,input): fx = fm(input, func_params) return fx per_sample_grads =vmap(jacrev(floss), (None, 0))(func_params, input) cnt=0 for g in per_sample_grads: g = g.detach() J_d = g.reshape(len(g),-1) if cnt == 0 else torch.hstack([J_d,g.reshape(len(g),-1)]) cnt = 1 result = J_d.detach() In this code, per_sample_grads includes all […]