1

I've never seen a colon used in this way in python and need an explanation.
Here dim, in_features, and out_features are all int. i is the index counter in a for loop. When I use dim:(i + 1) on it's own it doesn't throw an error, but also doesn't do anything. If I use 0:(i+1) on it's own i get a SyntaxError: illegal target for annotation.
A reproducible snippet is below.

dim = 8
in_features = dim
hidden_dim = 3
out_features = dim * hidden_dim
weight = np.zeros([out_features, in_features])
for i in range(dim):
    weight[i * out_features // dim:(i + 1) * out_features // dim,\
           0:(i + 1) * in_features // dim] \
           = np.random.uniform(size=[out_features // dim, (i + 1) * in_features // dim])
yaho cho
  • 1,779
  • 1
  • 7
  • 19
user3496060
  • 800
  • 10
  • 20
  • 3
    I think this is just `weight[ a : b : c]` where `a,b,c` are rather complicated expressions. A reproducible example would help. The comma rather than the colon is what seems mysterious. That makes `b` a tuple, which doesn't make sense in a slice. – John Coleman Jun 11 '19 at 20:55
  • ok sheesh -- i think maybe i'm just so used to languages like R that have a different order/priority of operations then. Parenthesis would make this more clear i guess! At the very least I think we agree this is confusing notation even if it is allowed syntactically.. – user3496060 Jun 11 '19 at 21:04
  • 4
    With the code, this seems like [numpy array slicing](https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html) with the comma delimiting the dimensions. Thus it parses as `weight[a:b , 0:c]` – John Coleman Jun 11 '19 at 21:08

0 Answers0