5

I'm trying to run a python code. This code have an '@' ( at) operator. I never saw it before, and Google returns no hits.

init = model.potentials[key].init_weights(model)
if init.ndim==2:
    U, S, Vt = np.linalg.svd(init, False)
    init = U@Vt

It says the following error:

init = U@Vt
        ^
SyntaxError: invalid syntax

I'm using Python 2.7 to compile it. Do anyone know about this operator ?

bratao
  • 1,980
  • 3
  • 21
  • 38

1 Answers1

11

The @ operator was proposed in PEP 465 and adopted into Python 3.5. You can't use it because you are using an older branch of Python. It is used to multiply matrixes (usually, but you can actually make @ do anything). You can use numpy.dot() instead if you are working with NumPy arrays, like this:

init = model.potentials[key].init_weights(model)
if init.ndim==2:
    U, S, Vt = np.linalg.svd(init, False)
    init = np.dot(U, Vt)
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Strictly speaking, `@` is `np.matmul`, not `np.dot` – Eric Jun 23 '16 at 06:02
  • 1
    `np.matmul` itself is new. In this 2d context `np.dot` is equivalent. It describes `dot` as, `alternative matrix product with different broadcasting rules`. – hpaulj Jun 23 '16 at 07:30
  • Here is a link to documentation on np.matmul: http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.matmul.html – Will Martin Jun 23 '16 at 09:08