Giter VIP home page Giter VIP logo

Comments (10)

rizar avatar rizar commented on August 16, 2024

After giving it another thought I am inclined to just have a wrapper that adapts 2D-only blocks to work with more-D data, like the one @bartvm wrote ages ago. Otherwise we can end up with lots of boilerplate code dealing with dimensions.

from blocks.

vdumoulin avatar vdumoulin commented on August 16, 2024

Just throwing in an idea here, but how about sandwiching the softmax operation in reshapes?

I'm thinking of something like this:

from theano import tensor

def nd_softmax(a, axis=-1):
    swapped_a = a.swapaxes(axis, -1)
    matrix_a = swapped_a.reshape((swapped_a.shape[:-1].prod(), a.shape[-1]))
    softmax_swapped_a = tensor.nnet.softmax(matrix_a).reshape(swapped_a.shape)
    return softmax_swapped_a.swapaxes(axis, -1)

The downside of that approach is that the reshape operations may complicate the graph optimization and prevent some optimizations being applied (I've run into that issue in the past).

from blocks.

bartvm avatar bartvm commented on August 16, 2024

You could make the reshaping conditional though, no?

shape, ndim = a.shape, a.ndim
if ndim > 2:
    a = a.reshape((a.shape[:-1].prod(), a.shape[-1]))
softmax_a = tensor.nnet.softmax(a)
if ndim  > 2:
    softmax_a = softmax_a.reshape(shape)

This seems like code that would be generally useful though, so I personally had something in mind like:

class As2D(Brick):
    def __init__(self, application_method):
        self.application_method = application_method

    def apply(self, x):
        reshaped_x = x.reshape(...)  # As above
        y = self.application_method(reshaped_x)
        reshaped_y = y.reshape(...) 

That way you can do

y = As2D(Softmax().apply).apply(x)

from blocks.

vdumoulin avatar vdumoulin commented on August 16, 2024

I like that idea. Do you think there's a use case for applying softmax on another axis than the last one? If so, something like

class WithAxesSwapped(Brick):
    def __init__(self, application_method, axis_0, axis_1):
        self.application_method = application_method
        self.axis_0 = axis_0
        self.axis_1 = axis_1

    def apply(self, x):
        swapped_x = x.swapaxes(self.axis_0, self.axis_1)
        y = self.application_method(reshaped_x)
        swapped_y = y.swapaxes(self.axis_0, self.axis_1)

would let you do

y = WithAxesSwapped(As2D(Softmax().apply), axis, -1).apply(x)

from blocks.

rizar avatar rizar commented on August 16, 2024

Actually the attention brick from here could use that, normalization is done along the first axis there.

from blocks.

vdumoulin avatar vdumoulin commented on August 16, 2024

I'll write WithAxesSwapped and As2D and make a pull request out of that.

from blocks.

vdumoulin avatar vdumoulin commented on August 16, 2024

Where would it be most natural to put these two bricks? In blocks.bricks directly?

from blocks.

rizar avatar rizar commented on August 16, 2024

I would start a new file bricks/wrappers.py for that. @bartvm?

from blocks.

bartvm avatar bartvm commented on August 16, 2024

SGTM

from blocks.

rizar avatar rizar commented on August 16, 2024

Effectively closed by #404

from blocks.

Related Issues (20)

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.