基本结构
import tensorflow as tf
a = tf.placeholder("float",[None,2])
b = tf.placeholder(tf.float32, [2,1 ])
y = tf.matmul(a, b)
sess = tf.Session()
print (sess.run(y, feed_dict={a: [[2,4],[4,1]],b:[[1],[2]]}))
sess.close()
sess1 = tf.InteractiveSession()
print("\n",sess1.run(y, feed_dict={a: [[1,2]],b:[[1],[2.]]}))
print("\n",y.eval(feed_dict={a: [[2,4],[4,1]],b:[[1],[2]]}))
sess1.close()
'''
output:
[[ 10.]
[ 6.]]
[[ 5.]]
[[ 10.]
[ 6.]]
'''
.placeholder()和feed_dict()
tf.placeholder(dtype,shape=None,name=None)
tf.Session.run(fetches, feed_dict=None, options=None, run=metadate=None)
'''
dtype是必须指定的数据格式,shape和name可以为空;
当计算时,需要feed给它这个占位符的值;feed_dict就是一个赋值字典;
'''
a=tf.placeholder('float')
b=tf.placeholder('float')
c=tf.multiply(a,b)
with tf.Session() as sess:
print(sess.run(c,feed_dict={a:2,b:3}))
'''
可以给非占位符的张量赋值,只要它是feedable的;
'''
tf.Graph.is_feedable(tensor)
保存模型与读取模型.train.Saver()
saver = tf.train.Saver()
saver.save(sess,"Model/model.ckpt")
saver = tf.train.Saver()
saver.restore(sess, "./Model/model.ckpt")
saver = tf.train.import_meta_graph("Model/model.ckpt.meta")
saver.restore(sess, "./Model/model.ckpt")
saver = tf.train.Saver({"v1": u1, "v2": u2})
saver.restore(sess, "./Model/model.ckpt")
ema = tf.train.ExponentialMovingAverage(0.99)
print(ema.variables_to_restore())