def augment_attr(attr_matrix: np.ndarray, N: int, fill_weight: Union[float, list, np.ndarray] =0.): """Augment a specified attribute matrix. Examples ---------- >>> augment_attr(attr_matrix, 10, fill_weight=1.0) >>> augment_attr(attr_matrix, 10, fill_weight=attr_matrix[-1]) Parameters ---------- attr_matrix: shape [n_nodes, n_nodes]. A Scipy sparse adjacency matrix. N: number of added nodes. node ids [n_nodes, ..., n_nodes+N-1]. fill_weight: float or 1D array. + float scalar: the weight for the augmented matrix + 1D array: repeated N times to augment the matrix. """ if is_scalar_like(fill_weight): M = np.zeros([N, attr_matrix.shape[1]], dtype=attr_matrix.dtype) + fill_weight elif isinstance(fill_weight, (list, np.ndarray)): fill_weight = fill_weight.astype(attr_matrix.dtype, copy=False) M = np.tile(fill_weight, N).reshape([N, -1]) else: raise ValueError(f"Unrecognized input: {fill_weight}.") augmented_attr = np.vstack([attr_matrix, M]) return augmented_attr
def cal_outpus(func: Callable, args: List, kwargs: Dict, type_check: bool = True): if is_list_like(args) and not is_scalar_like(args[0]): if type_check: assert_same_type(*args) return tuple(cal_outpus(func, arg, kwargs, type_check=type_check) for arg in args) return func(args, **kwargs)
def astensor(x, dtype=None, device=None): """Convert input matrices to Tensor or SparseTensor. Parameters: ---------- x: tf.Tensor, tf.Variable, Scipy sparse matrix, Numpy array-like, etc. dtype: The type of Tensor `x`, if not specified, it will automatically using appropriate data type. See `graphgallery.infer_type`. device (:class:`torch.device`, optional): the desired device of returned tensor. Default: if ``None``, uses the current device for the default tensor type (see :func:`torch.set_default_tensor_type`). :attr:`device` will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types. Returns: ---------- Tensor or SparseTensor with dtype: 1. `graphgallery.floatx()` if `x` is floating 2. `graphgallery.intx() ` if `x` is integer 3. `'bool'` if `x` is bool. """ if x is None: return x if dtype is None: dtype = infer_type(x) elif isinstance(dtype, str): ... # TODO elif isinstance(dtype, torch.dtype): dtype = str(dtype).split('.')[-1] else: raise TypeError( f"argument 'dtype' must be torch.dtype or str, not {type(dtype).__name__}." ) if is_th_tensor(x): tensor = x.to(getattr(torch, dtype)) elif sp.isspmatrix(x): tensor = sparse_adj_to_sparse_tensor(x, dtype=dtype) elif isinstance( x, (np.ndarray, np.matrix)) or is_list_like(x) or is_scalar_like(x): tensor = torch.tensor(x, dtype=getattr(torch, dtype), device=device) else: raise TypeError( f'Invalid type of inputs data. Allowed data type `(Tensor, SparseTensor, Numpy array, Scipy sparse tensor, None)`, but got {type(x)}.' ) return tensor.to(device)
def astensor(x, *, dtype=None, device=None): """Convert input matrices to Tensor or SparseTensor. Parameters: ---------- x: any python object dtype: The type of Tensor `x`, if not specified, it will automatically use appropriate data type. See `graphgallery.infer_type`. device (:class:`tf.device`, optional): the desired device of returned tensor. Default: if ``None``, uses the current device for the default tensor type. Returns: ---------- Tensor(s) or SparseTensor(s) with dtype, if dtype is `None`: 1. `graphgallery.floatx()` if `x` is floating 2. `graphgallery.intx() ` if `x` is integer 3. `'bool'` if `x` is bool. """ if x is None: return x if dtype is None: dtype = infer_type(x) elif isinstance(dtype, str): ... # TODO elif isinstance(dtype, tf.dtypes.DType): dtype = dtype.name else: raise TypeError( f"argument 'dtype' must be tensorflow.dtypes.DType or str, not {type(dtype).__name__}." ) with tf.device(device): if is_tensor(x, kind="T"): if x.dtype != dtype: x = tf.cast(x, dtype=dtype) return x elif sp.isspmatrix(x): return sparse_adj_to_sparse_tensor(x, dtype=dtype) elif isinstance( x, (np.ndarray, np.matrix)) or is_list_like(x) or is_scalar_like(x): return tf.convert_to_tensor(x, dtype=dtype) else: raise TypeError( f'Invalid type of inputs data. Allowed data type `(Tensor, SparseTensor, Numpy array, Scipy sparse tensor, None)`, but got {type(x)}.' )