Python common commands in deep learning

Source: Internet
Author: User

1. Print Dafa

Test = Hello worldprint ("Test:" + test)

2. The difference between math and numpy: math is only for a single element, NumPy broadcasting.

Import Mathimport numpy as npx = [1, 2, 3]s = 1/(1+math.exp (-X)  #这条语句会报错s = 1/(1+np.exp (x)) #这条语句没问题.

3. Defining functions

def sigmoid_derivative (x):    s = 1/(1+np.exp (-X)    ds = s* (1-s)    return DSX = Np.array ([1, 2, 3]) print ("Sigmoid_d Erivative (x) = "+ str (sigmoid_derivative (x))

4. Shape and reshape

image = Np.array ([[[0.67826139, 0.29380381], [0.90714982, 0.52835647], [0.4215251, 0.45017551]], [[0.92814219, 0.96677647], [0.85304703, 0.52351845], [0.199813 97, 0.27417313]], [[0.60659855, 0.00533165], [0.10820313, 0.49978937], [0.34144279, 0.94630077] ], [[0.85304703, 0.52835647], [0.10820313, 0.45017551], [0.34144279, 0.9071498 2]]) print ("image[3][2][1]:" + str (image[3][2][1)) # Image[2][3][1]: 0.90714982print ("image.shape =" + str (image.shap  e) # Image.shape = (4, 3, 2) vector = Image.reshape (image.shape[0]*image.shape[1]*image.shape[2], 1) print ("Vector.shape = "+ str (vector.shape)) # Vector.shape = (1) 

5. Normalize:x_norm = Np.linalg.norm (x, ord = 2, Axis = 1, keepdims = True), where ord=2 is the default value can not write, Axis=1 is the horizontal amount normalization, for one dimensional vector, axis can only For 0,keepdims=true is to keep the shape of the array to prevent the appearance (2,) of this shape, just in case as far as possible write Keepdims=true.

x = Np.array ([[0,3,4],[2,6,4]]) X_norm = Np.linalg.norm (x, ord=2, Axis =1, keepdims=true) x_new = X/x_normprint ("x:" + str (x)) Print ("X_norm:" +str (X_norm)) print ("X_new:" +str (x_new)) output: x: [[[0] 3 4] [2 6 4]]x_norm: [[5        ] [7.48331477]]x_new: [[0.          0.6         0.8       ] [0.26726124  0.80178373  0.53452248]]

6. Summation: X_sum = np.sum (x, Axis = 1, keepdims = True), where Axis=1 is the sum of the horizontal quantities.

x = Np.array ([[0,3,4],[2,6,4]]) X_sum = np.sum (x, Axis = 1, keepdims = True) print ("X_sum:" +str (x_sum)) output: x_sum: [[7] [12 ]]

7. Different multiplication:

Np.dot (x1, x2) is normal matrix multiplication for matrices, and for one-dimensional vectors it is the corresponding element multiplied and summed;

Np.multiply (x1, x2) is a one-dimensional vector that is multiplied by the corresponding elements of a single dimension.

Here time is the method of timing.

Import TIMEX1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]### vector point multiplication, corresponding Element multiplication and Summation # # #tic = time.process_time () dot = Np.dot (x1,x2) TOC = Time.process_time () print ("dot =" + str (dot) + "\ n-----Com Putation time = "+ str (1000* (toc-tic)) +" MS ") # # # X1 and x2 transpose do matrix multiplication, n*1 matrix multiplied by 1*n Matrix # # #tic = time.process_time () outer = NP.O Uter (x1,x2) TOC = Time.process_time () print ("outer =" + str (outer) + "\ n-----Computation time =" + str (1000* (toc-tic) + "MS") # # # corresponding element multiplied to get 1*n vector # # #tic = Time.process_time () Mul = np.multiply (x1,x2) TOC = Time.process_time () print ("Elementw Ise multiplication = "+ str (mul) +" \ n-----Computation time = "+ str (1000* (toc-tic)) +" MS ") # # # normal Matrix multiplication # # #W = Np.ra Ndom.rand (3,len (x1)) # Random 3*len (x1) NumPy arraytic = time.process_time () dot = Np.dot (w,x1) TOC = Time.process_time () pr int ("GDOT =" + str (dot) + "\ n-----Computation time =" + str (1000* (toc-tic)) + "MS") output: dot = 278-----computation T IME = 0.0msouter = [[81 18] 18 81 0 81 18 45 0 0 81 18 45 0 0 "[18 4 4 18 0 18 4 10 0 0 18 4 10 0 0] [45 10 10 45 0 45 10 25 0 0 45  10 25 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [63 14 14 63 0  63 14 35 0 0 63 14 35 0 0] [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [81 18 18 81 0 81 18 45 0 0 81 18 45 0 0] [18 4 4 18 0 18 4 10 0 0 18 4 10 0 0] [45 10 10 45 0 45 10 25 0 0 45 10 25 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]] -----computation time = 0.0mselementwise multiplication = [Bayi 4 0 0 0 0 0 Bayi 4, 0 0]-----computatio N time = 0.0msgdot = [14.98632469 18.30746169 17.30396991]-----computation time = 0.0ms

8. Broadcasting:loss = Np.sum ((yhat-y) **2, keepdims = True), the operation of this * *, also calculates the square for each element.

  

  

  

  

  

Python common commands in deep learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.