Apply a function (similar to map) on a tensor?

username: Lam_Nguyen
cooked: <p>Hello</p>
<p>I have a vector : (v1,v2,v3,…,vn) = v</p>
<p>For which I have a function f(v_i) = out_i</p>
<p>Is there a simple and clean way to define F such that :<br>
F(v) = (f(v_1),f(v_2),f(v_3),…,f(v_n)) ?</p>
<p>Without using for loops, possibly by parallelizing the computation? As each instance instance of the function f are independant from each other.</p>
<p>Thanks!</p>

username: KFrank
cooked: <p>Hi Lam!</p>
<aside class="quote no-group" data-username="Lam_Nguyen" data-post="1" data-topic="51088" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lam_nguyen/48/14247_2.png" class="avatar"> Lam_Nguyen:</div>
<blockquote>
<p>Is there a simple and clean way to define F such that :<br>
F(v) = (f(v_1),f(v_2),f(v_3),…,f(v_n)) ?</p>
<p>Without using for loops, possibly by parallelizing the computation? As each instance instance of the function f are independant from each other.</p>
</blockquote>
</aside>
<p>As far as I am aware, pytorch does not have this kind of “map”<br>
function.</p>
<p>However, pytorch supports many different functions that act<br>
element-wise on tensors (arithmetic, cos(), log(), etc.).  If you<br>
can rewrite your function using element-wise torch tensor<br>
operations, your composite function will also act element-wise,<br>
and will do what you want.</p>
<p>Good luck.</p>
<p>K. Frank</p>

username: girishponkiya
cooked: <p>Thanks, <a class="mention" href="/u/kfrank">@KFrank</a>!</p>
<p>Numpy provides a way to <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html" rel="nofollow noopener"><code>vectorize</code></a> a function. Examples for the same makes it very clear and easy to understand. I am not able to find a similar thing in PyTorch. A reference to any of the following would be really helpful:</p>
<ul>
<li>How to use <code>map()</code> with PyTorch tensors?</li>
<li>Is there any API like <code>np.vectorize</code>?</li>
</ul>
<p><strong>PS:</strong> We want to apply a function <code>f</code> on each element of <em>list of tensors</em>.</p>
<p>Thanks!</p>

username: Mazochi
cooked: <p>For other readers,<br>
I found this function that satisfy this need from <a href="https://medium.com/jovianml/introduction-to-pytorch-with-tensor-functions-8a3598a76e28" rel="noopener nofollow ugc">this link</a>:</p>
<pre><code class="lang-auto">tensor = torch.tensor([[3,5,1,2],[3,1,5,3],[7,5,8,3]],dtype=torch.float)
print(tensor)
tensor.apply_(lambda x: (x+0.2))
print(tensor)
</code></pre>

username: melike
cooked: <p>Is there a better approach? Because it comes with the following warning,</p>
<blockquote>
<p>This function only works with CPU tensors and should not be used in code sections that require high performance.</p>
</blockquote>

username: Texfy
cooked: <p><a href="https://pytorch.org/functorch/stable/generated/functorch.vmap.html" rel="noopener nofollow ugc"><code>functorch.vmap</code></a> implements this behavior (but is still in beta)</p>

username: alexneakameni
cooked: <p>An example after installing functorch :</p>
<pre><code class="lang-auto">batch_size, feature_size = 3, 5
v = torch.randn(batch_size, feature_size)

def simple_row_func(feature_vec):
    # remove mean
    return feature_vec - feature_vec.mean()

result = functorch.vmap(simple_row_func)(v)

# equivalent to
result = v - v.mean(1, keepdim=True)
</code></pre>