python 代码命令大全-深度学习中python常用命令

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) + " ----- 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) + " ----- 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) + " ----- 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) + " ----- 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),这种**的运算,也是对每个元素计算平方。

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值