np.mean 比循环和常规计算均值要慢这么多怎么可能?

huangapple go评论104阅读模式
英文:

How is it possible that np.mean takes so much longer than looping and calculating the mean regularly?

问题

我已经构建了一个使用二元分类的神经网络,注意到它运行得非常慢。 代码如下:

  1. predictions = self.neural_network.propagate(X_train)
  2. rounded_predictions = (predictions > 0.5).astype(int)
  3. accuracy = np.mean(rounded_predictions == y_train)

其中,x_train的形状为(16000,16),y_train的形状为(16000,)

在查找性能瓶颈时,我发现使用np.mean计算准确性平均需要0.55秒。我无法弄清楚为什么会花这么长时间,所以我尝试了另一种计算方式:

  1. def compute_accuracy_score(y_train, predictions):
  2. """
  3. 计算网络预测的准确性分数。
  4. 如果预测匹配其真实标签,则增加正确预测的计数。
  5. 准确性分数是正确预测数除以总预测数。
  6. """
  7. num_samples = len(y_train)
  8. correct_predictions = 0
  9. # 如果预测正确,则增加正确预测的计数
  10. for true_label, predicted_label in zip(y_train, predictions):
  11. if true_label == predicted_label:
  12. correct_predictions += 1
  13. # 计算准确性,即正确预测数除以样本总数
  14. accuracy = correct_predictions / num_samples
  15. return accuracy

这段代码在适当的情况下运行时间低于0.01秒。
我不明白为什么与简单循环相比,np.mean需要这么长时间。

这是我编写的用于检查运行时间的代码:

  1. def calculate_fitness(self):
  2. predictions = self.neural_network.propagate(X_train)
  3. rounded_predictions = (predictions > 0.5).astype(int)
  4. start_time = time.time()
  5. accuracy1 = compute_accuracy_score(y_train, rounded_predictions)
  6. end_time = time.time()
  7. print('使用compute accuracy计算适应度所需时间:', end_time - start_time, '秒')
  8. start_time = time.time()
  9. accuracy = np.mean(rounded_predictions == y_train)
  10. end_time = time.time()
  11. print('使用np.mean计算适应度所需时间:', end_time - start_time, '秒')
  12. print('准确性:', accuracy, '准确性1:', accuracy1)
  13. self.fitness = round(float(accuracy), 4)

准确性不同,运行时间也不同:
使用compute accuracy计算适应度所需时间:0.022114276885986328秒
使用np.mean计算适应度所需时间:0.6563560962677002秒
准确性:0.559151625 准确性1:0.6015625
使用compute accuracy计算适应度所需时间:0.021242141723632812秒
使用np.mean计算适应度所需时间:0.6553714275360107秒
准确性:0.441705 准确性1:0.398875

英文:

I've been building a neural network that uses a binary classification, and I noticed its running really slow. The code is:

  1. predictions = self.neural_network.propagate(X_train)
  2. rounded_predictions = (predictions > 0.5).astype(int)
  3. accuracy = np.mean(rounded_predictions == y_train)

where x_train is of shape (16000,16) and y_train is of shape (16000,) <br>
When checking where the bottleneck is, I found that calculating accuracy using np.mean took an average of 0.55 seconds. I couldn't figure out what is taking it so long so i tried calculating it in a different way:

  1. def compute_accuracy_score(y_train, predictions):
  2. &quot;&quot;&quot;
  3. Calculates the accuracy score of the predictions made by the network.
  4. If a prediction matches its true label, it increments the count of correct predictions.
  5. The accuracy score is the correct predictions divided by the total predictions.
  6. &quot;&quot;&quot;
  7. num_samples = len(y_train)
  8. correct_predictions = 0
  9. # If the prediction is correct, increment the count of correct predictions
  10. for true_label, predicted_label in zip(y_train, predictions):
  11. if true_label == predicted_label:
  12. correct_predictions += 1
  13. # Compute accuracy as the ratio of correct predictions to total number of samples
  14. accuracy = correct_predictions / num_samples
  15. return accuracy

This code ran it in an appropriate sub 0.01 seconds.
I don't understand why the np.mean took so much time compared to the simple loop.

This is the code I wrote to check the run time:

  1. def calculate_fitness(self):
  2. predictions = self.neural_network.propagate(X_train)
  3. rounded_predictions = (predictions &gt; 0.5).astype(int)
  4. start_time = time.time()
  5. accuracy1 = compute_accuracy_score(y_train, rounded_predictions)
  6. end_time = time.time()
  7. print(&#39;Time to calculate fitness using compute accuracy:&#39;, end_time - start_time, &#39;seconds&#39;)
  8. start_time = time.time()
  9. accuracy = np.mean(rounded_predictions == y_train)
  10. end_time = time.time()
  11. print(&#39;Time to calculate fitness using np.mean:&#39;, end_time - start_time, &#39;seconds&#39;)
  12. print(&#39;Accuracy:&#39;, accuracy, &#39;Accuracy1:&#39;, accuracy1)
  13. self.fitness = round(float(accuracy), 4)

The accuracy isnt the same, neither is the run time: <br>
Time to calculate fitness using compute accuracy: 0.022114276885986328 seconds <br>
Time to calculate fitness using np.mean: 0.6563560962677002 seconds <br>
Accuracy: 0.559151625 Accuracy1: 0.6015625 <br>
Time to calculate fitness using compute accuracy: 0.021242141723632812 seconds <br>
Time to calculate fitness using np.mean: 0.6553714275360107 seconds <br>
Accuracy: 0.441705 Accuracy1: 0.398875 <br>

答案1

得分: 1

我找到了问题。我使用的是大小为(16000,1)的nparray,它被翻译成了一个2D数组用于np.mean。然而,在使用flatten()将其变为(16000,)后,运行时间更快。

英文:

I found the problem. I was using nparray of size (16000,1) which was translated to a 2D array for np.mean. However after using flatten() to get it to (16000,) it produced faster run times.

答案2

得分: 0

你确定没有包括模型计算预测所需的时间吗?在这里:

  1. predictions = self.neural_network.propagate(X_train)

因为我在我的笔记本电脑上使用更大的维度运行了以下代码段,但仍然需要大约4秒的处理时间。

  1. _s = time.time()
  2. a = np.random.rand(160000000, 1)
  3. b = np.random.rand(160000000, 1)
  4. _round = (b > 0.5).astype(int)
  5. c = np.mean(_round == b)
  6. _e = time.time()
英文:

Are you sure you are not including the model's time to compute the predictions? Here:

  1. predictions = self.neural_network.propagate(X_train)

Because I ran the following snippet on my laptop with larger dimensions and still the process time is around 4 seconds.

  1. _s = time.time()
  2. a = np.random.rand(160000000, 1)
  3. b = np.random.rand(160000000, 1)
  4. _round = (b &gt; 0.5).astype(int)
  5. c = np.mean(_round == b)
  6. _e = time.time()

答案3

得分: 0

你确定在第一种方法的延迟中没有考虑从模型生成预测所需的时间吗?

我尝试复制你的结果,但我看到第一种方法快了100倍。处于矢量化与非矢量化基准的预期范围内。见:

np.mean 比循环和常规计算均值要慢这么多怎么可能?

英文:

Are you sure you are not accounting the time it takes to generate the predictions from your model inside the latency of the first method?

I have tried replicating your results, but I am seeing that the first method is 100x faster. Falling in the expectation range for a vectorized vs non-vectorized benchmark. See:

np.mean 比循环和常规计算均值要慢这么多怎么可能?

huangapple
  • 本文由 发表于 2023年6月15日 19:16:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481909.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定