internal void Register(GpuMatrix matrix) { if (_allocationLayer.Any()) { var layer = _allocationLayer.Peek(); if (layer != null) { layer.Add(matrix); } } }
public IVector FindDistances(IReadOnlyList <IVector> data, DistanceMetric distance) { Debug.Assert(IsValid && data.All(v => v.IsValid)); if (distance == DistanceMetric.Cosine) { var norm = DotProduct(this); var dataNorm = data.Select(d => d.DotProduct(d)).ToList(); var ret = new float[data.Count]; for (var i = 0; i < data.Count; i++) { ret[i] = Convert.ToSingle(1d - DotProduct(data[i]) / Math.Sqrt(norm * dataNorm[i])); } return(new GpuVector(_cuda, data.Count, i => ret[i])); } else if (distance == DistanceMetric.Euclidean) { var ptrArray = data.Cast <GpuVector>().Select(d => d._data.DevicePointer).ToArray(); var ret = _cuda.MultiEuclideanDistance(_data, ptrArray, _size); using (var matrix = new GpuMatrix(_cuda, _size, data.Count, ret)) { using (var temp = matrix.ColumnSums()) return(temp.Sqrt()); } } else if (distance == DistanceMetric.Manhattan) { var ptrArray = data.Cast <GpuVector>().Select(d => d._data.DevicePointer).ToArray(); var ret = _cuda.MultiManhattanDistance(_data, ptrArray, _size); using (var matrix = new GpuMatrix(_cuda, _size, data.Count, ret)) { return(matrix.ColumnSums()); } } else { var distanceFunc = _GetDistanceFunc(distance); var ret = new float[data.Count]; for (var i = 0; i < data.Count; i++) { ret[i] = distanceFunc(data[i]); } return(new GpuVector(_cuda, data.Count, i => ret[i])); } }
public IMatrix SoftmaxActivation() { Debug.Assert(IsValid); var rowOutput = new List <GpuVector>(); for (var i = 0; i < _rows; i++) { using (var row = Row(i)) rowOutput.Add(row.Softmax() as GpuVector); } var ret = _cuda.Allocate(_rows * _columns); for (var i = 0; i < _rows; i++) { using (var row = rowOutput[i]) ret.DeviceVariable.CopyToDevice(row.CudaDeviceVariable, 0, _columns * i * sizeof(float), _columns * sizeof(float)); } using (var temp = new GpuMatrix(_cuda, _columns, _rows, ret)) return(temp.Transpose()); }
public IMatrix SoftmaxActivation() { Debug.Assert(IsValid); var rowOutput = new List <GpuVector>(); for (var i = 0; i < _rows; i++) { using (var row = Row(i)) rowOutput.Add(row.Softmax() as GpuVector); } var ret = new CudaDeviceVariable <float>(_rows * _columns); for (var i = 0; i < _rows; i++) { using (var row = rowOutput[i]) { ret.CopyToDevice(row.CudaDeviceVariable, 0, _columns * i * sizeof(float), _columns * sizeof(float)); //CudaBlasNativeMethods.cublasScopy_v2(_cuda.Blas.CublasHandle, _columns * sizeof(float), row.CudaDeviceVariable.DevicePointer, sizeof(float), ret.DevicePointer, _rows * sizeof(float)); } } //return new GpuMatrix(_cuda, _rows, _columns, ret); using (var temp = new GpuMatrix(_cuda, _columns, _rows, ret)) return(temp.Transpose()); }
public void Add(GpuMatrix matrix) { _matrix.Add(matrix); }