def is_tensor(x): if is_theano(): return isinstance(x, T.TensorVariable) elif is_cgt(): return isinstance(x, cgt.core.Node) and x.is_tensor() and not isinstance(x.op, cgt.core.InMemoryData) else: return isinstance(x, tf.Variable)
def is_tensor(x): if is_theano(): return isinstance(x, T.TensorVariable) elif is_cgt(): return isinstance(x, cgt.core.Node) and x.is_tensor() and not isinstance( x.op, cgt.core.InMemoryData) else: return isinstance(x, tf.Variable)
def is_variable(x): if is_theano(): return isinstance(x, theano.gof.Variable) elif is_cgt(): return isinstance(x, cgt.core.Node) elif is_tf(): return isinstance(x, (tf.Tensor, tf.Variable)) else: import ipdb; ipdb.set_trace()
def is_shared(x): """ Check if x is a SharedVariable in Theano, or created via cgt.shared in CGT """ if is_theano(): return isinstance(x, theano.compile.SharedVariable) elif is_cgt(): return isinstance(x, cgt.core.Node) and isinstance(x.op, cgt.core.InMemoryData) else: return hasattr(x, '_tensorfuse_shared')
def is_variable(x): if is_theano(): return isinstance(x, theano.gof.Variable) elif is_cgt(): return isinstance(x, cgt.core.Node) elif is_tf(): return isinstance(x, (tf.Tensor, tf.Variable)) else: import ipdb ipdb.set_trace()
def is_shared(x): """ Check if x is a SharedVariable in Theano, or created via cgt.shared in CGT """ if is_theano(): return isinstance(x, theano.compile.SharedVariable) elif is_cgt(): return isinstance(x, cgt.core.Node) and isinstance( x.op, cgt.core.InMemoryData) else: return hasattr(x, '_tensorfuse_shared')
def get_inputs(outputs): if is_theano(): return theano.gof.graph.inputs(outputs) elif is_cgt(): outputs = list(outputs) return [node for node in cgt.core.topsorted(outputs) if node.is_input()] elif is_tf(): outputs = list(outputs) return [node for node in _tf_topsorted(outputs) if _tf_is_input(node)] else: import ipdb; ipdb.set_trace()
def tensor(dtype, ndim, name=None, fixed_shape=None): """ Create tensor variable from given information """ if is_theano(): #if fixed_shape is not None: # print 'fixed shape ignored in Theano' return T.TensorType(dtype, [False] * ndim)(name) elif is_cgt(): return cgt.tensor(dtype, ndim, name, fixed_shape) else: return tf_var_from_shape(name, fixed_shape, dtype, ndim)
def shape(x): if is_theano(): return x.shape elif is_cgt(): return x.shape elif is_tf(): if isinstance(x, (tf.Tensor, tf.Variable)): return x.shape else: import ipdb; ipdb.set_trace() else: import ipdb; ipdb.set_trace()
def set_value(x, val): """ Get parameter value from a shared variable. """ if is_theano(): x.set_value(val) elif is_cgt(): x.op.set_value(val) elif is_tf(): tf.get_session().run(tf.assign(x, val)) else: import ipdb; ipdb.set_trace()
def get_value(x, borrow=None): """ Get parameter value from a shared variable. """ if is_theano(): borrow = borrow or False return x.get_value(borrow=borrow) elif is_cgt(): return x.op.get_value() else: tf_ensure_init_variables() return tf_get_session().run(x)
def set_value(x, val): """ Get parameter value from a shared variable. """ if is_theano(): x.set_value(val) elif is_cgt(): x.op.set_value(val) elif is_tf(): tf.get_session().run(tf.assign(x, val)) else: import ipdb ipdb.set_trace()
def shape(x): if is_theano(): return x.shape elif is_cgt(): return x.shape elif is_tf(): if isinstance(x, (tf.Tensor, tf.Variable)): return x.shape else: import ipdb ipdb.set_trace() else: import ipdb ipdb.set_trace()
def get_inputs(outputs): if is_theano(): return theano.gof.graph.inputs(outputs) elif is_cgt(): outputs = list(outputs) return [ node for node in cgt.core.topsorted(outputs) if node.is_input() ] elif is_tf(): outputs = list(outputs) return [node for node in _tf_topsorted(outputs) if _tf_is_input(node)] else: import ipdb ipdb.set_trace()
from tensorfuse.config import is_theano, is_cgt, is_tf, floatX from utils import wrap_into_list if is_theano(): import theano import theano.tensor as T elif is_cgt(): import cgt else: import tensorflow as tf from tensorflow.python.framework import ops import numpy as np def is_shared(x): """ Check if x is a SharedVariable in Theano, or created via cgt.shared in CGT """ if is_theano(): return isinstance(x, theano.compile.SharedVariable) elif is_cgt(): return isinstance(x, cgt.core.Node) and isinstance(x.op, cgt.core.InMemoryData) else: return hasattr(x, '_tensorfuse_shared') # In Theano, TensorVariable and SharedVariable are different, and they do not # inherit from each other def is_tensor(x): if is_theano(): return isinstance(x, T.TensorVariable) elif is_cgt(): return isinstance(x, cgt.core.Node) and x.is_tensor() and not isinstance(x.op, cgt.core.InMemoryData)
def broadcastable(x): if is_theano(): return x.broadcastable else: return None
from tensorfuse.config import is_theano, is_cgt, is_tf, is_mxnet if is_theano(): from tensorfuse.backend.theano.tensor.signal import * elif is_cgt(): from tensorfuse.backend.cgt.tensor.signal import * elif is_tf(): from tensorfuse.backend.tensorflow.tensor.signal import * elif is_mxnet(): from tensorfuse.backend.mxnet.tensor.signal import * else: raise ValueError('Unknown backend')