GithubHelp home page GithubHelp logo

scisharp / numsharp Goto Github PK

View Code? Open in Web Editor NEW
1.3K 1.3K 188.0 33.14 MB

High Performance Computation for N-D Tensors in .NET, similar API to NumPy.

Home Page: https://github.com/SciSharp

License: Apache License 2.0

C# 99.99% Python 0.01%
machine-learning numpy numsharp pandas

numsharp's Introduction

SciSharp STACK Web Portal

This repo contains the source code for http://scisharpstack.org

scisharp

Comprehensive list of all Projects

Stable or Beta

  • TensorFlow.NET .NET Standard bindings for TensorFlow
  • NumSharp Pure C# implementation of NumPy
  • Keras.NET Keras.NET is a high-level neural networks API for C#/F# with Python Binding and capable of running on top of TensorFlow, CNTK, or Theano.
  • Numpy.NET C#/F# bindings for NumPy - a fundamental library for scientific computing, machine learning and AI
  • ICSharpCore Jupyter kernel in C# .NET Core which is the standard interface for SciSharp STACK.
  • SharpCV A image library combines OpenCV and NumSharp together. SharpCV returns Mat object with NDArray supported.

Related technologies

  • .NET Interactive Notebooks Jupyter notebooks for C# and F#. Share code, explore data, write, and learn across your apps in ways you couldn't before.
  • Plotly.NET A .NET interface for plotly.js written in F#
  • Dash.NET A .NET interface to Dash- the most downloaded framework for building ML & data science web apps
  • FsLab An F# Community incubation space for data science

Referenced by:

gitMemory

numsharp's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

numsharp's Issues

How to internal storage the array

This issue come up because of discussion in other issue.

We should clear how to internal storage the NDArray data.
Until now we discussed 2 strategies. And I want to use this issue to discuss pro and cons.

Option 1 ) NDArray as an adapter
This means we have a generic NDArray class< T > where T can be any array or jagged array. The storage is 100% the same like the generic T. So accessing an object arr[idx][jdx] would lead to the same like arr.Data[idx][jdx] and the methods would just extend existing .NET classes.

Option 2 ) NDArray as a total new Array type
This means we have for all types e.g. matrix, tensor etc. always just one 1D array. The NDArray must offer methods for indexing and so on by itself and every type is always a NDArray< T >.

open a Wiki

just want to ask if it is possible to open a wiki for numpy.
So we could start give examples for using in F#,C#.Powershell :D

Question : can a package be published to ~/.nuget on local machine?

Very stupid question.

Is it possible in msbuild to pack NumSharp after build automaticly and move to ~/.nuget?

I come up with this question because if there are other projects like pandas.net or I can image SciSharp and they are in different repos, we can not reference them in csproj. Because everybody of us has a different folder structure.

To avoid bad style or complains I think we have now 2 possible options.

No 1) one repo with one solution and all projects so we can reference them in csproj. And we all have same fder structure

No 2) we reference them as nuget package. Usual nuget search first local so if msbuild copy NumSharp nuget package automatically to local cache we are fine. :)

Maybe we have other options?

Project structure with "src" & "test" folder

Sorry again to disturb ^^'

since C# is compiler and not interpreter project - shouldn't we make the structure :

  • "/src/NumSharp" instead of NumSharp
  • "/test/NumSharp.Test" instead of NumSharp.UnitTest

and for future (maybe one day ^^ ) have even "/src/SciSharp" and "/src/PandasSharp" ;)

Which interfaces a NArray shall implement?

Hello NumSharp fans,

Since all great collections of numeric values implement some interfaces, I was thinking which we shall target?

It is not a critical question but it come to my mind and want to ask the others of you

  • IEnumerable< T > --> would be fine since we store all numerics in one 1D array (so should be not to hard to implement)
  • IList< T > --> because indexing but as far as I was reading IList< T > forces you to implement indexing with one int (which bring me to the question "ok what about matrix or Tensor? if we use a single int shall it be splitted / sliced? from tensor to matrix or matrix to vector?" if we think it could be beneficial why not? --> but here we should first implement the slicing which should using the Span< T > stuff.
  • ICollection< T > --> ok here we have the problem that a collection can add elements. sounds easy but .... here my question "do we allow heterogeneous tensors? which is actual possible with jagged arrays but never with multidimensional? Something like ok 3 rows but 1st row has 2 columns, 2nd has 3 columns and 3rd das 4?"

Thanks for your though and I again say it "it is not functional important but i just want to start an issue t collect thoughts about this topic". ;)

