<jupyter_start><jupyter_text>1. With python for loop<jupyter_code>%%time
tic = time.time()

for j in range(100):
    z=0
    for i in range(1_000_000):
        z += w[i] * x[i]

toc = time.time()

time_for_loop = (toc - tic) * 10
print(f"For loop: {time_for_loop} ms.")<jupyter_output>For loop: 193.084979057312 ms.
CPU times: user 19.3 s, sys: 7.04 ms, total: 19.3 s
Wall time: 19.3 s<jupyter_text>2. With numpy vectorization<jupyter_code>%%time
tic = time.time()

for j in range(100):
    z=0
    z = np.dot(w, x)

toc = time.time()

time_for_numpy = (toc - tic) * 10
print(f"Numpy vec: {time_for_numpy} ms.")<jupyter_output>Numpy vec: 0.11962652206420898 ms.
CPU times: user 215 ms, sys: 0 ns, total: 215 ms
Wall time: 12.1 ms<jupyter_text>3. With Tensorflow Tensor<jupyter_code>w_tf = tf.convert_to_tensor(w)
x_tf = tf.convert_to_tensor(x)
%%time
tic = time.time()

for j in range(100):
    z=0
    z = tf.tensordot(w_tf, x_tf, axes=1)

toc = time.time()

time_for_tf = (toc - tic) * 10
print(f"tf vec: {time_for_tf} ms.")<jupyter_output>tf vec: 0.44008970260620117 ms.
CPU times: user 41.6 ms, sys: 3.68 ms, total: 45.3 ms
Wall time: 44.1 ms<jupyter_text>With opti<jupyter_code>@tf.function
def dot(x, y):
    return tf.tensordot(x, y, axes=1)
%%time
tic = time.time()

for j in range(100):
    z=0
    z = dot(w_tf, x_tf)

toc = time.time()

time_for_tf_opti = (toc - tic) * 10
print(f"tf vec opti: {time_for_tf_opti} ms.")<jupyter_output>tf vec opti: 0.17971515655517578 ms.
CPU times: user 20.8 ms, sys: 184 µs, total: 21 ms
Wall time: 18.1 ms<jupyter_text>4. With Pytorch<jupyter_code>w_torch = torch.tensor(w)
x_torch = torch.tensor(x)
%%time
tic = time.time()

for j in range(100):
    z=0
    z = torch.dot(w_torch, x_torch)

toc = time.time()

time_for_torch = (toc - tic) * 10
print(f"torch vec: {time_for_torch} ms.")<jupyter_output>torch vec: 0.11466026306152344 ms.
CPU times: user 91.8 ms, sys: 1.12 ms, total: 92.9 ms
Wall time: 11.8 ms<jupyter_text>Results<jupyter_code>times = {
    'For Loop': time_for_loop,
    'Numpy vectorization': time_for_numpy,
    'TF vectorization': time_for_tf,
    'TF opti vectorization': time_for_tf_opti,
    'Torch vectorization': time_for_torch
}
df_time = pd.DataFrame.from_dict(times, orient='index', columns=['Time (ms)'])
df_time.sort_values(by='Time (ms)', inplace=True)
df_time.T
f"Vectorization can be {int(time_for_loop / time_for_torch)} times faster for this case"
fig = plt.figure(figsize=(12, 8))
plt.bar(df_time.index.values, df_time['Time (ms)'].values)
plt.title('Benchmark dot vector', size=20)
plt.show();
fig = plt.figure(figsize=(12, 8))
plt.bar(df_time.index.values[:-1], df_time['Time (ms)'].values[:-1])
plt.title('Benchmark dot vector without for loop', size=20)
plt.show();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15348670021567218, 'fraction_numerical': 0.0715312724658519, 'mean_word_length': 4.622222222222222, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '440973', 'license': 'MIT', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1205, 'n_words': 287, 'path': '01_Neural_Networks_And_Deep_Learning/01_vectorization.ipynb', 'repo_name': 'ThomasFavrel/Deep_learning_spe_Coursera', 'size': '2782'}