public Tensor[] TFE_TapeGradient(ITape tape,
                                         Tensor[] target,
                                         Tensor[] sources,
                                         Tensor[] output_gradients)
        {
            var target_vec  = MakeTensorIDList(target);
            var sources_vec = MakeTensorIDList(sources);
            var sources_set = sources_vec;

            var seq_array = target;
            var source_tensors_that_are_targets = new UnorderedMap <long, TapeTensor>();

            for (int i = 0; i < target.Length; ++i)
            {
                var target_id = target_vec[i];
                var tensor    = seq_array[i];
                source_tensors_that_are_targets.Add(target_id, TapeTensorFromTensor(tensor));
            }

            if (output_gradients != null)
            {
                throw new NotImplementedException("");
            }
            else
            {
                output_gradients = new Tensor[0];
            }

            var outgrad_vec = MakeTensorList(output_gradients);

            return(tape.ComputeGradient(target_vec, sources_vec, source_tensors_that_are_targets, outgrad_vec));
        }
Beispiel #2
0
        private static void DrawTape(ITape tape)
        {
            int tapeViewLength = 19;

            for (int index = tape.GetIndex() - tapeViewLength; index < tape.GetIndex() + tapeViewLength; ++index)
            {
                Console.Write((char)194);
                Console.Write((char)196);
            }
            Console.WriteLine();

            for (int index = tape.GetIndex() - tapeViewLength; index < tape.GetIndex() + tapeViewLength; ++index)
            {
                char symbol = tape.GetSymbol(index);
                Console.Write((char) 179);
                Console.Write(tape.GetSymbol(index));
            }
            Console.WriteLine();

            for (int index = tape.GetIndex() - tapeViewLength; index < tape.GetIndex() + tapeViewLength; ++index)
            {
                Console.Write((char)193);
                Console.Write((char)196);
            }
            Console.WriteLine();
        }
Beispiel #3
0
 /// <summary>
 /// Create a Dragon tape filesystem and associate it with a virtual tape.
 /// </summary>
 /// <param name="tape">Virtual tape to associate filesystem with.</param>
 public DragonTape(ITape tape)
 {
     if (tape == null)
     {
         throw new ArgumentNullException("tape");
     }
     IsDisposed = false;
     Tape       = tape;
 }
        /// <summary>
        /// Computes the gradient using operations recorded in context of this tape.
        /// </summary>
        /// <param name="target"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public Tensor gradient(Tensor target, Tensor source)
        {
            ITape tape = stop_recording();

            var results = tf.Runner.TFE_TapeGradient(tape,
                                                     new[] { target },
                                                     new[] { source },
                                                     null);

            return(results[0]);
        }
Beispiel #5
0
        /// <summary>
        /// Read the next tape block.
        /// Assumes that the tape reader is positioned just after the block sync byte.
        /// This method will not validate the block and may return invalid blocks.  Use <see cref="Validate"/> to
        /// verify the validity of a block.
        /// </summary>
        /// <param name="tape">Tape reader.</param>
        /// <returns>Tape block object.</returns>
        internal static DragonTapeBlock ReadBlockSynced(ITape tape)
        {
            int blocktype   = tape.ReadByte();
            int blocklength = tape.ReadByte();
            var data        = new byte[blocklength];

            for (int i = 0; i < blocklength; i++)
            {
                data[i] = tape.ReadByte();
            }
            int checksum = tape.ReadByte();

            return(CreateBlock((DragonTapeBlockType)blocktype, data, 0, blocklength, checksum));
        }
Beispiel #6
0
        public Processor(ITape tape, IInstructionTable table)
        {
            if(null == tape)
            {
                throw new ArgumentNullException("Tape must not be null");
            }

            if(null == table)
            {
                throw new ArgumentNullException("Instruction table must not be null");
            }

            mTable = table;
            mTape = tape;
            mNextState = "START";
            Tick = 0;
        }
        /// <summary>
        /// Pushes a new tape onto the tape stack.
        /// </summary>
        private void _push_tape()
        {
            if (_recording)
            {
                throw new ValueError("Tape is still recording, This can happen if you try to " +
                                     "re-enter an already-active tape.");
            }

            if (_tape == null)
            {
                _tape = new Tape(_persistent, _watch_accessed_variables);
            }
            else
            {
                tf.GetTapeSet().Add(_tape);
            }

            _recording = true;
        }
