In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Pytorch acquires values in gradient-free Tensor. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Problems encountered:
Using two networks to operate in parallel, the output value of one network is fed back to the other network. The output value of feedback has the gradient of network weight, that is, grad_fn=..
At this time, if the feedback value is thrown into the second network to update, there will be the first error that the calculation chart is lost and cannot be updated. Oh hey, I don't need the gradient of the first network at all, okay?
At first, I used a stupid way to transfer to numpy, and then back to torch.Tensor. Because numpy data has no gradient.
But my original tensor's is on cuda.
The tensor of cuda cannot be transferred directly to Tensor, so t_error = td_error.cuda (). Data.cpu (). Numpy () t_error = torch.FloatTensor (t_error) .to (device)
From cuda back to cpu, to numpy, to tensor, and back to cuda, daddy, maybe I'm the only one who can write such an inefficient spicy chicken code.
It was later found that, in fact, it was added directly on return.
With torch.no_grad (): td_error = reward + GAMMA * v _-v
It will be fine.
Add: take a mean of tensor in pytorch, and then compare all the values in the tensor with it!
The shape of Tensor in Pytorch is (B, C, W, H)
The code for averaging the tensor and comparing it with all values is as follows: C, H, W = tensor.shape [1], tensor.shape [2], tensor.shape [3] for c in range (C): mean = torch.mean (x [0] [c]) for h in range (H): for w in range (W): if x [0] [c] [h] [w] > = mean: X [0] [c] [h] [w] = mean! This is the end of this article on "how to get the value of Pytorch in gradient-free Tensor". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.