这种标准化的更快的数字方法是什么:
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)
可以
np.digitize
让它更快吗?
我有类似的问题,但这与NumPy无关。
我也期待它支持批量向量的
axis
参数。
def normalize_vector2(x, b):
"""
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.
"""
# Step 1: Find the maximum absolute value in x
m = np.max(np.abs(x))
y = np.piecewise(x, [x > 0, x < 0],
[lambda xi: ((2**b - 1) * xi / m), lambda xi: (-2**b * xi / m)])
return y.astype(int)
比较:
import numpy as np
import time
def normalize_vector2(x, b):
"""
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.
"""
# Step 1: Find the maximum absolute value in x
m = np.max(np.abs(x))
y = np.piecewise(x, [x > 0, x < 0],
[lambda xi: ((2**b - 1) * xi / m), lambda xi: (-2**b * xi / m)])
return y.astype(int)
def normalize_vector(x, b):
"""
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)
for elements in [100,1000,10000]:
iterations = int(100000 / elements)
x = np.random.random(elements) * 256-128
t1 = time.time()
for i in range(iterations):
normalize_vector(x,7)
t2 = time.time()
for i in range(iterations):
normalize_vector2(x, 7)
t3 = time.time()
print(f"{(t2-t1)/iterations:.7f}, {elements} elements python: ")
print(f"{(t3-t2)/iterations:.7f}, {elements} elements numpy")
0.0000596, 100 elements python:
0.0000300, 100 elements numpy
0.0004920, 1000 elements python:
0.0000443, 1000 elements numpy
0.0045721, 10000 elements python:
0.0003924, 10000 elements numpy