text stringlengths 1 93.6k |
|---|
TensorDataset + DataLoader because dataloader grabs individual indices of
|
the dataset and calls cat (slow).
|
Source: https://discuss.pytorch.org/t/dataloader-much-slower-than-manual-batching/27014/6
|
"""
|
def __init__(self, *tensors, batch_size=32, shuffle=False):
|
"""
|
Initialize a FastTensorDataLoader.
|
:param *tensors: tensors to store. Must have the same length @ dim 0.
|
:param batch_size: batch size to load.
|
:param shuffle: if True, shuffle the data *in-place* whenever an
|
iterator is created out of this object.
|
:returns: A FastTensorDataLoader.
|
"""
|
assert all(t.shape[0] == tensors[0].shape[0] for t in tensors)
|
self.tensors = tensors
|
self.dataset_len = self.tensors[0].shape[0]
|
self.batch_size = batch_size
|
self.shuffle = shuffle
|
# Calculate # batches
|
n_batches, remainder = divmod(self.dataset_len, self.batch_size)
|
if remainder > 0:
|
n_batches += 1
|
self.n_batches = n_batches
|
def __iter__(self):
|
if self.shuffle:
|
r = torch.randperm(self.dataset_len)
|
self.tensors = [t[r] for t in self.tensors]
|
self.i = 0
|
return self
|
def __next__(self):
|
if self.i >= self.dataset_len:
|
raise StopIteration
|
batch = tuple(t[self.i:self.i+self.batch_size] for t in self.tensors)
|
self.i += self.batch_size
|
return batch
|
def __len__(self):
|
return self.n_batches
|
# <FILESEP>
|
import networkx as nx
|
import json
|
from networkx.readwrite import json_graph
|
from itertools import chain, combinations
|
# from earthquake_loglikelihood import ll_per_graph
|
def powerset(iterable):
|
# "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
s = list(iterable)
|
#
|
len_powerset = 0
|
powerset_vals = chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
return powerset_vals
|
def clean_json_adj_load(file_name):
|
with open(file_name) as d:
|
json_data = json.load(d)
|
H = json_graph.adjacency_graph(json_data)
|
for edge_here in H.edges():
|
del(H[edge_here[0]][edge_here[1]]["id"])
|
return H
|
def clean_json_adj_loads(json_str):
|
json_data = json.loads(json_str)
|
H = json_graph.adjacency_graph(json_data)
|
for edge_here in H.edges():
|
del(H[edge_here[0]][edge_here[1]]["id"])
|
return H
|
def intervention_effects(graph):
|
f = lambda x: x[0].endswith("int")
|
return [x for x in graph.edges() if f(x)]
|
def cause_observation_pairings(graph):
|
f = lambda x: x[0].endswith("★") and x[1].endswith("out")
|
return [x for x in graph.edges() if f(x)]
|
def hidden_cause_pairs(graph):
|
f = lambda x: x[0].endswith("★") and x[1].endswith("★")
|
return [x for x in graph.edges() if f(x)]
|
def completeDiGraph(nodes):
|
"""
|
returns a directed graph with all possible edges for a set of nodes
|
Variables:
|
nodes are a list of strings that specify the node names
|
"""
|
G = nx.DiGraph() # Creates new graph
|
G.add_nodes_from(nodes) # adds nodes to graph
|
edgelist = list(combinations(nodes,2)) # build list of directed edges
|
edgelist.extend([(y,x) for x,y in list(combinations(nodes,2))]) #add symmetric edges
|
edgelist.extend([(x,x) for x in nodes]) # add self-loops
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.