Looking Good!

Are you planning on porting all functionality or just a subset?

Generic ARange?

Is there a reason why the ARange isn't generic?

public static NDArray<T> ARange<T>(this NDArray<T> np, int stop, int start = 0, int step = 1) where T : IConvertible
{
    int index = 0;

    np.Data = Enumerable.Range(start, stop - start)
                        .Where(x => index++ % step == 0)
                        .Select(x => (T)Convert.ChangeType(x, typeof(T)))
                        .ToArray();
    return np;
}

Does mean Test fail on your local system?

  • for my local the first (simplest) test fail
  • debugging show all fine
  • but when running without debugging fail because the mean is inf?
  • have no idea why
  • please some people can check your local system and let me know result ? :D

Bitmaps?

I'm interested in loading a bitmap in to a NumSharp array. I realize that isn't written yet but I'm concerned that if I write and contribute that method it will be way too slow to do anything with. What are your thoughts on speed?

Thanks,
Darren


EDIT:
System.Drawing.Bitmap are now supported by a separate package, read more.

Check matrix multiplication again with index check

I think I was wrong about the indexing with matrix multiplication. Will check this tomorrow.

Seems when mapping 1 dimension to 2 dimension the columns are 1st dimension. I did the multiplication with lines first. I need to check this again.

Until now just checked the computed values.

Which APIs and Modules could be implemented?

Hallo,

first of all : awesome work! :D
I just was thinking APIs could be used for numpy (so which classes and methods)?
And I was asking what modules should be also implemented?

  • numpy
  • scipy
  • pandas
  • matplotlib

Is there a way to generate a List with APIs on every module?
So could check which APIs exist in python and which should be implemented.

Moreover I think this libs could be used very well in F# and Powershell.
At end : great work for starting ;)

Refactor shape property in NDArray

Since the dimension offset is depended on shapes, they should be binding together. We want to encapsulate these two properties into one class named Shape. Only way to modify shape property is to new it, after that, it becomes read only. That prevents some operations that change shape without modifying dimension offsets.

Better Encapsulating for high level API

We're going to add a new entry class named NumSharp. NumSharp will have our API more stable for high level usage. Just change NDArray implementation, better encapsulation of OOP, and more readable function names, trying to be as same as possible for NumPy.

var np = new NumSharp();
np.arrange(10).reshape(2, 5);

Reshape with -1

I'm sure this can be optimized. I couldn't really find how numpy actually does this so I guessed and rewrote the reshape routines. It currently only handles two dimensional reshape.

public static NDArray<T> ReShape<T>(this NDArray<T> np, params int[] shape)
{
    var count = shape.Length;
    var idx = FindNegativeIndex(shape);
    if (idx == -1)
        np.Shape = shape;
    else
        np.Shape = CalculateNegativeShape(idx, np.Shape, shape);

    //np.Shape = newShape;

    return np;
}

private static int FindNegativeIndex(params int[] shape)
{
    var count = shape.Length;
    var negOne = false;
    var indexOfNegOne = -1;
    for (int i = 0; i < count; i++)
    {
        if (shape[i] == -1)
        {
            if (negOne)
                throw new ArgumentException("Only allowed to pass one shape dimension as -1");

            negOne = true;
            indexOfNegOne = i;
        }
    }

    return indexOfNegOne;
}

