Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions samples/Notebooks/powershell/micrograd/Value.format.ps1xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Value</Name>
<ViewSelectedBy>
<TypeName>Value</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>$_.data</ScriptBlock>
</ExpressionBinding>
<NewLine />
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
</Configuration>
163 changes: 163 additions & 0 deletions samples/Notebooks/powershell/micrograd/graphHelper.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#Requires -Modules @{ ModuleName = 'PSQuickGraph'; ModuleVersion = '2.5.0' }
#Requires -Modules @{ ModuleName = 'PSGraphView'; ModuleVersion = '0.1.0' }

function New-ValueNode([Value]$value) {
[pscustomobject]@{
kind = 'value'
value = $value
label = $value.label
data = $value.data
grad = $value.grad
}
}

function New-OperationNode([Value]$value) {
[pscustomobject]@{
kind = 'op'
value = $value
label = $value.operation
operation = $value.operation
data = $value.data
}
}


# visualisation
function New-ExpressionGraph {
param(
[Value]$val,
$graph = $null,
[hashtable]$valueVertices = $null,
[hashtable]$operationVertices = $null
)

if ($null -eq $graph) {
$graph = New-Graph -UseNonUniqueLabels
}

if ($null -eq $valueVertices) {
$valueVertices = @{}
}

if ($null -eq $operationVertices) {
$operationVertices = @{}
}

if (-not $valueVertices.ContainsKey($val)) {
$valueVertices[$val] = Add-Vertex -Graph $graph -Vertex (New-ValueNode $val) -PassThru
}

$valueVertex = $valueVertices[$val]

if ($val.operation) {
if (-not $operationVertices.ContainsKey($val)) {
$operationVertices[$val] = Add-Vertex -Graph $graph -Vertex (New-OperationNode $val) -PassThru
}

$operationVertex = $operationVertices[$val]

Add-Edge -From $operationVertex -To $valueVertex -Graph $graph | Out-Null
}


if ($val.children) {
foreach ($child in $val.children) {
New-ExpressionGraph `
-val $child `
-graph $graph `
-valueVertices $valueVertices `
-operationVertices $operationVertices | Out-Null

$childVertex = $valueVertices[$child]

if ($val.operation) {
Add-Edge -From $childVertex -To $operationVertex -Graph $graph | Out-Null
}
}
}

return $graph
}



# backprop
function New-BackpropagationGraph {
param(
[Value]$val,
$graph = $null,
[hashtable]$valueVertices = $null
)

if ($null -eq $graph) {
$graph = New-Graph -UseNonUniqueLabels
}

if ($null -eq $valueVertices) {
$valueVertices = @{}
}

if (-not $valueVertices.ContainsKey($val)) {
$valueVertices[$val] = Add-Vertex -Graph $graph -Vertex $val -PassThru
}

$currentVertex = $valueVertices[$val]

foreach ($child in $val.children) {
if (-not $valueVertices.ContainsKey($child)) {
New-BackpropagationGraph `
-Graph $graph `
-val $child `
-valueVertices $valueVertices | Out-Null
}

$childVertex = $valueVertices[$child]
Add-Edge -Graph $graph -From $childVertex -To $currentVertex | Out-Null
}

return $graph
}


function Show-ExpressionGraph {
param (
$graph,
$rankdir = "LR"
)

begin {
# node formatting lambda

$vs = {
$node = $_

if ($node.kind -eq 'op') {
@{
shape = 'Ellipse'
label = $node.label
}
}
else {
$name = if ($node.label) { $node.label } else { '' }

@{
shape = 'Record'
label = "{ $name | data $($node.data) | grad $($node.grad) }"
}
}
}

$dot = Export-Graph -Graph $graph -Format Graphviz -GraphScript { @{ rankdir = $rankdir; label = 'Micrograd' } } -VertexScript $vs

$svg = Export-GraphvizView -InputObject $dot -Renderer Dot -As Svg
Display $svg 'image/svg+xml'
}

process {

}

end {

}
}
26 changes: 26 additions & 0 deletions samples/Notebooks/powershell/micrograd/helpers.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Zip {
param(
[Parameter(Mandatory)]
[object[]]$Left,

[Parameter(Mandatory)]
[object[]]$Right
)

$count = [Math]::Min($Left.Count, $Right.Count)

for ($i = 0; $i -lt $count; $i++) {
[pscustomobject]@{
Left = $Left[$i]
Right = $Right[$i]
}
}
}

function Sum-Value {
param([scriptblock]$Selector)

begin { $sum = [Value]::new(0.0, 'loss') }
process { $sum = $sum + (& $Selector $_) }
end { $sum }
}
Loading