r"""Coalesce edge features of duplicate edges via given aggregator in g. Parameters ---------- g : DGLGraph The input graph. edge_maps : List[Tensor] The edge mapping corresponding to each edge type in g. counts : List[Tensor] The number of duplicated edg
(g, edge_maps, counts, aggregator)
| 2313 | |
| 2314 | |
| 2315 | def _coalesce_edge_frame(g, edge_maps, counts, aggregator): |
| 2316 | r"""Coalesce edge features of duplicate edges via given aggregator in g. |
| 2317 | |
| 2318 | Parameters |
| 2319 | ---------- |
| 2320 | g : DGLGraph |
| 2321 | The input graph. |
| 2322 | edge_maps : List[Tensor] |
| 2323 | The edge mapping corresponding to each edge type in g. |
| 2324 | counts : List[Tensor] |
| 2325 | The number of duplicated edges from the original graph for each edge type. |
| 2326 | aggregator : str |
| 2327 | Indicates how to coalesce edge features, could be ``arbitrary``, ``sum`` |
| 2328 | or ``mean``. |
| 2329 | |
| 2330 | Returns |
| 2331 | ------- |
| 2332 | List[Frame] |
| 2333 | The frames corresponding to each edge type. |
| 2334 | """ |
| 2335 | if aggregator == "arbitrary": |
| 2336 | eids = [] |
| 2337 | for i in range(len(g.canonical_etypes)): |
| 2338 | feat_idx = F.asnumpy(edge_maps[i]) |
| 2339 | _, indices = np.unique(feat_idx, return_index=True) |
| 2340 | eids.append(F.zerocopy_from_numpy(indices)) |
| 2341 | |
| 2342 | edge_frames = utils.extract_edge_subframes(g, eids) |
| 2343 | elif aggregator in ["sum", "mean"]: |
| 2344 | edge_frames = [] |
| 2345 | for i in range(len(g.canonical_etypes)): |
| 2346 | feat_idx = edge_maps[i] |
| 2347 | _, indices = np.unique(F.asnumpy(feat_idx), return_index=True) |
| 2348 | _num_rows = len(indices) |
| 2349 | _data = {} |
| 2350 | for key, col in g._edge_frames[i]._columns.items(): |
| 2351 | data = col.data |
| 2352 | new_data = F.scatter_add(data, feat_idx, _num_rows) |
| 2353 | if aggregator == "mean": |
| 2354 | norm = F.astype(counts[i], F.dtype(data)) |
| 2355 | norm = F.reshape( |
| 2356 | norm, (F.shape(norm)[0],) + (1,) * (F.ndim(data) - 1) |
| 2357 | ) |
| 2358 | new_data /= norm |
| 2359 | _data[key] = new_data |
| 2360 | |
| 2361 | newf = Frame(data=_data, num_rows=_num_rows) |
| 2362 | edge_frames.append(newf) |
| 2363 | else: |
| 2364 | raise DGLError( |
| 2365 | "Aggregator {} not regonized, cannot coalesce edge feature in the " |
| 2366 | "specified way".format(aggregator) |
| 2367 | ) |
| 2368 | return edge_frames |
| 2369 | |
| 2370 | |
| 2371 | def to_simple( |