Код: Выделить всё
def normalize_vector(x, b, axis):
"""
Normalize real vector x and outputs an integer vector y.
Parameters:
x (numpy.ndarray): Input real vector. (batch_size, seq_len)
b (int): Unsigned integer defining the scaling factor.
axis (int/None): if None, perform flatenned version, if axis=-1, perform relative normalization across batch.
Returns:
numpy.ndarray: Integer vector y.
"""
# Find the maximum absolute value in x
m = np.max(np.abs(x))
# Process each element in x
y = []
for xi in x:
if xi > 0:
y.append(int((2**b - 1) * xi / m))
elif xi < 0:
y.append(int(2**b * xi / m))
else:
y.append(0)
return np.array(y)
У меня похожий вопрос, но он не о NumPy.Я также ожидаю, что он поддерживает параметр axis для пакетного вектора.
Подробнее здесь: https://stackoverflow.com/questions/792 ... malization