@register_canonicalize
@gof.local_optimizer([Reshape])
def local_useless_dimshuffle_in_reshape(node):
 op = node.op
 if (not isinstance(op, Reshape)):
  return False
 if (not ((node.inputs[0].owner is not None) and isinstance(node.inputs[0].owner.op, DimShuffle))):
  return False
 new_order = node.inputs[0].owner.op.new_order
 input = node.inputs[0].owner.inputs[0]
 broadcastables = node.inputs[0].broadcastable
 new_order_of_nonbroadcast = []
 for (i, bd) in zip(new_order, broadcastables):
  if (not bd):
   new_order_of_nonbroadcast.append(i)
 no_change_in_order = all(((new_order_of_nonbroadcast[i] <= new_order_of_nonbroadcast[(i + 1)]) for i in xrange((len(new_order_of_nonbroadcast) - 1))))
 if no_change_in_order:
  shape = node.inputs[1]
  ret = op.__class__(node.outputs[0].ndim)(input, shape)
  copy_stack_trace(node.outputs[0], ret)
  return [ret]

@ register_canonicalize @ gof . local_optimizer ( [ Reshape ] ) def local_useless_dimshuffle_in_reshape ( node ) : op = node . op if ( not isinstance ( op , Reshape ) ) : return False if ( not ( ( node . inputs [ 0 ] . owner is not None ) and isinstance ( node . inputs [ 0 ] . owner . op , DimShuffle ) ) ) : return False new_order = node . inputs [ 0 ] . owner . op . new_order input = node . inputs [ 0 ] . owner . inputs [ 0 ] broadcastables = node . inputs [ 0 ] . broadcastable new_order_of_nonbroadcast = [ ] for ( i , bd ) in zip ( new_order , broadcastables ) : if ( not bd ) : new_order_of_nonbroadcast . append ( i ) no_change_in_order = all ( ( ( new_order_of_nonbroadcast [ i ] <= new_order_of_nonbroadcast [ ( i + 1 ) ] ) for i in xrange ( ( len ( new_order_of_nonbroadcast ) - 1 ) ) ) ) if no_change_in_order : shape = node . inputs [ 1 ] ret = op . __class__ ( node . outputs [ 0 ] . ndim ) ( input , shape ) copy_stack_trace ( node . outputs [ 0 ] , ret ) return [ ret ]
removes useless dimshuffle operation inside reshape: reshape(vector .