an Kamal
an Kamal

Reputation: 1

What does " t[..., :1] " in below code means?

Here is the context:

def normal_scale_uncertainty(t, softplus_scale=0.05):
    """Create distribution with variable mean and variance"""
    ts = t[..., :1]
    return tfd.Normal(loc = ts,
                      scale = 1e-3 + tf.math.softplus(softplus_scale * ts))

Upvotes: 0

Views: 348

Answers (1)

irudyak
irudyak

Reputation: 2341

Short answer: ... replaces multiple :.

Long answer: let's have a look at an example.

In [20]: d = np.array([[[i + 2*j + 8*k for i in range(5)] for j in range(4)] for k in range(3)])                        

In [21]: d.shape                                                                                                        
Out[21]: (3, 4, 5)

In [22]: d[:, :, 0]                                                                                                     
Out[22]: 
array([[ 0,  2,  4,  6],
       [ 8, 10, 12, 14],
       [16, 18, 20, 22]])

In [23]: d[..., 0]                                                                                                      
Out[23]: 
array([[ 0,  2,  4,  6],
       [ 8, 10, 12, 14],
       [16, 18, 20, 22]])

In [24]: d[:, :, 0] == d[..., 0]                                                                                        
Out[24]: 
array([[ True,  True,  True,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]])

Can you use d[0, ..., 0] or d[0, ...]? You can. What about d[..., 0, ...]? You'll get an error: IndexError: an index can only have a single ellipsis ('...').

Upvotes: 1

Related Questions