Beispiel #8
0
        /// <summary>
        /// Synchronize tape.
        /// Reads the tape until a sufficiently long sequence of leader bytes and a single sync byte has been read.
        /// </summary>
        /// <param name="tape">Object for reading bits from tape.</param>
        /// <param name="minLeaderLength">The minimum required number of leader bits before the sync byte.  Can be 0.</param>
        internal static void Sync(ITape tape, int minLeaderLength)
        {
            if (tape == null)
            {
                throw new ArgumentNullException("tape");
            }
            if (minLeaderLength < 0)
            {
                throw new ArgumentOutOfRangeException("minLeaderLength", minLeaderLength, "minLeaderLength cannot be negative");
            }

            int  leaderlength = 0;
            int  leaderpos    = 0;
            int  synclength   = 0;
            int  syncpos      = 0;
            bool bit;

            while (synclength < 8)
            {
                bit = tape.ReadBit();

                if (leaderlength >= minLeaderLength && bit == ((SyncByte & (0x80 >> syncpos)) != 0))
                {
                    synclength++;
                    syncpos   = (syncpos + 1) % 8;
                    leaderpos = (leaderpos + 1) % 8;
                }
                else if (bit == ((LeaderByte & (0x80 >> leaderpos)) != 0))
                {
                    synclength = 0;
                    syncpos    = 0;
                    leaderlength++;
                    leaderpos = (leaderpos + 1) % 8;
                }
                else
                {
                    synclength = leaderlength = 0;
                    syncpos    = leaderpos = 0;
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Write a block to tape.
        /// This will write the block to tape, including leader, sync byte and trailing leader.
        /// </summary>
        /// <param name="tape">Tape writer.</param>
        /// <param name="sync">When set, the tape is assumed to be synchronized and only a short leader is output.</param>
        public void WriteBlock(ITape tape, bool sync)
        {
            /* Write leader and sync byte. */
            var leaderlength = sync ? DefaultShortLeaderLength : DefaultLongLeaderLength;

            while (leaderlength-- > 0)
            {
                tape.WriteByte(LeaderByte);
            }
            tape.WriteByte(SyncByte);

            /* Write the block header, payload and checksum. */
            tape.WriteByte((byte)BlockType);
            tape.WriteByte((byte)Length);
            for (var i = 0; i < Length; i++)
            {
                tape.WriteByte(Data[i]);
            }
            tape.WriteByte((byte)Checksum);

            /* Write the trailing leader byte. */
            tape.WriteByte(LeaderByte);
        }
Beispiel #10
0
 /// <summary>
 /// Create a Dragon tape filesystem and associate it with a virtual tape.
 /// </summary>
 /// <param name="tape">Virtual tape to associate filesystem with.</param>
 public DragonTape(ITape tape)
 {
     if (tape == null) throw new ArgumentNullException("tape");
     IsDisposed = false;
     Tape = tape;
 }
Beispiel #11
0
		public bool CompleteContentEquals (ITape t)
		{
			return Equals (t as PackedExponentTape, ExponentComparison.TotalEquality, false, false);
		}
Beispiel #12
0
 /// <summary>
 /// Pops the given tape in the stack.
 /// </summary>
 /// <param name="tape"></param>
 public void PopTape(ITape tape)
 {
     tf.GetTapeSet().Remove(tape);
 }
Beispiel #13
0
 public bool CompleteContentEquals(ITape t)
 {
     return(CompleteContentEquals(t as LimitedBasicTape));
 }
Beispiel #14
0
 public bool CompleteContentEquals(ITape t)
 {
     return(Equals(t as PackedExponentTape, ExponentComparison.TotalEquality, false, false));
 }
Beispiel #15
0
 /// <summary>
 /// Read the next block from the tape.
 /// This method will not validate the block read and may return an invalid block.  Use <see cref="Validate">Validate</see>
 /// to verify the validity of the returned block.
 /// </summary>
 /// <param name="tape">Virtual tape.</param>
 /// <param name="minLeaderLength">The minimum leader length (in bits) required for synchronizing the block.  Can be <value>0</value>.</param>
 /// <returns>Tape block object.</returns>
 public static DragonTapeBlock ReadBlock(ITape tape, int minLeaderLength)
 {
     Sync(tape, minLeaderLength);
     return(ReadBlockSynced(tape));
 }
Beispiel #16
0
 public void PopTape(ITape tape)
 {
     throw new NotImplementedException();
 }
Beispiel #17
0
		public bool CompleteContentEquals (ITape t)
		{
			return CompleteContentEquals (t as BasicTape);
		}
Beispiel #18
0
 /// <summary>
 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <filterpriority>2</filterpriority>
 public void Dispose()
 {
     if (!IsDisposed)
     {
         if (Tape != null && Tape is IDisposable) ((IDisposable)Tape).Dispose();
         Tape = null;
         IsDisposed = true;
     }
 }