Esempio n. 1
0
        private float ValAccuracy(uint batch_size, Symbol lenet)
        {
            uint val_num = val_data.GetShape()[0];

            uint correct_count = 0;
            uint all_count     = 0;

            uint start_index = 0;

            while (start_index < val_num)
            {
                if (start_index + batch_size > val_num)
                {
                    start_index = val_num - batch_size;
                }
                args_map["data"] =
                    val_data.Slice(start_index, start_index + batch_size).Clone(ctx_dev);
                args_map["data_label"] =
                    val_label.Slice(start_index, start_index + batch_size).Clone(ctx_dev);
                start_index += batch_size;
                NDArray.WaitAll();

                Executor exe = lenet.SimpleBind(ctx_dev, args_map, new XavierInitializer(2));
                exe.Forward(false);
                var     outputs   = exe.Outputs;
                NDArray out_cpu   = outputs[0].Clone(ctx_cpu);
                NDArray label_cpu =
                    val_label.Slice(start_index - batch_size, start_index).Clone(ctx_cpu);
                NDArray.WaitAll();

                float *dptr_out   = out_cpu.GetData();
                float *dptr_label = label_cpu.GetData();
                for (int i = 0; i < batch_size; ++i)
                {
                    float label = dptr_label[i];
                    uint  cat_num = out_cpu.GetShape()[1];
                    float p_label = 0, max_p = dptr_out[i * cat_num];
                    for (int j = 0; j < cat_num; ++j)
                    {
                        float p = dptr_out[i * cat_num + j];
                        if (max_p < p)
                        {
                            p_label = j;
                            max_p   = p;
                        }
                    }
                    if (label == p_label)
                    {
                        correct_count++;
                    }
                }
                all_count += batch_size;
                exe.Dispose();
            }
            return((float)(correct_count * 1.0 / all_count));
        }
        public unsafe EagerTensorV2(NDArray nd, string device_name = "")
        {
            if (nd.typecode == NPTypeCode.String)
            {
                throw new NotImplementedException("Support for NDArray of type string not implemented yet");
            }

            var arraySlice = nd.Unsafe.Storage.Shape.IsContiguous ? nd.GetData() : nd.CloneData();

            _handle = c_api.TF_NewTensor(nd.dtype.as_dtype(),
                                         nd.shape.Select(i => (long)i).ToArray(),
                                         nd.ndim,
                                         new IntPtr(arraySlice.Address),
                                         nd.size * nd.dtypesize,
                                         deallocator: (IntPtr dataPtr, long len, IntPtr args) =>
            {
            }, IntPtr.Zero);

            EagerTensorHandle = c_api.TFE_NewTensorHandle(_handle, tf.status.Handle);
        }
Esempio n. 3
0
        public void Exp_Sliced()
        {
            var a = np.arange(1.0, 81.0);

            a = a.reshape(5, 4, 4);
            var b   = a[":, :, 0"];
            var ret = np.exp(b);

            for (int i = 0; i < ret.size; i++)
            {
                Console.WriteLine(ret.GetAtIndex(i));
            }
            var actual   = ret.GetData <double>();
            var expected = ret54.GetData <double>();

            for (int i = 0; i < actual.Count; i++)
            {
                (System.Math.Abs((actual[i] / expected[i]) - 1.0) < 0.0000001).Should().BeTrue();
            }
        }
Esempio n. 4
0
        public String Predict(NDArray nd)
        {
            nd.CopyTo(_data);
            NDArray.WaitAll();
            _exe.Forward(false);
            List <NDArray> outputs = _exe.Outputs;
            NDArray        outCpu  = outputs[0].Clone(Context.Cpu());

            NDArray.WaitAll();
            float *p    = outCpu.GetData();
            int    iMax = 0;
            float  max  = p[0];

            for (int i = 0; i < 10; i++)
            {
                if (p[i] > max)
                {
                    max  = p[i];
                    iMax = i;
                }
            }
            return(iMax.ToString());
        }