def WrapConstants(constants, dtype='float32'): if not isinstance(constants, Tensor): if not isinstance(constants, np.ndarray): constants = np.array(constants, dtype=dtype) tensor = Tensor() tensor.set_value(constants) tensor.shape = constants.shape constants = tensor return constants
def constant(value, dtype=None, shape=None, name=None): if dtype == None: dtype = dtypes.float32 if isinstance(value, np.ndarray): feed = value.astype(dtype) elif isinstance(value, list): feed = np.array(value, dtype) else: feed = np.array([value], dtype) if shape is not None: if feed.size == 1: c = feed[0] feed = np.zeros(shape, dtype) feed.fill(c) else: feed = feed.reshape(shape) tensor = Tensor(name) tensor.shape = list(feed.shape) ws.FeedTensor(tensor, feed) return tensor
def constant(value, dtype=None, shape=None, name=None, verify_shape=False): # determine the data type if dtype == None: dtype = dtypes.float32 if isinstance(value, np.ndarray): feed = value.astype(dtype.as_numpy_dtype) elif isinstance(value, list): feed = np.array(value, dtype.as_numpy_dtype) else: feed = np.array([value], dtype.as_numpy_dtype) # determine the shape if shape is not None: if feed.size == 1: # case 1: broadcast with scalar value c = feed[0] feed = np.zeros(shape, dtype.as_numpy_dtype) feed.fill(c) else: # case 2: reshape directly if verify_shape: if shape is not None: if len(shape) != len(value.shape): raise RuntimeError( 'The constant was limited to {} dimensions, \ while feed a value with {} dimensions.' .format(len(shape), len(value.shape))) for i in xrange(len(shape)): if shape[i] is None: continue if shape[i] != value.shape[i]: raise RuntimeError( 'The shape of constant was limited as (' + ','.join([str(dim) for dim in shape]) + '), ' + 'while feed a value with (' + ','.join([str(dim) for dim in value.shape]) + ').') feed = feed.reshape(shape) # feed to VM tensor = Tensor(name) tensor.shape = list(feed.shape) ws.FeedTensor(tensor, feed) return tensor
def constant(value, dtype=None, shape=None, name=None, verify_shape=False): # determine the data type if dtype == None: dtype = dtypes.float32 if isinstance(value, np.ndarray): feed = value.astype(dtype.as_numpy_dtype) elif isinstance(value, list): feed = np.array(value, dtype.as_numpy_dtype) else: feed = np.array([value], dtype.as_numpy_dtype) # determine the shape if shape is not None: if feed.size == 1: # case 1: broadcast with scalar value c = feed[0] feed = np.zeros(shape, dtype.as_numpy_dtype) feed.fill(c) else: # case 2: reshape directly if verify_shape: if shape is not None: if len(shape) != len(value.shape): raise RuntimeError('The constant was limited to {} dimensions, \ while feed a value with {} dimensions.'. format(len(shape), len(value.shape))) for i in xrange(len(shape)): if shape[i] is None: continue if shape[i] != value.shape[i]: raise RuntimeError('The shape of constant was limited as (' + ','.join([str(dim) for dim in shape]) + '), ' + 'while feed a value with (' + ','.join([str(dim) for dim in value.shape]) + ').') feed = feed.reshape(shape) # feed to VM tensor = Tensor(name) tensor.shape = list(feed.shape) ws.FeedTensor(tensor, feed) return tensor