日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python的基础命令_深度学习中python常用命令

發布時間:2025/3/15 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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) + "\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的基础命令_深度学习中python常用命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。