def conv3d(signals, filters, signals_shape=None, filters_shape=None, border_mode='valid'):
 if isinstance(border_mode, str):
  border_mode = (border_mode, border_mode, border_mode)
 if (signals_shape is None):
  _signals_shape_5d = signals.shape
 else:
  _signals_shape_5d = signals_shape
 if (filters_shape is None):
  _filters_shape_5d = filters.shape
 else:
  _filters_shape_5d = filters_shape
 (Ns, Ts, C, Hs, Ws) = _signals_shape_5d
 (Nf, Tf, C, Hf, Wf) = _filters_shape_5d
 _signals_shape_4d = ((Ns * Ts), C, Hs, Ws)
 _filters_shape_4d = ((Nf * Tf), C, Hf, Wf)
 if (border_mode[1] != border_mode[2]):
  raise NotImplementedError('height and width bordermodes must match')
 conv2d_signal_shape = _signals_shape_4d
 conv2d_filter_shape = _filters_shape_4d
 if (signals_shape is None):
  conv2d_signal_shape = None
 if (filters_shape is None):
  conv2d_filter_shape = None
 out_4d = tensor.nnet.conv2d(signals.reshape(_signals_shape_4d), filters.reshape(_filters_shape_4d), input_shape=conv2d_signal_shape, filter_shape=conv2d_filter_shape, border_mode=border_mode[1])
 if (border_mode[1] == 'valid'):
  Hout = ((Hs - Hf) + 1)
  Wout = ((Ws - Wf) + 1)
 elif (border_mode[1] == 'full'):
  Hout = ((Hs + Hf) - 1)
  Wout = ((Ws + Wf) - 1)
 elif (border_mode[1] == 'half'):
  Hout = ((Hs - (Hf % 2)) + 1)
  Wout = ((Ws - (Wf % 2)) + 1)
 elif (border_mode[1] == 'same'):
  raise NotImplementedError()
 else:
  raise ValueError('invalid border mode', border_mode[1])
 out_tmp = out_4d.reshape((Ns, Ts, Nf, Tf, Hout, Wout))
 if (Tf == 1):
  out_5d = out_tmp.reshape((Ns, Ts, Nf, Hout, Wout))
 else:
  if (border_mode[0] == 'valid'):
   Tpad = 0
  elif (border_mode[0] == 'full'):
   Tpad = (Tf - 1)
  elif (border_mode[0] == 'half'):
   Tpad = (Tf // 2)
  elif (border_mode[0] == 'same'):
   raise NotImplementedError()
  else:
   raise ValueError('invalid border mode', border_mode[0])
  if (Tpad == 0):
   out_5d = diagonal_subtensor(out_tmp, 1, 3).sum(axis=3)
  else:
   out_tmp_padded = tensor.zeros(dtype=out_tmp.dtype, shape=(Ns, (Ts + (2 * Tpad)), Nf, Tf, Hout, Wout))
   out_tmp_padded = tensor.set_subtensor(out_tmp_padded[:, Tpad:(Ts + Tpad), :, :, :, :], out_tmp)
   out_5d = diagonal_subtensor(out_tmp_padded, 1, 3).sum(axis=3)
 return out_5d

def conv3d ( signals , filters , signals_shape = None , filters_shape = None , border_mode = 'valid' ) : if isinstance ( border_mode , str ) : border_mode = ( border_mode , border_mode , border_mode ) if ( signals_shape is None ) : _signals_shape_5d = signals . shape else : _signals_shape_5d = signals_shape if ( filters_shape is None ) : _filters_shape_5d = filters . shape else : _filters_shape_5d = filters_shape ( Ns , Ts , C , Hs , Ws ) = _signals_shape_5d ( Nf , Tf , C , Hf , Wf ) = _filters_shape_5d _signals_shape_4d = ( ( Ns * Ts ) , C , Hs , Ws ) _filters_shape_4d = ( ( Nf * Tf ) , C , Hf , Wf ) if ( border_mode [ 1 ] != border_mode [ 2 ] ) : raise NotImplementedError ( 'height and width bordermodes must match' ) conv2d_signal_shape = _signals_shape_4d conv2d_filter_shape = _filters_shape_4d if ( signals_shape is None ) : conv2d_signal_shape = None if ( filters_shape is None ) : conv2d_filter_shape = None out_4d = tensor . nnet . conv2d ( signals . reshape ( _signals_shape_4d ) , filters . reshape ( _filters_shape_4d ) , input_shape = conv2d_signal_shape , filter_shape = conv2d_filter_shape , border_mode = border_mode [ 1 ] ) if ( border_mode [ 1 ] == 'valid' ) : Hout = ( ( Hs - Hf ) + 1 ) Wout = ( ( Ws - Wf ) + 1 ) elif ( border_mode [ 1 ] == 'full' ) : Hout = ( ( Hs + Hf ) - 1 ) Wout = ( ( Ws + Wf ) - 1 ) elif ( border_mode [ 1 ] == 'half' ) : Hout = ( ( Hs - ( Hf % 2 ) ) + 1 ) Wout = ( ( Ws - ( Wf % 2 ) ) + 1 ) elif ( border_mode [ 1 ] == 'same' ) : raise NotImplementedError ( ) else : raise ValueError ( 'invalid border mode' , border_mode [ 1 ] ) out_tmp = out_4d . reshape ( ( Ns , Ts , Nf , Tf , Hout , Wout ) ) if ( Tf == 1 ) : out_5d = out_tmp . reshape ( ( Ns , Ts , Nf , Hout , Wout ) ) else : if ( border_mode [ 0 ] == 'valid' ) : Tpad = 0 elif ( border_mode [ 0 ] == 'full' ) : Tpad = ( Tf - 1 ) elif ( border_mode [ 0 ] == 'half' ) : Tpad = ( Tf // 2 ) elif ( border_mode [ 0 ] == 'same' ) : raise NotImplementedError ( ) else : raise ValueError ( 'invalid border mode' , border_mode [ 0 ] ) if ( Tpad == 0 ) : out_5d = diagonal_subtensor ( out_tmp , 1 , 3 ) . sum ( axis = 3 ) else : out_tmp_padded = tensor . zeros ( dtype = out_tmp . dtype , shape = ( Ns , ( Ts + ( 2 * Tpad ) ) , Nf , Tf , Hout , Wout ) ) out_tmp_padded = tensor . set_subtensor ( out_tmp_padded [ : , Tpad : ( Ts + Tpad ) , : , : , : , : ] , out_tmp ) out_5d = diagonal_subtensor ( out_tmp_padded , 1 , 3 ) . sum ( axis = 3 ) return out_5d
this function will build the symbolic graph for convolving a mini-batch of a stack of 3d inputs with a set of 3d filters .