npto_var documented and with dtype argument
@@ -4,17 +4,29 @@ import numpy as np
import random
-def np_to_var(X, requires_grad=False, **var_kwargs):
+def np_to_var(X, requires_grad=False, dtype=None, **var_kwargs):
"""
Convenience function to transform numpy array to `torch.autograd.Variable`.
Converts `X` to ndarray using asarray if necessary.
+ Parameters
+ ----------
+ X: ndarray or list
+ Input arrays
+ requires_grad: bool
+ passed on to Variable constructor
+ dtype: numpy dtype, optional
+ var_kwargs:
+ passed on to Variable constructor
+
Returns
-------
var: `torch.autograd.Variable`
"""
X = np.asarray(X)
+ if dtype is not None:
+ X = X.astype(dtype)
return Variable(th.from_numpy(X), requires_grad=requires_grad, **var_kwargs)
