Код: Выделить всё
def process_vector(x, b, axis):
"""
Processes a 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.
"""
# Step 1: Find the maximum absolute value in x
m = np.max(np.abs(x))
# Step 2-6: 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.>
Подробнее здесь: https://stackoverflow.com/questions/792 ... malization