How to update a tensor in a loop?

Hi, there. I’m writing a function for texture rendering that needs to update the rendered image in a loop. I have to deal with the z-buffer in a loop because it stores depth information that decides whether a pixel occurs front or back. Here is the code (in Numpy):

def render_texture(vertices, colors, triangles, h, w, c=3):
    """render mesh by z buffer
        vertices: 3 x nver
        colors: 3 x nver
        triangles: 3 x ntri
        h: height
        w: width
    # initial
    # print(vertices.shape, colors.shape, triangles.shape)
    # exit(0)
    image = np.zeros((h, w, c))

    depth_buffer = np.zeros([h, w]) - 999999.0
    # triangle depth: approximate the depth to the average value of z in each vertex(v0, v1, v2), since the vertices are closed to each other
    tri_depth = (
        vertices[2, triangles[0, :]]
        + vertices[2, triangles[1, :]]
        + vertices[2, triangles[2, :]]
    ) / 3.0
    tri_tex = (
        colors[:, triangles[0, :]]
        + colors[:, triangles[1, :]]
        + colors[:, triangles[2, :]]
    ) / 3.0

    for i in range(triangles.shape[1]):
        tri = triangles[:, i]  # 3 vertex indices

        # the inner bounding box
        umin = max(int(np.ceil(np.min(vertices[0, tri]))), 0)
        umax = min(int(np.floor(np.max(vertices[0, tri]))), w - 1)

        vmin = max(int(np.ceil(np.min(vertices[1, tri]))), 0)
        vmax = min(int(np.floor(np.max(vertices[1, tri]))), h - 1)

        if umax < umin or vmax < vmin:

        for u in range(umin, umax + 1):
            for v in range(vmin, vmax + 1):
                if tri_depth[i] > depth_buffer[v, u] and isPointInTri(
                    [u, v], vertices[:2, tri]
                    depth_buffer[v, u] = tri_depth[i]
                    image[v, u, :] = tri_tex[:, i]
    return image

I want to rewrite the code with PyTorch tensor to optimize the input through backpropagation. I know that we can’t do an in-place update for a tensor that requires grad. How should I deal with the tensor update in the for loop?

I figured it out. Just replace np with torch.