0

What is the proper way to add a parameter and a numpy generated array in Dynet? For instance, when I try the following:

from dynet import *
import numpy as np
import numpy.matlib as npm

model = ParameterCollection()
....
LayerA = model.add_parameters((1, 100))
LayerA = model.add_parameters((1, 100))
...
normal = np.random.normal(npm.zeros((1, 100)), npm.ones((1, 100)))
Layer = LayerA + cmult(LayerB,normal) # cmult is Dynet component-wise multiplication
...
output = Layer.expr() * activation(...)

it gives me the following error,

TypeError: Argument 'x' has incorrect type (expected _dynet.Expression, got _dynet.Parameters)

So, I tried this instead of what I have above,

Layer = LayerA + cmult(LayerB.expr(),normal)

But it gives me the following error,

TypeError: Argument 'y' has incorrect type (expected _dynet.Expression, got numpy.ndarray)

So I thought I might need to convert the numpy array to a dynet expression,

Layer = LayerA.expr() + cmult(LayerB.expr(),inputTensor(normal))
...
output = Layer * activation(...)

and it passes without any error, but it complains somewhere else where I call output.scalar_value(),

terminate called after throwing an instance of 'std::runtime_error'
what():  Input tensor has more than one element, cannot convert to scalar.
Aborted (core dumped)
user3639557
  • 4,791
  • 6
  • 30
  • 55
  • 1
    `.scalar_value()` only works for an expression that is a single value, use `.value()` instead. – mcoav May 22 '18 at 13:27
  • Thank you! Well spotted - Regarding the conversion itself: it seems a bit weird to generate a numpy array and then convert it to an expression. May be there is a better way? – user3639557 May 22 '18 at 13:49
  • No, expression arithmetic can only occur between expression objects (that is why you also need to convert your parameter with `LayerA.expr()` or `dy.parameter(LayerA)`. Same for numpy arrays. Expressions are what constitute your computation graph, so you need to do this conversion each time you renew the computation graph (`dy.renew_cg()`). – mcoav May 22 '18 at 13:55
  • I see - odd question: Is there a way to convert a parameter to expression and then convert it back to a parameter? – user3639557 May 22 '18 at 13:57
  • You could compute the value of an expression (to obtain e.g. an array) and use it to initialize a new Parameter object, but I do not think you can retrieve directly the original parameter. What would you want to achieve? – mcoav May 22 '18 at 14:04
  • Nothing very specific. I am new to Dynet, and just want to make sure things I do are not redundant :) - your comments are very helpful, thanks! – user3639557 May 22 '18 at 14:06

0 Answers0