The Wayback Machine - https://web.archive.org/web/20240717024401/https://www.geeksforgeeks.org/why-for-loop-is-not-preferred-in-neural-network-problems/
Open In App

Why For loop is not preferred in Neural Network Problems?

Last Updated : 17 May, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

For loop take much time for completing iterations and in ML practise we have to optimize the time so we can use for loops. But then you must be wondering what to use then? Don’t worry we will discuss this in the below section.

How to get rid from loop in Machine Learning or Neural Network?

The solution is Vectorization. Now the question arises what is vectorization? Vectorization is the process to convert algorithms from operating on a single value at a time to operate on a set of values (vector) at one time. Modern CPUs provide direct support for vector operations where a single instruction is applied to multiple data (SIMD).

Why to use Vectorization over for loop ?

Vectorization is used to speed up the Python code . Using np.function can help in minimizing the running time of code efficiently. Let’s see the below example for better understanding.

Example:




import numpy as np
import time
  
  
a = np.array([1, 2, 3, 4])
  
# there will be 100000 training data
a = np.random.rand(100000)
b = np.random.rand(100000)
  
# FOR VECTORIZTION
# Measure current time
time1 = time.time()
  
# computing
c = np.dot(a, b)
  
# Measure current time
time2 = time.time()
  
# printing time taken for above calculation
print("vectorized form time taken"+ "\t"+str(1000*(time2-time1))+"ms")
   
# FOR for loop
# measure current time
time3 = time.time()
c = 0
  
# computing 
for i in range(100000):
  c+= a[i]*b[i]
  
# measure current time
time4 = time.time()
  
# printing time taken for above calculation
print("for loop time taken "+"\t\t"+str(1000*(time4-time3))+"ms")


Output:

vectorized form time taken    0.43511390686035156ms
for loop time taken         216.04394912719727ms


Similar Reads

Why PyPy3 is preferred over Python3?
"If you want your code to run faster, you should probably just use PyPy." -- Guido van Rossum (creator of Python) If you have ever coded in Python, you know how much slower it is compared to some of the other popular programming languages. In most of the online code judges, the time limit of Python is as much a 5 times more than that of C and more
3 min read
Why do we have to normalize the input for an artificial neural network?
Answer: Normalizing input for an artificial neural network improves convergence and training stability by ensuring consistent scale and reducing the impact of different feature magnitudes.Normalize Input for Artificial Neural Networks: Scale Consistency:Ensures all input features have similar scales.Prevents certain features from dominating the lea
2 min read
Difference Between Feed-Forward Neural Networks and Recurrent Neural Networks
Pre-requisites: Artificial Neural Networks and its Applications Neural networks are artificial systems that were inspired by biological neural networks. These systems learn to perform tasks by being exposed to various datasets and examples without any task-specific rules. In this article, we will see the difference between Feed-Forward Neural Netwo
2 min read
Why neural networks do not perform well on structured data?
Answer: Neural networks may face challenges with structured data due to complexity and sensitivity to feature scaling, demanding more tuning and preprocessing than traditional algorithms designed for such datasets.Neural networks, particularly deep learning models, may face challenges when dealing with structured data for several reasons: Data Repr
2 min read
A single neuron neural network in Python
Neural networks are the core of deep learning, a field that has practical applications in many different areas. Today neural networks are used for image classification, speech recognition, object detection, etc. Now, Let's try to understand the basic unit behind all these states of art techniques.A single neuron transforms given input into some out
3 min read
Applying Convolutional Neural Network on mnist dataset
CNN is basically a model known to be Convolutional Neural Network and in recent times it has gained a lot of popularity because of its usefulness. CNN uses multilayer perceptrons to do computational works. CNN uses relatively little pre-processing compared to other image classification algorithms. This means the network learns through filters that
6 min read
Effect of Bias in Neural Network
Neural Network is conceptually based on actual neuron of brain. Neurons are the basic units of a large neural network. A single neuron passes single forward based on input provided. In Neural network, some inputs are provided to an artificial neuron, and with each input a weight is associated. Weight increases the steepness of activation function.
3 min read
Importance of Convolutional Neural Network | ML
Convolutional Neural Network as the name suggests is a neural network that makes use of convolution operation to classify and predict. Let's analyze the use cases and advantages of a convolutional neural network over a simple deep learning network. Weight sharing: It makes use of Local Spatial coherence that provides same weights to some of the edg
2 min read
Neural Network Advances
We know that our world is changing quickly but there are lot of concrete technology advances that you might not hear a lot about in the newspaper or on tv, that are nevertheless having a dramatic impact on our lives. Some of these big new stories are related to the ANN(Artificial Neural Network) - a relatively new phenomenon in artificial intellige
3 min read
Implementation of Artificial Neural Network for AND Logic Gate with 2-bit Binary Input
Artificial Neural Network (ANN) is a computational model based on the biological neural networks of animal brains. ANN is modeled with three types of layers: an input layer, hidden layers (one or more), and an output layer. Each layer comprises nodes (like biological neurons) are called Artificial Neurons. All nodes are connected with weighted edge
4 min read