tensorflow入门
Start
https://tensorflow.google.cn/tutorials/
import tensorflow as tf a = 3 # Create a variable. w = tf.Variable([[0.5,1.0]],dtype=tf.float64) x = tf.Variable([[2.0],[1.0]],dtype=tf.float64) print(w) y = tf.matmul(w, x) #variables have to be explicitly initialized before you can run Ops init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) print (y.eval())
Functions
with tf.Session() as sess: print(sess.run(tf.zeros([3, 4], tf.int32))) # ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] tensor = [[1, 2, 3], [4, 5, 6]] print(sess.run(tf.zeros_like(tensor))) # ==> [[0, 0, 0], [0, 0, 0]] print(sess.run(tf.ones([2, 3], tf.int32))) # ==> [[1, 1, 1], [1, 1, 1]] # 'tensor' is [[1, 2, 3], [4, 5, 6]] print(sess.run(tf.ones_like(tensor))) # ==> [[1, 1, 1], [1, 1, 1]] # Constant 1-D Tensor populated with value list. print(sess.run(tf.constant([1, 2, 3, 4, 5, 6, 7]))) # => [1 2 3 4 5 6 7] # Constant 2-D tensor populated with scalar value -1. print(sess.run(tf.constant(-1.0, shape=[2, 3]))) # => [[-1. -1. -1.] # [-1. -1. -1.]] print(sess.run(tf.linspace(10.0, 12.0, 3, name="linspace"))) # => [ 10.0 11.0 12.0] start = 3 limit = 18 delta = 3 print(sess.run(tf.range(start, limit, delta))) # ==> [3, 6, 9, 12, 15]
norm = tf.random_normal([2, 3], mean=-1, stddev=4) # Shuffle the first dimension of a tensor c = tf.constant([[1, 2], [3, 4], [5, 6]]) shuff = tf.random_shuffle(c) # Each time we run these ops, different results are generated sess = tf.Session() print (sess.run(norm)) print (sess.run(shuff))
state = tf.Variable(0) new_value = tf.add(state, tf.constant(1)) update = tf.assign(state, new_value) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(state)) for _ in range(3): sess.run(update) print(sess.run(state))
# 输出
# 0
# 1
# 2
# 3
import numpy as np a = np.zeros((3,3)) ta = tf.convert_to_tensor(a) with tf.Session() as sess: print(sess.run(ta))
输出
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
input1 = tf.placeholder(tf.float32) input2 = tf.placeholder(tf.float32) output = tf.multiply(input1, input2) with tf.Session() as sess: print(sess.run([output], feed_dict={input1:[7.], input2:[2.]}))
Model Save/Restore
注意,如下两段代码,不能再同一文件中执行。
v1 = tf.Variable(tf.random_normal([1,2]), name="v1") v2 = tf.Variable(tf.random_normal([2,3]), name="v2")
因为v1和v2只能初始化一次,第二次执行时,name会被tensorflow自动分配成v1_1,v2_1
Save:
import tensorflow as tf v1 = tf.Variable(tf.random_normal([1,2]), name="v1") v2 = tf.Variable(tf.random_normal([2,3]), name="v2") print ("V1:", v1) print ("V2:", v2) init_op = tf.global_variables_initializer() saver = tf.train.Saver() with tf.Session() as sess: sess.run(init_op) print ("V1:",sess.run(v1)) print ("V2:",sess.run(v2)) saver_path = saver.save(sess, "save/model.ckpt") print ("Model saved in file: ", saver_path)
Restore:
import tensorflow as tf v1 = tf.Variable(tf.random_normal([1,2]), name="v1") v2 = tf.Variable(tf.random_normal([2,3]), name="v2") print ("v1 =", v1) print ("v2 =", v2) saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, "save/model.ckpt") print ("Model restored") print ("V1:", sess.run(v1), ", V2:", sess.run(v2)) print ("V1:", sess.run(v1), ", V2:", sess.run(v2))