码迷,mamicode.com
首页 > 编程语言 > 详细

深度学习中python常用命令

时间:2017-10-27 01:42:38      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:tin   oid   ica   乘法   ast   turn   res   矩阵   计算   

1. print大法

test = Hello World
print ("test:" + test)

2. math和numpy的区别:math只对单个元素,numpy会broadcasting。  

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

3. 定义函数

def sigmoid_derivative(x):
    s = 1/(1+np.exp(-x)
    ds = s*(1-s)
    return ds

x = np.array([1, 2, 3])
print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x))

4. Shape和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.19981397,  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.90714982]]])

print ("image[3][2][1]: " + str(image[3][2][1])) # image[2][3][1]: 0.90714982

print ("image.shape = " + str(image.shape))  # 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 = (24, 1)

5. Normalize: x_norm = np.linalg.norm(x, ord = 2, axis = 1, keepdims = True),其中ord=2是默认值可以不写,axis=1是对横向量归一化,对于一维向量,axis只可以为0,keepdims=True是保持array的shape,防止出现(2, )这种shape,以防万一尽量都写上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_norm
print ("x: " + str(x))
print ("x_norm: "+str(x_norm))
print ("x_new: "+str(x_new))

输出:
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. 求和:x_sum = np.sum(x, axis = 1, keepdims = True),其中axis=1是对横向量求和。

x = np.array([[0,3,4],[2,6,4]])
x_sum = np.sum(x, axis = 1, keepdims = True)
print ("x_sum: "+str(x_sum))

输出:
x_sum: [[ 7]
 [12]]

7. 不同的乘法:

np.dot(x1, x2)对于矩阵是正常的矩阵乘法,对于一维向量是对应元素相乘再求和;

np.multiply(x1, x2)是一维向量对应元素相乘,得到一个一维向量。

这里time是计时的方法。  

import time

x1 = [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]

### 向量点乘,对应元素相乘再求和 ###
tic = time.process_time()
dot = np.dot(x1,x2)
toc = time.process_time()
print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### x1和x2的转置做矩阵乘法,n*1的矩阵乘以1*n的矩阵 ###
tic = time.process_time()
outer = np.outer(x1,x2)
toc = time.process_time()
print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### 对应元素相乘得到1*n的向量 ###
tic = time.process_time()
mul = np.multiply(x1,x2)
toc = time.process_time()
print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### 正常的矩阵乘法 ###
W = np.random.rand(3,len(x1)) # Random 3*len(x1) numpy array
tic = time.process_time()
dot = np.dot(W,x1)
toc = time.process_time()
print ("gdot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

输出:
dot = 278
 ----- Computation time = 0.0ms
outer = [[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.0ms
elementwise multiplication = [81  4 10  0  0 63 10  0  0  0 81  4 25  0  0]
 ----- Computation time = 0.0ms
gdot = [ 14.98632469  18.30746169  17.30396991]
 ----- Computation time = 0.0ms

8. Broadcasting:loss = np.sum((yhat - y)**2, keepdims = True),这种**的运算,也是对每个元素计算平方。  

  

  

  

  

  

深度学习中python常用命令

标签:tin   oid   ica   乘法   ast   turn   res   矩阵   计算   

原文地址:http://www.cnblogs.com/zonghaochen/p/7739899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!