Ben Chuanlong Du's Blog

It is never too late to learn.

Collections in Java

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Oracel Java Tutorial on Collections

ArrayList

  1. You can point an element of Array or ArrayList to null, but remember that a null reference cannot invoke any method. For example, if you …

Broadcast Arrays in Numpy

Tips and Traps

  1. The broadcast concept in numpy is essentially a way to "virtually" duplicate data in a numpy array so that it is "virtually" reshaped to be compatible with another numpy array for a certain operation. Do not confused yourself about it with the broadcast concept in Spark which sends a full copy of a (small) DataFrame to each work node for BroadCastJoin

Convert a Tensor to a Numpy Array or List in PyTorch

Tips

There are multiple ways to convert a Tensor to a numpy array in PyTorch. First, you can call the method Tensor.numpy.

my_tensor.numpy()

Second, you can use the function numpy.array.

import numpy as np
np.array(my_tensor)

It is suggested that you use the function numpy.array to convert a Tensor to a numpy array. The reason is that numpy.array is more generic. You can also use it to convert other objects (e.g., PIL.Image) to numpy arrays while those objects might not have a method named numpy

Array in Kotlin

Comments

  1. An array in Java is not an Iterable (due to design reasons). There is no super class of array and Iterable either in Java. If you want a method to support both array and Iterable as the parameter, you need to overload it (for both array and Iterable.) It is the same situation in Kotlin as Kotlin is mostly Java.

Collections in Kotlin

Fold vs Reduce

fold takes an initial value, and the first invocation of the lambda you pass to it will receive that initial value and the first element of the collection as parameters. reduce doesn't take an initial value, but instead starts with the first element of the collection as the accumulator (called sum in the following example).