Python TensorFlow Basics
目录
第三天
讲课:
斯坦福大学教程Lecture 1和Lecture 2
回归模型 regression model(Lecture 1)
线性回归 linear model(Lecture 2)
IPython
- IPython
命令行输入:$ ipython notebook
或者使用:$jupyter notebook
Python Math library
https://docs.python.org/3.6/library/math.html
matplotlib
(matplotlib/pyplot)展示WaveForm AudioPlot
import matplotlib.pyplot as plt from wavReader import readWav
rate, data =readWav('c:\\Users\\icenter\\Documents\\a.wav')
plt.plot(data)
plt.show()
sigmoid
import matplotlib.pyplot as plt import numpy as np
x = np.linspace(-5,5,100)
sigmoid = lambda x: 1 / (1 + np.exp(-x))
plt.plot(x,sigmoid(x), color='red', lw=2)
plt.show()
softmax函数
import math import matplotlib.plot as plt
w=[1,2,3,4,5,6,7,8,9]
w_exp=[math.exp(i) for i in w]
print(w_exp)
sum_w_exp = sum(w_exp)
softmax = [round(i / sum_w_exp, 3) for i in w_exp]
print(softmax)
print(sum(softmax))
plt.plot(softmax)
plt.show()
Tensorflow
TensorFlow:opensource machine intelligence libraries
import tensorflow as tf import numpy
A=tf.Variable([[1,2],[3,4]], dtype=tf.float32) A.get_shape()
TensorShape([Dimension(2), Dimension(2)])
B=tf.Variable([[5,6],[7,8]],dtype=tf.float32) B.get_shape()
TensorShape([Dimension(2), Dimension(2)])
C=tf.matmul(A,B)
tf.global_variables()
[<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32_ref>, <tf.Variable 'Variable_1:0' shape=(2, 2) dtype=float32_ref>]
init = tf.global_variables_initializer()
sess =tf.Session()
sess.run(init)
print(sess.run(C))
[[ 19. 22.] [ 43. 50.]]
print(sess.run(tf.reduce_sum(C, 0)))
[ 62. 72.]
print(sess.run(tf.reduce_sum(C, 1)))
[ 41. 93.]
TensorFlow 的表示
Sigmoid 函数/SOFTMAX 函数/SOFTPLUS函数
(https://www.tensorflow.org/api_guides/python/nn)
R=tf.Variable([1.,.2,3],dtype=tf.float32)
T=tf.Variable([True, True, False, False], dtype=tf.bool)
U=tf.Variable([True, True, True, False], dtype=tf.bool)
init=tf.global_variables_initializer()
sess.run(init) //sess.run(tf.global_variables_initializer())
print(sess.run(tf.sigmoid(R, name="last")))
print(sess.run(tf.softmax(R,dim=-1, name="last")))
print(sess.run(tf.nn.softplus(R,name="last")))
print(sess.run(tf.nn.relu(R,name="XXX")))
print(sess.run(tf.cast(T, tf.int32)))
print(sess.run(tf.cast(T, tf.float32)))
print(sess.run(tf.reduce_mean(tf.cast(T, tf.float32))))
Tensorflow的reshape函数
tf.reshape()
Y=tf.Variable([[1,2,3],[4,5,6]],dtype=tf.float32)
sess.run(tf.global_variables_initializer())
print(sess.run(Y))
[[1 2 3] [4 5 6]]
print(sess.run(tf.reshape(Y,[6])))
[ 1. 2. 3. 4. 5. 6.]
print(sess.run(tf.reshape(Y,[3,2])))
array([[1 2] [3 4] [5 6]], dtype=float32)
print(sess.run(tf.reshape(Y,[-1])))
array([1 2 3 4 5 6],dtype=float32)
print(sess.run(tf.reshape(Y, [-1,1])))
array([[ 1.], [ 2.], [ 3.], [ 4.], [ 5.], [ 6.]], dtype=float32)
tf.argmax
tf.argmax is an extremely useful function which gives you the index of the highest entry in a tensor along some axis.
print(sess.run(tf.argmax(Y,1)))
[2 2]
print(sess.run(tf.argmax(Y,0)))
[1 1 1]
TensorFlow Saver
- Add ops to save and restore all the variables.
saver = tf.train.Saver()
# Save the variables to disk. save_path = saver.save(sess, "/tmp/model.ckpt") print("Model saved in file: %s" % save_path)
saver.restore(sess, "/tmp/model.ckpt") print("Model restored.") # Do some work with the model
- current file path
import os os.path.realpath('.')
线性代数 Linear Algebra
matrix(matrices) and vector(vectors)
addition and scalar multiplication
Matrix and vector multiplication
matrix and matrix multiplication
matrix inverse/transpose
(https://www.tensorflow.org/api_docs/python/tf/matrix_inverse)
(https://www.tensorflow.org/api_guides/python/math_ops)
Octave
展示Sigmoid函数和Softplus函数
x = -5:0.1:5
plot(x, y=1 ./(1+e.^-x)
plot(x, z = log(1+e.^x))