You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using some tensor flow linear algebra operators require input tensors to be 2d. This can lead to problems during model evaluation when Random Variables are defined as 1d tensors. Very likely related to #222 and #193:
Suppose you want to use tensorflow matrix multiple to supply inputs into your likelihood:
gives this output (some error lines suppressed and error thrown at the line mu=):
shape of beta: (3,)
shape of data: (1000, 3)
...
InvalidArgumentError: In[1] is not a matrix. Instead it has shape [3] [Op:MatMul]
``
The calculation of mu would have been ok using PyMC3 and theano.dot since we have conformability and theano would treat the 1d tensor as a matrix for purposes of the multiplication. So I have to do a tf.reshape to make beta into a 2d tensor and use that in the matrix multiply op:
While mu is created it is now 1000x1 (rather than (1000,)) and sampling fails (with the error occurring at the obs= line) since the likelihood expected a 1d tensor:
shape of beta: (3,)
shape of data: (1000, 3)
shape of beta matrix: (3, 1)
shape of mu: (1000, 1)
...
EvaluationError: The values supplied to the distribution 'toy_pm4/obs' are not consistent with the distribution's shape (dist_shape).
dist_shape = batch_shape + event_shape = TensorShape([1000, 1])
Supplied values shape = TensorShape([1000]).
A values array is considered to have a consistent shape with the distribution if two conditions are met.
1) It has a greater or equal number of dimensions when compared to the distribution (len(value.shape) >= len(dist_shape))
2) The values shape is compatible with the distribution's shape: dist_shape.is_compatible_with( value_shape[(len(values.shape) - len(dist_shape)):])
To get the model to run, you need to do an additional reshape of mu (really a squeeze) to make mu a 1d tensor:
@roblem, this is a tensorflow problem. tensorflow, unlike theano has no general purpose dot operator, that decides whether to delegate its calculations to matmul or matvec. Tensorflow forces the choice on the users. In your case, you want to multiply a matrix against a vector so you should just do tf.linalg.matvec instead of tf.linalg.matmul.
On the other hand, when you reshaped beta to be a matrix, your sampling fails because you didn't reshape the observeds to have shape (1000, 1). It actually says this in the error message. You supplied values of shape (1000,) but we expected values with shape (1000, 1). For all purposes, a tensor that has shape (1000, 1) is completely different than one that has shape (1000,), so you'll have to be careful with shape handling (pymc3 handled this recklessly, and that lead to very big problems down the line).
Using some tensor flow linear algebra operators require input tensors to be 2d. This can lead to problems during model evaluation when Random Variables are defined as 1d tensors. Very likely related to #222 and #193:
Suppose you want to use tensorflow matrix multiple to supply inputs into your likelihood:
gives this output (some error lines suppressed and error thrown at the line
mu=
):The calculation of mu would have been ok using PyMC3 and
theano.dot
since we have conformability and theano would treat the 1d tensor as a matrix for purposes of the multiplication. So I have to do a tf.reshape to make beta into a 2d tensor and use that in the matrix multiply op:While mu is created it is now 1000x1 (rather than (1000,)) and sampling fails (with the error occurring at the obs= line) since the likelihood expected a 1d tensor:
To get the model to run, you need to do an additional reshape of mu (really a squeeze) to make mu a 1d tensor:
Also, adding a dimension to beta in plate (e.g
beta = yield pm4.Normal("beta", 0, 10, plate=(3,1))
) doesn't fix the problem.The text was updated successfully, but these errors were encountered: