def make_step_guided(net, step_size=1.5, end='inception_4c/output', jitter=32, clip=True, objective_fn=objective_guide, **objective_params):
 src = net.blobs['data']
 dst = net.blobs[end]
 (ox, oy) = np.random.randint((- jitter), (jitter + 1), 2)
 src.data[0] = np.roll(np.roll(src.data[0], ox, (-1)), oy, (-2))
 net.forward(end=end)
 objective_fn(dst, **objective_params)
 net.backward(start=end)
 g = src.diff[0]
 src.data[:] += ((step_size / np.abs(g).mean()) * g)
 src.data[0] = np.roll(np.roll(src.data[0], (- ox), (-1)), (- oy), (-2))
 if clip:
  bias = net.transformer.mean['data']
  src.data[:] = np.clip(src.data, (- bias), (255 - bias))

def make_step_guided ( net , step_size = 1.5 , end = 'inception_4c/output' , jitter = 32 , clip = True , objective_fn = objective_guide , ** objective_params ) : src = net . blobs [ 'data' ] dst = net . blobs [ end ] ( ox , oy ) = np . random . randint ( ( - jitter ) , ( jitter + 1 ) , 2 ) src . data [ 0 ] = np . roll ( np . roll ( src . data [ 0 ] , ox , ( - 1 ) ) , oy , ( - 2 ) ) net . forward ( end = end ) objective_fn ( dst , ** objective_params ) net . backward ( start = end ) g = src . diff [ 0 ] src . data [ : ] += ( ( step_size / np . abs ( g ) . mean ( ) ) * g ) src . data [ 0 ] = np . roll ( np . roll ( src . data [ 0 ] , ( - ox ) , ( - 1 ) ) , ( - oy ) , ( - 2 ) ) if clip : bias = net . transformer . mean [ 'data' ] src . data [ : ] = np . clip ( src . data , ( - bias ) , ( 255 - bias ) )
basic gradient ascent step .