Comments (10)
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.
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.
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.
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.
Actually the attention brick from here could use that, normalization is done along the first axis there.
from blocks.
I'll write WithAxesSwapped
and As2D
and make a pull request out of that.
from blocks.
Where would it be most natural to put these two bricks? In blocks.bricks
directly?
from blocks.
I would start a new file bricks/wrappers.py
for that. @bartvm?
from blocks.
SGTM
from blocks.
Effectively closed by #404
from blocks.
Related Issues (20)
- Initialization in Stacked LSTM HOT 1
- EarlyStopping does nothing with default triggers
- Ensemble Learning
- Blocks-examples, float64 test is broken
- Errors in the Adam implementation HOT 10
- VisibleDeprecationWarning now results an error HOT 1
- Sampling in recurrent function causes error HOT 4
- ERROR:blocks.main_loop:Error occured during training. HOT 4
- How to use the model? HOT 2
- BN serialization HOT 3
- Using generated output for further calculation HOT 2
- python setup.py install can't work HOT 1
- Softmax.categorical_cross_entropy documentation is wrong
- WithExtraDims wrapper should pass kwargs to the wrapped method
- theano.gof.fg.InconsistencyError: Trying to reintroduce a removed node
- can't install stable version
- Installation fails with no such option: --allow-external HOT 5
- Feature Request: Add Mish activation to the available non-linearities
- Building wheel for fuel (setup.py) ... error HOT 3
- I want to know how the file named 'main_loop.tar"came about
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blocks.