“Python TensorFlow Basics”版本间的差异

来自iCenter Wiki
跳转至: 导航搜索
第1行: 第1行:
 +
=Python =
  
 
+
==IPython==
=IPython=
+
  
 
* IPython  
 
* IPython  
第8行: 第8行:
  
 
或者使用:$jupyter notebook
 
或者使用:$jupyter notebook
 
  
 
=Python Math library=
 
=Python Math library=
第32行: 第31行:
  
 
</code>
 
</code>
 +
 +
=激活函数=
 +
==ReLU==
 +
 +
import matplotlib.pyplot as plt
 +
import numpy as np
 +
 +
x = np.linspace(-5,5,100)
 +
 +
relu = lambda x: 1 / (1 + np.exp(x))
 +
 +
plt.plot(x,relu(x), color='red', lw=2)
 +
 +
plt.show()
 +
  
 
== sigmoid==
 
== sigmoid==

2017年10月16日 (一) 02:24的版本

Python

IPython

  • IPython

命令行输入:$ ipython notebook

或者使用:$jupyter notebook

Python Math library

https://docs.python.org/3.6/library/math.html

Numpy

[x] python-numpy, http://cs231n.github.io/python-numpy-tutorial/.

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()

激活函数

ReLU

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,100)
relu = lambda x: 1 / (1 + np.exp(x))
plt.plot(x,relu(x), color='red', lw=2)
plt.show()


sigmoid

http://matplotlib.org/)

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.pyplot 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.nn.sigmoid(R, name="last")))
print(sess.run(tf.nn.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

  1. 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
  1. 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)