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)