private static IList<int> CalculateNegativeShape(int negativeIndex, IList<int> currentShape, params int[] shapeParams)
{
    var currentShapeCount = currentShape.Count;
    var shapeParamCount = shapeParams.Length;
    var newShape = new List<int>();
    var curShapeVolume = currentShape.Aggregate((x, y) => x * y);
    if (negativeIndex > -1)
    {
        int x = shapeParams[0];
        int y = 0;
        if (shapeParamCount >= 1)
            y = shapeParams[1];
        if (shapeParamCount > 2)
            throw new ArgumentException("We cannot currently handle reshapes of more than 2 dimensions");

        if (negativeIndex == 0 && shapeParamCount == 2)
        {
            var mod = curShapeVolume % y == 0;
            if (!mod)
                throw new ArgumentException($"Wrong Reshape. {curShapeVolume} is not evenly divisible by {y}");
            else
            {
                var a = curShapeVolume / y;
                var b = y;
                newShape.Add(a);
                newShape.Add(b);
            }
        }
        else if (negativeIndex == 1 && shapeParamCount == 2)
        {
            var mod = curShapeVolume % x == 0;
            if (!mod)
                throw new ArgumentException($"Wrong Reshape. {curShapeVolume} is not evenly divisible by {x}");
            else
            {
                var a = x;
                var b = curShapeVolume / x;
                newShape.Add(a);
                newShape.Add(b);
            }
        }
    }
    else
        return currentShape;

    return newShape;
}

And the tests.

[TestMethod]
public void ReshapeNegative()
{
    var np = new NDArray<int>();
    np.ARange(12);
    np.ReShape(-1, 2);
    Assert.IsTrue(np.Shape[0] == 6);
    Assert.IsTrue(np.Shape[1] == 2);

    np.ARange(12);
    np.ReShape(2, -1);
    Assert.IsTrue(np.Shape[0] == 2);
    Assert.IsTrue(np.Shape[1] == 6);

    np.ARange(12);
    np.ReShape(1, 3, 4);
    np.ReShape(-1, 3);
    Assert.IsTrue(np.Shape[0] == 4);
    Assert.IsTrue(np.Shape[1] == 3);

    np.ARange(12);
    np.ReShape(1, 3, 4);
    np.ReShape(3, -1);
    Assert.IsTrue(np.Shape[0] == 3);
    Assert.IsTrue(np.Shape[1] == 4);

    np.ARange(100 * 100 * 3);
    np.ReShape(100, 100, 3);
    np.ReShape(-1, 3);
    Assert.IsTrue(np.Shape[0] == 10000);
    Assert.IsTrue(np.Shape[1] == 3);

    np.ARange(15801033);
    np.ReShape(2531, 2081, 3);
    np.ReShape(-1, 3);
    Assert.IsTrue(np.Shape[0] == 5267011);
    Assert.IsTrue(np.Shape[1] == 3);
}

Rewrite operations with switch pattern matching

I just found out that the modern c# language 7.x supports pattern matching in combination with switch.

Since numerical types like double complex, quaternion,... Do not have a common interface or numerical parent class and most numerical operations require operations like +, -, /, * it is necessary to have a switch case inside which check the generic type of ndarray.

At moment I wrote it quite ugly and lot of casting. Just want to rewrite it like this pattern matching

https://docs.microsoft.com/de-de/dotnet/csharp/pattern-matching

Is there a reason to implement methods as extended methods?

Hello again,

just a question - no complain. :)
Is there a reason to implement methods as extension methods for NumSharp?

My consideration is just the following :

  • extensions methods is sth. C# connected
  • extensions methods are hard to use in other languages (e.g. powershell, F#. python.NET, ...)
  • NumSharp could be used by many other languages if they are normal methods ;)

thanks again for your time. ;)

Target Multiple Frameworks

Are there any objections to changing the csproj from this:

<TargetFramework>netstandard2.0</TargetFramework>

To this?

<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>

This just allows one to test with the normal dotnet framework as well as dotnet standard.

Implement a method which computes .NET tensor from NDArray

I was thinking that it could be very beneficial to create a method "ToDotNetArray" which convert the 1D NDArray to a jagged array in .NET World.

  • Name --> ToDotNetArray
  • Input --> nothing
  • output --> dynamic

The reason for this is "better integration of NumSharp for other .NET packages". So no Numpy based reason for implementation.

partial class NDArray<T>

@dotChris90 Don't know why we need this. Could you give me the use case?

public partial class NDArray<T>
{
    public NDArray<T> reshape(params int[] shape)
    {
        return NumSharp.Extensions.NDArrayExtensions.reshape(this, shape);
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.