Canonical way to assert tensor shape

What is the canonical way to assert that a given tensor has the correct shape, i.e. if it is known beforehand what shape it should have? Currently, I use assertions in the following way, which adds a lot of clutter to the code:

assert x.shape == torch.Size([dim1, dim2])

A similar question about IDE based tensor-shape checking has been asked here, but has not received an answer.

assert x.shape == (dim1, dim2) works aswell :slight_smile:


Thanks, that looks a lot nicer already.

1 Like