Skip to main content
Tweeted twitter.com/StackMma/status/1197892669940404224
added 151 characters in body
Source Link
Nico A
  • 3.7k
  • 1
  • 16
  • 28

I am trying to design a neural network layer that takes in an array of dimensions n x 100, and produces an array of dimensions n x 100, where each of the n sublists is replaced by the nth. For example, this function would turn {{1,2,3,4,5,...},{4,5,6,7,8,...},{7,8,9,10,11,...}} into {{7,8,9,10,11,...},{7,8,9,10,11,...},{7,8,9,10,11,...}} - the same number of elements, but all replaced with the last sublist.

My original implementation was this:

allRep[] := 
 NetGraph[{"last" -> SequenceLastLayer[], 
   "repl" -> ReplicateLayer[Automatic, "Input" -> 100], 
   "switch" -> ThreadingLayer[#2 &]}, {"last" -> 
    "repl", {NetPort@"Input", "repl"} -> 
    "switch"}]

This actually works, and initializes, but when you try to pair it with any other layer you get an Internal error saying that it can't serialize something to do with the ReplicateLayer:

NetInitialize[NetChain[{allRep[], LongShortTermMemoryLayer[500]}]][
 ConstantArray[0, {2, 100}]]

It seems the problem is that the ReplicateLayer can't figure out to replicate the input n times, even though it says it does in the NetGraph. A simple SequenceLastLayer and ReplicateLayer doesn't automatically infer the first dimension (n) of the output either. Is this possible to do in Mathematica?

I am trying to design a neural network layer that takes in an array of dimensions n x 100, and produces an array of dimensions n x 100, where each of the n sublists is replaced by the nth. For example, this function would turn {{1,2,3,4,5,...},{4,5,6,7,8,...},{7,8,9,10,11,...}} into {{7,8,9,10,11,...},{7,8,9,10,11,...},{7,8,9,10,11,...}} - the same number of elements, but all replaced with the last sublist.

My original implementation was this:

allRep[] := 
 NetGraph[{"last" -> SequenceLastLayer[], 
   "repl" -> ReplicateLayer[Automatic, "Input" -> 100], 
   "switch" -> ThreadingLayer[#2 &]}, {"last" -> 
    "repl", {NetPort@"Input", "repl"} -> 
    "switch"}]

This actually works, and initializes, but when you try to pair it with any other layer you get an Internal error saying that it can't serialize something to do with the ReplicateLayer:

NetInitialize[NetChain[{allRep[], LongShortTermMemoryLayer[500]}]][
 ConstantArray[0, {2, 100}]]

A simple SequenceLastLayer and ReplicateLayer doesn't automatically infer the first dimension (n) of the output. Is this possible to do in Mathematica?

I am trying to design a neural network layer that takes in an array of dimensions n x 100, and produces an array of dimensions n x 100, where each of the n sublists is replaced by the nth. For example, this function would turn {{1,2,3,4,5,...},{4,5,6,7,8,...},{7,8,9,10,11,...}} into {{7,8,9,10,11,...},{7,8,9,10,11,...},{7,8,9,10,11,...}} - the same number of elements, but all replaced with the last sublist.

My original implementation was this:

allRep[] := 
 NetGraph[{"last" -> SequenceLastLayer[], 
   "repl" -> ReplicateLayer[Automatic, "Input" -> 100], 
   "switch" -> ThreadingLayer[#2 &]}, {"last" -> 
    "repl", {NetPort@"Input", "repl"} -> 
    "switch"}]

This actually works, and initializes, but when you try to pair it with any other layer you get an Internal error saying that it can't serialize something to do with the ReplicateLayer:

NetInitialize[NetChain[{allRep[], LongShortTermMemoryLayer[500]}]][
 ConstantArray[0, {2, 100}]]

It seems the problem is that the ReplicateLayer can't figure out to replicate the input n times, even though it says it does in the NetGraph. A simple SequenceLastLayer and ReplicateLayer doesn't automatically infer the first dimension (n) of the output either. Is this possible to do in Mathematica?

Source Link
Nico A
  • 3.7k
  • 1
  • 16
  • 28

Net layer that replicates last element

I am trying to design a neural network layer that takes in an array of dimensions n x 100, and produces an array of dimensions n x 100, where each of the n sublists is replaced by the nth. For example, this function would turn {{1,2,3,4,5,...},{4,5,6,7,8,...},{7,8,9,10,11,...}} into {{7,8,9,10,11,...},{7,8,9,10,11,...},{7,8,9,10,11,...}} - the same number of elements, but all replaced with the last sublist.

My original implementation was this:

allRep[] := 
 NetGraph[{"last" -> SequenceLastLayer[], 
   "repl" -> ReplicateLayer[Automatic, "Input" -> 100], 
   "switch" -> ThreadingLayer[#2 &]}, {"last" -> 
    "repl", {NetPort@"Input", "repl"} -> 
    "switch"}]

This actually works, and initializes, but when you try to pair it with any other layer you get an Internal error saying that it can't serialize something to do with the ReplicateLayer:

NetInitialize[NetChain[{allRep[], LongShortTermMemoryLayer[500]}]][
 ConstantArray[0, {2, 100}]]

A simple SequenceLastLayer and ReplicateLayer doesn't automatically infer the first dimension (n) of the output. Is this possible to do in Mathematica?