-
Notifications
You must be signed in to change notification settings - Fork 2
initial DNC forward pass. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jpsamaroo
wants to merge
12
commits into
dnc-cleanroom
Choose a base branch
from
jm/dnc_forward_pass
base: dnc-cleanroom
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
99b102b
initial DNC forward pass.
jumerckx 271474f
renamed variables, vectorized stuff and added docstrings
kraftpunk97 5a2498b
added LinearAlgebra
kraftpunk97 cf8b1c0
minor changes to improve readability
kraftpunk97 0d98168
missed a spot
kraftpunk97 70304ec
fixed a typo
kraftpunk97 00b56a8
interface disect: first attempt
kraftpunk97 0b1eaa9
updated Manifest
kraftpunk97 b2043c9
introduced interfacedisect to forward pass.
kraftpunk97 756fbe5
restructed project
kraftpunk97 7363984
added Distributions.jl
kraftpunk97 4c18821
for testing crazy ideas...
kraftpunk97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using Flux: softmax | ||
|
|
||
| cosine_sim(u, v) = (u'v)/(norm(u)*norm(v)) | ||
|
|
||
| # content-based addressing | ||
| """ | ||
| memprobdistrib(MemMat, key, keystrength) | ||
|
|
||
| Defines a normalized probability distribution over the memory locations. | ||
| """ | ||
| function memprobdistrib(M, k, β) | ||
| out = [cosine_sim(k, M[i, :]) for i in 1:size(M, 1)] .* β | ||
| out = softmax(out) | ||
| end | ||
|
|
||
| oneplus(x::AbstractVecOrMat) = log.(exp.(x) .+ 1) | ||
|
|
||
| mutable struct Access | ||
| MemMat # R^{N*W} | ||
| LinkMat # R^{N*N} | ||
| readWts # RH element Array of [0, 1]^N | ||
| wrtWt # WH element Array of (0, 1)^{N} | ||
| usageVec # (0, 1)^{N} | ||
| precedenceWt # (0, 1)^{N} | ||
| readVecs | ||
| numWriteHeads | ||
| numReadHeads | ||
| wordSize | ||
| memorySize | ||
| end | ||
|
|
||
| function Access(memorySize=128, wordSize=20, numReadHeads=1, numWriteHeads=1) | ||
| MemMat = zeros(Float32, memorySize, wordSize) | ||
| LinkMat = zeros(Float32, memorySize, memorySize) | ||
|
|
||
| readWts = rand(Float32, memorySize, numReadHeads) | ||
| fill!(readWts, 1f-6) | ||
|
|
||
| wrtWt = rand(Float32, memorySize, numWriteHeads) | ||
| fill!(wrtWt, 1f-6) | ||
|
|
||
| usageVec = zeros(Float32, memorySize, numWriteHeads) | ||
| fill!(usageVec, 1f-6) | ||
| precedenceWt = zeros(Float32, memorySize) | ||
|
|
||
| readVecs = zeros(Float32, wordSize, numReadHeads) | ||
| fill!(readVecs, 1f-6) | ||
|
|
||
| Access(MemMat, LinkMat, readWts, wrtWt, usageVec, precedenceWt, readVecs, | ||
| numWriteHeads, numReadHeads, wordSize, memorySize) | ||
| end | ||
|
|
||
| """ | ||
| interfacedisect(interfaceVec, writeHead, wordSize, readHeads) | ||
|
|
||
| Disects the interface vector obtained as the output to obtain various memory | ||
| access controls for the DNC. | ||
| """ | ||
| function interfacedisect(interfaceVec, writeHeads, wordSize, readHeads) | ||
| demarcations = cumsum([0, # Starting Index | ||
| readHeads*wordSize, # read keys | ||
| readHeads, # read strengths | ||
| writeHeads*wordSize, # write keys | ||
| writeHeads, # write strengths | ||
| writeHeads*wordSize, # erase vectors | ||
| writeHeads*wordSize, # write vectors | ||
| readHeads, # free gates | ||
| writeHeads, # allocation gates | ||
| writeHeads, # write gates | ||
| readHeads * (1 + 2writeHeads) # read modes | ||
| ]) | ||
|
|
||
|
|
||
| readkeys = interfaceVec[demarcations[1]+1:demarcations[2]] | ||
| readstrengths = oneplus(interfaceVec[demarcations[2]+1:demarcations[3]]) | ||
| writekeys = interfaceVec[demarcations[3]+1:demarcations[4]] | ||
| writestrengths = oneplus(interfaceVec[demarcations[4]+1:demarcations[5]]) | ||
| eraseVec = σ.(interfaceVec[demarcations[5]+1:demarcations[6]]) | ||
| writeVec = interfaceVec[demarcations[6]+1:demarcations[7]] | ||
| freeGts = σ.(interfaceVec[demarcations[7]+1:demarcations[8]]) | ||
| allocGt = σ.(interfaceVec[demarcations[8]+1:demarcations[9]]) | ||
| writeGt = σ.(interfaceVec[demarcations[9]+1:demarcations[10]]) | ||
| readmodes = softmax(interfaceVec[demarcations[10]+1:demarcations[11]]) | ||
|
|
||
| readkeys = reshape(readkeys, wordSize, readHeads) # W * RH | ||
| writekeys = reshape(writekeys, wordSize, writeHeads) # W * WH | ||
| eraseVec = reshape(eraseVec, wordSize, writeHeads) # W * WH | ||
| writeVec = reshape(writeVec, wordSize, writeHeads) # W * WH | ||
| readmodes = reshape(readmodes, (1+2writeHeads), readHeads) # (WH for backward + WH for forward + 1 for content lookup) * RH | ||
|
|
||
| return (readkeys=readkeys, readstrengths=readstrengths, writekeys=writekeys, | ||
| writestrengths=writestrengths, eraseVec=eraseVec, writeVec=writeVec, | ||
| freeGts=freeGts, allocGts=allocGt, writeGts=writeGt, readmodes=readmodes) | ||
| end | ||
|
|
||
| function (access::Access)(interfaceVec) | ||
| # dynamic memory allocation | ||
|
|
||
| interface = interfacedisect(interfaceVec, access.numWriteHeads, access.wordSize, access.numReadHeads) | ||
|
|
||
| memRetVec = prod(1 .- interface[:freeGts]' .* access.readWts, dims=2) # Memory Retention Vector = [0, 1]^{N} | ||
| access.usageVec = (access.usageVec .+ access.wrtWt .- access.usageVec .* access.wrtWt) .* memRetVec | ||
| freelist = sortperm(access.usageVec) # Z^{N} | ||
| allocWt = zeros(access.usageVec) | ||
| @. allocWt[freelist] = (1 - access.usageVec[freelist]) * cumprod([1; access.usageVec[freelist]][1:end-1]) # (0, 1)^{N} | ||
|
|
||
| # writing | ||
| wrtcntWt = memprobdistrib(access.MemMat, interface[:writekeys], interface[:writestrengths]) # Write content weighting = (0, 1)^{N} | ||
| access.wrtWt .= interface[:writeGts] * (interface[:allocGts] * allocWt + (1 - interface[:allocGts])*wrtcntWt) | ||
| @. access.MemMat *= (ones(access.MemMat) - access.wrtWt*interface[:eraseVec]') # First we erase... | ||
| @. access.MemMat += access.wrtWt*interface[:writeVec]' # Then we write. | ||
|
|
||
| # temporal linkage | ||
| eye = Matrix{Float32}(I, size(access.LinkMat)...) | ||
| prevlinkscale = @. 1 - access.wrtWt - access.wrtWt' | ||
| newlink = @. access.wrtWt * access.precedenceWt' | ||
| @. access.LinkMat = (1 - eye) * (prevlinkscale * access.LinkMat + newlink) | ||
|
|
||
| access.precedenceWt = (1 - sum(access.wrtWt)) .* access.precedenceWt .+ access.wrtWt | ||
|
|
||
| # reading | ||
| forwardWts = [access.LinkMat * readWt for readWt in access.readWts] | ||
| backwardWts = [access.LinkMat' * readWt for readWt in access.readWts] | ||
| readcntWts = memprobdistrib.([access.MemMat], readkeys, readstrengths) # Read content weightings | ||
|
|
||
| access.readWts = [π[1].*b .+ π[2].*readcntWts .+ π[3].*f for (π, b, f) in (interface[:readmodes], backwardWts, forwardWts)] | ||
| access.readvecs = [access.MemMat' * W_r for W_r in access.readWts] | ||
|
|
||
| return(readvecs) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #include("DNCLSTM.jl") | ||
| include("Access.jl") | ||
|
|
||
| using Distributions: TruncatedNormal | ||
| using Flux | ||
|
|
||
| mutable struct DNC | ||
| controller | ||
| access::Access | ||
| interfaceVec | ||
| end | ||
|
|
||
| function DNC(memory_size=16, word_size=16, num_read_heads=4, num_write_heads=1, | ||
| hidden_size=64, output_size=4, input_size=4) | ||
| controller_input_size = input_size + word_size * num_read_heads | ||
|
|
||
| # A truncated normal distribution with no elements further than 2σ of μ. | ||
| dist = TruncatedNormal(0, 0.01, -0.02, 0.02) | ||
|
|
||
| # The interface vector of this dimension will only work when the number of | ||
| # write heads is equal to 1. I have yet to figure out how this value changes | ||
| # as the number of write heads change. | ||
| interface_vec_dimensions = word_size * num_read_heads + 3word_size + 5num_read_heads + 3 | ||
| controller_output_size = output_size + interface_vec_dimensions | ||
| controller = LSTM(controller_input_size, controller_output_size) | ||
| access = Access(memory_size, word_size, num_read_heads, num_write_heads) | ||
|
|
||
| interface_vec = rand(dist, interface_vec_dimensions) | ||
| DNC(controller, access, interface_vec) | ||
| end | ||
|
|
||
| function (dnc::DNC)(input) | ||
| readVecs = dnc.access.readVecs | ||
|
|
||
| # flattening readVecs | ||
| readVecs = reshape(readVecs, readVecs |> size |> prod) | ||
|
|
||
| # concatinating them with input to form controller input | ||
| controller_input = [input; readVecs] | ||
|
|
||
| controller_output = dnc.controller(controller_input) | ||
|
|
||
|
|
||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,7 @@ | ||
| module DifferentiableNeuralComputer | ||
|
|
||
| import Flux | ||
| import Zygote | ||
|
|
||
| include("DNCLSTM.jl") | ||
| include("Access.jl") | ||
| include("DNC.jl") | ||
|
|
||
| struct DNC end | ||
|
|
||
| function (dnc::DNC)(x) | ||
| return x | ||
| end | ||
|
|
||
| end # module | ||
| end # module DifferentiableNeuralComputer |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.