How to fix "TypeError: data type not understood" in numpy when creating transformer no peak mask
I'm trying to implement and train a transformer for NMT via a blog post, everything works except I can't create the no peaking mask as I get this error: "TypeError: data type not understood" 

Code: 

target_seq = batch.Python.transpose(0,1)
target_pad = PY_TEXT.vocab.stoi['&lt;pad&gt;']
target_msk = (target_seq != target_pad).unsqueeze(1)
size = target_seq.size(1) # get seq_len for matrix
nopeak_mask = np.triu(np.ones(1, size, size),
k=1).astype('uint8')
nopeak_mask = Variable(torch.from_numpy(nopeak_mask) == 0)
target_msk = target_msk &amp; nopeak_mask


Error message: 

TypeError                                 Traceback (most recent call last)
&lt;ipython-input-36-e19167b74ba0&gt; in &lt;module&gt;()
      4 target_msk = (target_seq != target_pad).unsqueeze(1)
      5 size = target_seq.size(1) # get seq_len for matrix
----&gt; 6 nopeak_mask = np.triu(np.ones(1, size, size),
      7 k=1).astype('uint8')
      8 nopeak_mask = Variable(torch.from_numpy(nopeak_mask) == 0)

~/.local/lib/python3.6/site-packages/numpy/core/numeric.py in ones(shape, dtype, order)
    201 
    202     """
--&gt; 203     a = empty(shape, dtype, order)
    204     multiarray.copyto(a, 1, casting='unsafe')
    205     return a

TypeError: data type not understood


The first input to np.triu should be a tuple of desired sizes instead of a numpy array.

Try:

np.triu((1, size, size), k=1).astype("uint8")

