Matrix Computation With NumPy

栏目: IT技术 · 发布时间: 4年前

内容简介:How NumPy Can Simplify Your Code And Increase Its Performance At The Same Time. Broadcasting and SIMD come to rescue!Performance is really an important aspect when we are solving problems using Machine Learning, especially Deep Learning. Using a neural net

How NumPy Can Simplify Your Code And Increase Its Performance At The Same Time. Broadcasting and SIMD come to rescue!

Introduction

Performance is really an important aspect when we are solving problems using Machine Learning, especially Deep Learning. Using a neural network for calculating things can become a complex calculation because it involves matrix and vectors to it.

Suppose we want to calculate a 25 x 25 matrix that is being multiplied by 5. At the first time you are being introduced to programming, You will calculate vectors or matrices that look like this,

for i in range(25):
    for j in range(25):
        x[i][j] *= 5

If we use this method, it will make our computation longer and we will not get the result at the time that we desired. Thankfully, in Python, we have NumPy library to solve this matrix and vector calculation.

By using NumPy library, we can make that 3-lines of code become 1-line of code like this one below,

import numpy as np# Make it as NumPy array first
x = np.array(x)
x = x * 5

And if we compare the time, for this case, the conventional way will take around 0.000224 and the NumPy method is just 0.000076. The NumPy is almost 3 times faster than the conventional one, but it also simplifies your code at the same time!

Just imagine when you want to calculate a bigger matrix than in this example here and imagine how much the time that will you save. This calculation is classified as vectorization where the calculation has occurred on a matrix or vector representation. Therefore, with that computation method, it will save your time for the same result.

How is that happen? What is the magic of NumPy so it can calculate the matrix simpler and faster? This article will introduce you to the concepts of Single Instruction Multiple Data (SIMD) and Broadcasting. Without further, let’s get to it.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

鲜活的数据

鲜活的数据

[美] Nathan Yau / 向怡宁 / 人民邮电出版社 / 2012-10-1 / 69.00元

在生活中,数据几乎无处不在,任我们取用。然而,同样的数据给人的感觉可能会千差万别:或冰冷枯燥,让人望而生畏、百思不解其意;或生动有趣,让人一目了然、豁然开朗。为了达到后一种效果,我们需要采用一种特别的方式来展示数据,来解释、分析和应用它。这就是数据可视化技术。Nath an Yau是这一创新领域的先锋。在本书中,他根据数据可视化的工作流程,先后介绍了如何获取数据,将数据格式化,用可视化工具(如R)......一起来看看 《鲜活的数据》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具