Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the Flatten layer #6

Open
cesarsouza opened this issue Aug 13, 2017 · 0 comments
Open

Implement the Flatten layer #6

cesarsouza opened this issue Aug 13, 2017 · 0 comments

Comments

@cesarsouza
Copy link
Owner

cesarsouza commented Aug 13, 2017

Note: Implementing the Flatten layer does not actually involve implement it from scratch.

What needs to be done is to navigate to https://github.com/fchollet/keras/tree/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras, find where the Flatten layer is implemented (in this case, core.py, line 452), and port its arguably simple code:

class Flatten(Layer):
    """Flattens the input. Does not affect the batch size.
    # Example
    ```python
        model = Sequential()
        model.add(Convolution2D(64, 3, 3,
                                border_mode='same',
                                input_shape=(3, 32, 32)))
        # now: model.output_shape == (None, 64, 32, 32)
        model.add(Flatten())
        # now: model.output_shape == (None, 65536)
    ```
    """

    def __init__(self, **kwargs):
        super(Flatten, self).__init__(**kwargs)
        self.input_spec = InputSpec(min_ndim=3)

    def compute_output_shape(self, input_shape):
        if not all(input_shape[1:]):
            raise ValueError('The shape of the input to "Flatten" '
                             'is not fully defined '
                             '(got ' + str(input_shape[1:]) + '. '
                             'Make sure to pass a complete "input_shape" '
                             'or "batch_input_shape" argument to the first '
                             'layer in your model.')
        return (input_shape[0], np.prod(input_shape[1:]))

    def call(self, inputs):
        return K.batch_flatten(inputs)

into C# using the same K backend already present in Keras Sharp.

cesarsouza added a commit that referenced this issue Aug 13, 2017
Updates GH-6: Implement the Flatten layer
Updates GH-5: Implement the Conv2D layer
Updates GH-4: Make the first example for the Sequential model pass
Updates GH-1: Contributing to Keras-Sharp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant