Beispiel #1
0
 /// <summary>
 /// Create Value object from dense input as batch data.
 /// </summary>
 /// <typeparam name="T">float or double</typeparam>
 /// <param name="sampleShape">shape of the Value</param>
 /// <param name="batch">batch of data</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">readonly value</param>
 /// <returns>the value</returns>
 public static Value CreateBatch <T>(NDShape sampleShape, IEnumerable <T> batch, DeviceDescriptor device, bool readOnly = false)
 {
     if (typeof(T).Equals(typeof(float)))
     {
         var inputVector = Helper.AsFloatVector(batch);
         return(Value._CreateBatchFloat(sampleShape, inputVector, device, readOnly));
     }
     else if (typeof(T).Equals(typeof(double)))
     {
         var inputVector = Helper.AsDoubleVector(batch);
         return(Value._CreateBatchDouble(sampleShape, inputVector, device, readOnly));
     }
     else
     {
         throw new ArgumentException("The data type " + typeof(T).ToString() + " is not supported. Only float or double is supported by CNTK.");
     }
 }
Beispiel #2
0
 /// <summary>
 /// Loads a model from memory buffer.
 /// </summary>
 /// <param name="modelBuffer"></param>
 /// <param name="computeDevice"></param>
 /// <returns></returns>
 public static Function Load(byte[] modelBuffer, DeviceDescriptor computeDevice)
 {
     return(_Load(modelBuffer, (uint)modelBuffer.Length, computeDevice));
 }
 /// <summary>
 /// Create Value object from dense input as batch data.
 /// </summary>
 /// <typeparam name="T">float or double</typeparam>
 /// <param name="sampleShape">shape of the Value</param>
 /// <param name="batch">batch of data</param>
 /// <param name="device">device</param>
 /// <param name="readOnly">readonly value</param>
 /// <returns>the value</returns>
 public static Value CreateBatch <T>(NDShape sampleShape, IEnumerable <T> batch, DeviceDescriptor device, bool readOnly = false)
 {
     T[] batchAsArray = batch.ToArray();
     return(CreateBatch <T>(sampleShape, batchAsArray, 0, batchAsArray.Count(), device, readOnly));
 }
Beispiel #4
0
        /// <summary>
        /// Evaluates the Function using provided inputs.
        /// </summary>
        /// <param name="inputs"></param>
        /// <param name="outputs"></param>
        /// <param name="createPersistentOutputValues"></param>
        /// <param name="computeDevice"></param>
        public void Evaluate(IDictionary <Variable, Value> inputs, IDictionary <Variable, Value> outputs, bool createPersistentOutputValues, DeviceDescriptor computeDevice)
        {
            // Evaluate the rootFunction.
            var inMap  = new UnorderedMapVariableValuePtr();
            var outMap = new UnorderedMapVariableValuePtr();

            foreach (var p in inputs)
            {
                inMap.Add(p.Key, p.Value);
            }

            foreach (var p in outputs)
            {
                outMap.Add(p.Key, p.Value);
            }

            _Evaluate(inMap, outMap, computeDevice);

            foreach (var p in outMap)
            {
                if (createPersistentOutputValues && (outputs[p.Key] == null))
                {
                    outputs[p.Key] = p.Value.DeepClone();
                }
                else
                {
                    // for shared_ptr<Value>, the p.Value returns a copy, so it is safe to use it directly in outputs.
                    outputs[p.Key] = p.Value;
                }
            }
        }
Beispiel #5
0
 /// <summary>
 /// Loads a model from file.
 /// </summary>
 /// <param name="filepath"></param>
 /// <param name="computeDevice"></param>
 /// <returns></returns>
 public static Function Load(string filepath, DeviceDescriptor computeDevice)
 {
     return(_Load(filepath, computeDevice));
 }
Beispiel #6
0
 /// <summary>
 /// Loads a model from memory buffer.
 /// </summary>
 /// <param name="modelBuffer"></param>
 /// <param name="computeDevice"></param>
 /// <returns></returns>
 public static Function Load(byte[] modelBuffer, DeviceDescriptor computeDevice, ModelFormat format = ModelFormat.CNTKv2)
 {
     return(_Load(modelBuffer, (uint)modelBuffer.Length, computeDevice, format));
 }
Beispiel #7
0
 /// <summary>
 /// Evaluates the Function using provided inputs.
 /// </summary>
 /// <param name="inputs"></param>
 /// <param name="outputs"></param>
 /// <param name="computeDevice"></param>
 public void Evaluate(IDictionary <Variable, Value> inputs, IDictionary <Variable, Value> outputs, DeviceDescriptor computeDevice)
 {
     Evaluate(inputs, outputs, false, computeDevice);
 }
 public UniversalStyleTransferModel(DeviceDescriptor device, byte[] modelWithWeights)
 {
     this.device  = device;
     modelWeights = modelWithWeights;
 }
Beispiel #9
0
 /// <summary>
 /// Loads a model from file.
 /// </summary>
 /// <param name="filepath"></param>
 /// <param name="computeDevice"></param>
 /// <returns></returns>
 public static Function Load(string filepath, DeviceDescriptor computeDevice, ModelFormat format = ModelFormat.CNTKv2)
 {
     return(_Load(filepath, computeDevice, format));
 }
Beispiel #10
0
        /// <summary>
        /// ニューラルネットワークモデルの入力設定
        /// </summary>
        /// <param name="width">入力画像の幅</param>
        /// <param name="height">入力画像の高さ</param>
        /// <param name="channel">入力画像のチャンネル</param>
        /// <param name="dataType">データタイプ</param>
        /// <param name="device">CPU/GPUの使用設定</param>
        /// <returns></returns>
        public static Function Input(int width, int height, int channel, CNTK.DataType dataType = DataType.Float, CNTK.DeviceDescriptor device = null, string name = "")
        {
            if (device == null)
            {
                // CPU or GPUの設定(GPU優先)
                GetDevice();
            }
            else
            {
                _device = device;
            }

            _dataType = dataType;

            return(CognitiveCSharpKit.Data.Variable(width, height, channel, dataType, name));
        }
Beispiel #11
0
 /// <summary>
 /// create a constant
 /// </summary>
 /// <param name="shape">shape of the constant</param>
 /// <param name="initValue">initial value</param>
 /// <param name="device">device</param>
 /// <param name="name">name</param>
 public Constant(NDShape shape, float initValue, DeviceDescriptor device, string name = "") :
     this(shape, DataType.Float, initValue, device, name)
 {
 }
Beispiel #12
0
        // Todo: set default parameters  = DeviceDescriptor.UseDefaultDevice()
        // Todo: how to define DeviceDescriptor?? Better just enum or something easier
        // Todo: use Variable as method, string as extension method.
        public void Evaluate(Dictionary <string, Value> arguments, Dictionary <string, Value> outputs, DeviceDescriptor computeDevice)
        {
            if (rootFunction == null)
            {
                throw new NullReferenceException("No rootFunction is loaded. Please load the rootFunction first before evaluation.");
            }

            // Evaluate the rootFunction.
            var argMap = new UnorderedMapVariableValuePtr();

            foreach (var p in arguments)
            {
                var variable = rootFunction.Arguments.Where(v => string.Equals(v.Name, p.Key)).FirstOrDefault();
                if (variable == null)
                {
                    throw new KeyNotFoundException("No input variable '" + p.Key + "' found.");
                }
                argMap.Add(variable, p.Value);
            }

            var outMap = new UnorderedMapVariableValuePtr();

            foreach (var p in outputs)
            {
                var variable = rootFunction.Outputs.Where(v => string.Equals(v.Name, p.Key)).FirstOrDefault();
                if (variable == null)
                {
                    throw new KeyNotFoundException("No output variable '" + p.Key + "' found.");
                }
                outMap.Add(variable, p.Value);
            }

            rootFunction.Evaluate(argMap, outMap, computeDevice);

            foreach (var p in outMap)
            {
                outputs[p.Key.Name] = p.Value;
            }
        }
Beispiel #13
0
 // Create Value based on onehot input
 // Todo: could this be a extension to Value class??
 // Todo: use Variable instead of varName. VarName as extension method
 public Value CreateValue <T>(string varName, List <List <long> > oneHotIndex, DeviceDescriptor computeDevice)
 {
     throw new NotImplementedException("Not implemented");
 }
Beispiel #14
0
 // Create Value based on sparse input
 // Todo: could this be a extension to Value class??
 // Todo: use Variable instead of varName. VarName as extension method
 public Value CreateValue <T>(string varName, List <List <T> > data, List <List <long> > indexes, List <List <long> > nnzCounts, DeviceDescriptor computeDevice)
 {
     throw new NotImplementedException("Not implemented");
 }