简体   繁体   中英

How to broadcast across 3d tensor in theano?

If I have a 3d tensor block B and I would like to set some of its "faces" to 0 with probability 0.5. Here axis 1 are rows, axis 2 are columns, and axis 3 are the "faces". I have tried

    size = (B.shape[1], 1, 1)
    noise = self.theano_rng.binomial(size=size, n=1, p=0.5)
    return noise * B

But this isn't working, the shapes aren't lining up and I get an error.
For example, I would like

2 2 2                        2 2 2
3 3 3                        3 3 3
4 4 4                        4 4 4
           *  [1 0]   ->       
6 6 6                        0 0 0
7 7 7                        0 0 0 
8 8 8                        0 0 0 

You can use dimshuffle to add the dimensions necessary to enable broadcasting.

Here's a working example:

import numpy
import theano
import theano.tensor as tt

x = tt.tensor3()
y = tt.bvector()
z = x * y.dimshuffle(0, 'x', 'x')
f = theano.function([x, y], z)
x_value = numpy.array([[[2, 2, 2], [3, 3, 3], [4, 4, 4]],
                       [[6, 6, 6], [7, 7, 7], [8, 8, 8]]], dtype=theano.config.floatX)
y_value = numpy.array([1, 0], dtype=numpy.int8)
print f(x_value, y_value)

which prints

[[[ 2.  2.  2.]
  [ 3.  3.  3.]
  [ 4.  4.  4.]]

 [[ 0.  0.  0.]
  [ 0.  0.  0.]
  [ 0.  0.  0.]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM