Skip to content

How to contribute in development

César Souza edited this page Nov 30, 2017 · 8 revisions

Welcome to the keras-sharp wiki!

This is the first page in the wiki while the project is being developed. Hopefully it should instruct you how to contribute in the development of Keras Sharp.

Configuring the development environment

1) Install Visual Studio 2017

You can install any version of Visual Studio 2017, such as the free community edition.

2) Clone the project

You can clone the project using either the command line:

git clone https://github.com/cesarsouza/keras-sharp.git

or any visual client of your choice.

3) Open the project

To open the project in visual studio, double-click the Keras Sharp.sln file to open the project in Visual Studio

4) Build the project

In Visual Studio, click on the Build -> Rebuild solution menu item to compile the project for the first time. All required NuGet packages (Accord.NET, CNTK and TensorFlow) will be installed automatically.

5) Voilà

This is all that is required to start developing Keras#.

Contributing backend implementations

The workflow for a contribution is the following. First, please open the following link:

As you can see, this is a link to the original Keras project. Now, please select a function of your liking, such as for example, zeros:

As you might be able to see, the function should look like this:

def zeros(shape, dtype=None, name=None):
    """Instantiates an all-zeros variable and returns it.
    # Arguments
        shape: Tuple of integers, shape of returned Keras variable
        dtype: String, data type of returned Keras variable
        name: String, name of returned Keras variable
    # Returns
        A variable (including Keras metadata), filled with `0.0`.
    # Example
    ```python
        >>> from keras import backend as K
        >>> kvar = K.zeros((3,4))
        >>> K.eval(kvar)
        array([[ 0.,  0.,  0.,  0.],
               [ 0.,  0.,  0.,  0.],
               [ 0.,  0.,  0.,  0.]], dtype=float32)
    ```
    """
    if dtype is None:
        dtype = floatx()
    shape = tuple(map(int, shape))
    tf_dtype = _convert_string_dtype(dtype)
    return variable(tf.constant_initializer(0., dtype=tf_dtype)(shape),
                    dtype, name)

Now, notice there is an example demonstrating how this function can be used from Python. Now, to contribute an implementation for this function in Keras Sharp, please open up the Keras Sharp project in Visual Studio:

Now, open the files TensorFlowBackend.cs in the Keras Sharp project, and TensorFlowBackendTest.cs in the Unit Tests project. Then, proceed to convert the Python usage example above into C# and add it as a unit test in the TensorFlowBackendTest.cs file:

[Test]
public void zeros()
{
    using (var K = new TensorFlowBackend())
    {
        #region doc_zeros
        var kvar = K.zeros(new int[] { 3, 4 });
        var a = K.eval(kvar); // new[,] {{ 0.,  0.,  0.,  0.},
                              //         { 0.,  0.,  0.,  0.},
                              //         { 0.,  0.,  0.,  0.}}
        #endregion

        float[,] actual = (float[,])a;
        Assert.AreEqual(3, actual.Rows());
        Assert.AreEqual(4, actual.Columns());

        double[,] expected = new[,] {{ 0.0,  0.0,  0.0,  0.0},
                                        { 0.0,  0.0,  0.0,  0.0},
                                        { 0.0,  0.0,  0.0,  0.0}};

        Assert.AreEqual(expected, actual);
    }
}

Which should end up looking like this:

Now, try to execute your unit test by pressing Ctrl+R, Ctrl+T: you will notice that the compilation will fail because the zeros method (or the method you are currently trying to implement) is not currently present in Keras Sharp, receiving a message similar to:

Error	CS1061	'TensorFlowBackend' does not contain a definition for 'zeros' and no extension method 'zeros' accepting a first argument of type 'TensorFlowBackend' could be found (are you missing a using directive or an assembly reference?)

Now, let's solve this error by going to the TensorFlowBackend.cs file in the main Keras Sharp project, and adding the following code based on the existing TensorFlow implementation already present in the Keras codebase:

/// <summary>
///   Instantiates an all-zeros variable and returns it.
/// </summary>
/// <param name="shape">Tuple of integers, shape of returned Keras variable.</param>
/// <param name="dtype">Data type of returned Keras variable.</param>
/// <param name="name">String, name of returned Keras variable.</param>
/// <returns>A variable(including Keras metadata), filled with <c>0.0</c>.</returns>
public Tensor zeros(int[] shape, TFDataType dtype = Utils.DEFAULT_DTYPE, string name = null)
{
    // The following is not necessary since C# is strongly typed:
    // if dtype is None:
    //     dtype = floatx()
    // shape = tuple(map(int, shape))
    // tf_dtype = _convert_string_dtype(dtype)

    // However, we might have to perform other conversions of our own:
    Type type = Utils.GetSystemType(dtype);
    Array zeros = Array.CreateInstance(type, shape);

    return this.variable(array: zeros, name: name);
}

Which should then look like this:

Now, let's go back to our unit test, click on its body and hit Ctrl+R, Ctrl+T again: the test should now pass, and you have just finished implementing your first TensorFlow method into C#!

In order to make your new contribution available to others, please take your time to submit your new code as a pull request into the framework issue tracker!