Tensorflow: base Python calculation inside an op -
i trying perform datetime-related calculations element-wise on timestamps contained in tensor, using tf.map_fn
. requires conversion datetime , tensorflow-compatible type.
for example, let's want number of month tensor of timestamps:
from datetime import datetime dates = [datetime(2016, 1, 1).timestamp(), datetime(2016, 2, 1).timestamp()] def timestamp_to_month(timestamp): return datetime.fromtimestamp(timestamp).month def month(x, name=none): tf.op_scope([x], name, "month") scope: return tf.map_fn(timestamp_to_month, x, back_prop=false) month(dates)
this not work timestamp
parameter in timestamp_to_month
passed tensor shape []
, not float, , has evaluated.
one solution perform timestamp.eval()
before using actual value, have current session, additional session
parameter, inconvenient.
additionally, month
op fails during graph-building phase, not event during execution, meaning mapped timestamp_to_month
function invoked when building graph. including timestamp.eval()
call therefore trigger execution of graph when want build it.
how can include such base python (or numpy) steps inside op while still deferring execution of graph?
Comments
Post a Comment