Skip to content

Main content

PinSage Aggregation Demonstrator Source Code

Browse the implementation files for PinSage Aggregation Demonstrator.
tool.ts typescript
import { parseFiniteNumber } from '../../parse/number/tool';
import { parseInteger } from '../../parse/integer/tool';

type Neighbor = { node: number; visits: number };

export const demonstratePinsageAggregation = (
	featuresInput: unknown,
	neighborsInput: unknown,
	targetInput: unknown,
	selfWeightInput: unknown
): string => {
	const target = parseInteger(targetInput);
	const selfWeight = parseFiniteNumber(selfWeightInput);
	if (
		target === null ||
		target < 0 ||
		selfWeight === null ||
		selfWeight < 0 ||
		typeof featuresInput !== 'string' ||
		typeof neighborsInput !== 'string'
	)
		return '';

	let features: unknown;
	let neighbors: unknown;
	try {
		features = JSON.parse(featuresInput);
		neighbors = JSON.parse(neighborsInput);
	} catch {
		return '';
	}
	if (
		!Array.isArray(features) ||
		!features.length ||
		features.some(
			(row) =>
				!Array.isArray(row) ||
				!row.length ||
				row.some(
					(value) =>
						typeof value !== 'number' || !Number.isFinite(value)
				)
		) ||
		new Set(features.map((row) => row.length)).size !== 1
	)
		return '';
	const typedFeatures = features as number[][];
	if (
		target >= typedFeatures.length ||
		!Array.isArray(neighbors) ||
		!neighbors.length ||
		neighbors.length > 100_000 ||
		neighbors.some(
			(neighbor) =>
				typeof neighbor !== 'object' ||
				neighbor === null ||
				!Number.isSafeInteger((neighbor as Neighbor).node) ||
				(neighbor as Neighbor).node < 0 ||
				(neighbor as Neighbor).node >= typedFeatures.length ||
				!Number.isFinite((neighbor as Neighbor).visits) ||
				(neighbor as Neighbor).visits <= 0
		)
	)
		return '';

	const typedNeighbors = neighbors as Neighbor[];
	if (
		typedFeatures.length * typedFeatures[0].length >
		1_000_000
	)
		return '';
	const visitTotal = typedNeighbors.reduce(
		(sum, neighbor) => sum + neighbor.visits,
		0
	);
	if (!Number.isFinite(visitTotal) || visitTotal <= 0)
		return '';
	const neighborAggregate = typedFeatures[0].map(
		(_, dimension) =>
			typedNeighbors.reduce(
				(sum, neighbor) =>
					sum +
					(neighbor.visits / visitTotal) *
						typedFeatures[neighbor.node][dimension],
				0
			)
	);
	const combined = neighborAggregate.map(
		(value, dimension) =>
			value + selfWeight * typedFeatures[target][dimension]
	);
	if (
		neighborAggregate.some(
			(value) => !Number.isFinite(value)
		) ||
		combined.some((value) => !Number.isFinite(value))
	)
		return '';
	const norm = combined.reduce(
		(result, value) => Math.hypot(result, value),
		0
	);
	if (!Number.isFinite(norm) || norm === 0) return '';
	return JSON.stringify(
		{
			embedding: combined.map((value) =>
				Number((value / norm).toPrecision(10))
			),
			neighborAggregate: neighborAggregate.map((value) =>
				Number(value.toPrecision(10))
			),
			normalizedVisitWeights: typedNeighbors.map(
				(neighbor) => ({
					node: neighbor.node,
					weight: Number(
						(neighbor.visits / visitTotal).toPrecision(10)
					)
				})
			),
			target
		},
		null,
		2
	);
};

export const Tool: TypeTool = {
	name: 'PinSage Aggregation Demonstrator',
	slug: 'graph/pinsage-aggregation',
	intro:
		'Demonstrate PinSage-style importance pooling from random-walk visit counts and node features.',
	tool: demonstratePinsageAggregation,
	i: [
		{
			example: '[[1,0],[0,1],[1,1]]',
			label: 'Node Features',
			placeholder: 'Feature matrix as JSON',
			render: 'textarea',
			type: 'text'
		},
		{
			example: '[{"node":1,"visits":3},{"node":2,"visits":1}]',
			label: 'Sampled Neighbors',
			placeholder: 'Neighbor nodes and visit counts as JSON',
			render: 'textarea',
			type: 'text'
		},
		{
			example: '0',
			label: 'Target Node',
			placeholder: 'Zero-based target index',
			type: 'number'
		},
		{
			example: '1',
			label: 'Self Weight',
			placeholder: 'Nonnegative target-feature weight',
			type: 'number'
		}
	],
	o: [
		{
			example: '{ "embedding": [0.8, 0.6] }',
			label: 'PinSage Aggregation',
			placeholder: 'Embedding will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	cite: [
		{
			title: 'Graph neural network - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/Graph_neural_network'
		},
		{
			title:
				'Graph Convolutional Neural Networks for Web-Scale Recommender Systems',
			url: 'https://arxiv.org/abs/1806.01973'
		}
	]
};
export default Tool;

Description

Browse the implementation files for PinSage Aggregation Demonstrator.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools