Exemple #1
0
        private Constant InGeneric <T>(T value, int?[] shape = null, KerasSharp.DataType?dtype = null, string name = null)
        {
            if (dtype == null)
            {
                dtype = floatx();
            }

            CNTKDataType _dtype = In(dtype.Value);

            int[] _shape = shape.Select(x => x.Value).ToArray();

            Constant c = _constant(value, shape, dtype, name);

            if (!Out(c.Shape).IsEqual(_shape))
            {
                throw new Exception();
            }

            if (dtype == null || c.DataType == _dtype)
            {
                return(c);
            }

            throw new Exception();
        }
Exemple #2
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));
        }
        public Tensor placeholder(int?[] shape = null, int?ndim = null, DataType?dtype = null, bool sparse = false, string name = null)
        {
            log(new { shape, ndim, dtype, sparse, name });

            // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py
            if (shape == null)
            {
                if (ndim != null)
                {
                    shape = new int?[ndim.Value];
                }
            }

            var cntk_shape = shape.Select(s => s == null ? NDShape.FreeDimension : s.Value);

            //if (dynamic_axis_num > len(cntk_shape)
            //{
            //    raise ValueError('CNTK backend: creating placeholder with '
            //            '%d dimension is not supported, at least '
            //            '%d dimensions are needed.'
            //            % (len(cntk_shape, dynamic_axis_num)))
            //}

            if (dtype == null)
            {
                dtype = floatx();
            }

            if (name is null)
            {
                name = String.Empty;
            }

            // cntk_shape = cntk_shape[dynamic_axis_num:]

            NDShape      _shape = NDShape.CreateNDShape(cntk_shape);
            CNTKDataType _dtype = In(dtype.Value);
            Variable     v      = Variable.InputVariable(shape: _shape, dataType: _dtype, isSparse: sparse, name: name);

            var x = Out(v);

            x._keras_shape         = shape;
            x._uses_learning_phase = false;
            return(x);
        }
        public Constant InGeneric <T>(T value, int[] shape = null, KerasSharp.DataType?dtype = null, string name = null)
        {
            if (dtype == null)
            {
                dtype = floatx();
            }

            CNTKDataType _dtype = In(dtype.Value);

            int[] _shape;
            if (shape == null)
            {
                if (value is Array)
                {
                    _shape = (value as Array).GetLength();
                }
                else
                {
                    _shape = new int[] { };
                }
            }
            else
            {
                _shape = shape;
            }

            Constant c = _constant(value, _shape, _dtype, name);

            if (!Out(c.Shape).IsEqual(_shape))
            {
                throw new Exception();
            }

            if (dtype == null || c.DataType == _dtype)
            {
                return(c);
            }

            throw new Exception();
        }
        public Constant _constant <T>(T value, int[] shape, CNTKDataType dtype, string name)
        {
            if (value is Array)
            {
                NDArrayView x = In(value as Array, Out(dtype));
                if (name != null)
                {
                    return(new Constant(x, name));
                }
                else
                {
                    return(new Constant(x));
                }
            }

            if (shape == null || shape.Length == 0)
            {
                if (dtype == CNTKDataType.Double)
                {
                    return(Constant.Scalar <double>(value.To <double>(), device: DeviceDescriptor.CPUDevice));
                }
                if (dtype == CNTKDataType.Float)
                {
                    return(Constant.Scalar <float>(value.To <float>(), device: DeviceDescriptor.CPUDevice));
                }
            }

            if (name == null)
            {
                return(new Constant(shape: InShape(shape), dataType: dtype,
                                    initValue: (dynamic)value, device: DeviceDescriptor.CPUDevice));
            }

            return(new Constant(shape: InShape(shape), dataType: dtype,
                                initValue: (dynamic)value, device: DeviceDescriptor.CPUDevice, name: name));
        }
Exemple #6
0
 public static Variable Variable(Variable var, CNTK.DataType dataType = DataType.Float)
 {
     return(CNTKLib.InputVariable(var.Shape, dataType));
 }
Exemple #7
0
 public static Variable Variable(int width, int height, int channel, CNTK.DataType dataType = DataType.Float, string name = "")
 {
     return(CNTKLib.InputVariable(new int[] { width, height, channel }, dataType, name));
 }
Exemple #8
0
 /// <summary>
 /// 入力データ
 /// </summary>
 /// <param name="dataCount"></param>
 /// <returns></returns>
 public static Variable Variable(int dataLength, CNTK.DataType dataType = DataType.Float)
 {
     return(CNTKLib.InputVariable(new int[] { dataLength }, dataType));
 }
 /// <summary>
 /// データタイプの設定
 /// </summary>
 /// <param name="dataType"></param>
 public static void SetDataType(CNTK.DataType dataType)
 {
     _dataType = dataType;
 }
Exemple #10
0
 public static Variable LabelData(this Function function, CNTK.DataType dataType = DataType.Float)
 {
     return(CNTKLib.InputVariable(function.Output.Shape, dataType));
 }