thanks
your solution is correct. I solved it in this way

def get_new_weights(weights):
	nw=weights.view(-1)
	PINFINITY = float('inf')
	NINFINITY = -PINFINITY
	# if there is -inf --> -1e10, +inf-->max(weights), nan --> 1
	nw[nw != nw] = 1
	nw[nw == PINFINITY] = max(weights)
	nw[nw == NINFINITY] = -1e10
	return nw