I ran into some code that made me think about the nature of coding and computer science. I was reviewing some PyTorch documentation example code and saw a statement that looked something like:
foo = torch.tensor(foo[:-1]).cumsum(dim=0).to(device)
From the context of the code, I knew what the result of the statement would be, but the syntax of the statement was not obvious to me at all. The problem is to compute offsets of some concatenated arrays. Suppose:
arr1 = [11 15 17] 0 1 2 arr2 = [21 28] 0 1 arr3 = [33 39 35 34] 0 1 2 3 arr4 = [44 46 43] 0 1 2
The lengths of the arrays are [3, 2, 4, 3].
If the four arrays were concatented:
crr = [11 15 17 21 28 33 39 35 34 44 46 43] 0 1 2 3 4 5 6 7 8 9 10 11
The offsets of where each small array starts in the concatenated array would be: [0, 3, 5, 8]. So,to cut to the chase, you can compute offsets from concatenated arrays by computing the cumulative sums of their lengths. That is what the mysterious documentation code did.
On the other hand, you could write a custom function like so:
def compute_offsets(arr): # arr is a numpy array of lengths n = len(arr) result = np.zeros(n, dtype=np.int64) result = 0 for i in range(1, n): result[i] = result[i-1] + arr[i-1] return result
Which approach is better? If you use the built-in cumsum() function, you take on a dependency (which could break your system if the container library changed implementation or interface) and the intent of the calling code is not obvious (although you could add a comment). If you write a custom function, you have extra lines of code, and there’s a chance you could have a bug in even the simplest code.
The reason this particular example got me thinking was that in my opinion, the pros and cons of library vs. custom are just about evenly balanced for this code. Choosing between using a library function or writing a custom function is rarely a black or white decision. One of the benefits of experience in software development is that you gain an intuitive sense of when to use library code and when to write custom code.
Horst P. Horst (1906-1999) was a famous photographer in the 1930s and 1940s. Here are three of his photographs of well-known fashion models of the time. Left: model Agneta Fischer. Center: model Bettina Bolegard. Right: model Georgia Carroll. The looks of Fischer and Bolegard appear very modern but Carroll’s looks seem quite 1940-ish to me (but trust me, I’m no expert on looks of the 1940s or of today either).