Last active
March 23, 2020 10:24
-
-
Save taka-wang/bd9b22db366333a153a6d1340e8dda4c to your computer and use it in GitHub Desktop.
ai basic1 snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"A = np.array(\n", | |
" [[1,2,3],\n", | |
" [4,5,6],\n", | |
" [7,8,9]]) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A:\n", | |
" [[1 2 3]\n", | |
" [4 5 6]\n", | |
" [7 8 9]]\n", | |
"\n", | |
"A*2:\n", | |
" [[ 2 4 6]\n", | |
" [ 8 10 12]\n", | |
" [14 16 18]]\n", | |
"\n", | |
"A+10:\n", | |
" [[11 12 13]\n", | |
" [14 15 16]\n", | |
" [17 18 19]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"A:\\n\", A)\n", | |
"print(\"\\nA*2:\\n\", A * 2) # multiply by 2\n", | |
"print(\"\\nA+10:\\n\", A + 10) # add 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"B = np.array([[10],\n", | |
" [100],\n", | |
" [1000]]) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"A:\n", | |
" [[1 2 3]\n", | |
" [4 5 6]\n", | |
" [7 8 9]]\n", | |
"\n", | |
"B:\n", | |
" [[ 10]\n", | |
" [ 100]\n", | |
" [1000]]\n", | |
"\n", | |
"A+B:\n", | |
" [[ 11 12 13]\n", | |
" [ 104 105 106]\n", | |
" [1007 1008 1009]]\n", | |
"\n", | |
"A*B:\n", | |
" [[ 10 20 30]\n", | |
" [ 400 500 600]\n", | |
" [7000 8000 9000]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(\"A:\\n\", A)\n", | |
"print(\"\\nB:\\n\", B)\n", | |
"print(\"\\nA+B:\\n\", A+B) \n", | |
"print(\"\\nA*B:\\n\", A*B)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## np.log, np.abs, np.maximum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[0. 0.69314718 1.09861229]\n", | |
" [1.38629436 1.60943791 1.79175947]\n", | |
" [1.94591015 2.07944154 2.19722458]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.log(A))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 1 4 9]\n", | |
" [16 25 36]\n", | |
" [49 64 81]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.square(A))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1. 1.41421356 1.73205081]\n", | |
" [2. 2.23606798 2.44948974]\n", | |
" [2.64575131 2.82842712 3. ]]\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.sqrt(A))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## RELU" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"X = np.array([[1,-2,3,-4],\n", | |
" [-9,4,5,6]])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[1 0 3 0]\n", | |
" [0 4 5 6]]\n" | |
] | |
} | |
], | |
"source": [ | |
"Y = np.maximum(0, X)\n", | |
"print(Y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Reference: [Python的矩陣傳播機制&矩陣運算](https://www.jianshu.com/p/e26f381f82ad)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment