对Tensor的理解
wiki对Tensor的定义:
import tensorflow as tf
#简单的常数乘法 a = 2 b = 3 mul = a*b
print ("The multiplication produces:::", mul)
The multiplication produces::: 6
# 改写成Tensorflow的常量形式 at = tf.constant(3) bt = tf.constant(4)
mult = tf.mul(at, bt)
print ("The multiplication produces:::", mult)
The multiplication produces::: Tensor("Mul:0", shape=(), dtype=int32)
目前的graph包括at和bt两个Tensor以及mult运算。为了处理mult运算,运算图需要开启一个session:
print ("The actual multiplication result:::", sess.run(mult))
The actual multiplication result::: 12
# Variable
var1 = tf.Variable(2, name="var1") var2
= tf.Variable(3, name="var2")
mulv = tf.mul(var1, var2)
print (mulv)
Tensor("Mul_2:0", shape=(), dtype=int32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # always need to initialize the variable
print ("The variable var1 is:::", sess.run(var1))
print ("The variable var2 is:::", sess.run(var2))
print ("The computational result is:::", sess.run(mulv))
The variable var1 is::: 2
The variable var2 is::: 3
The computational result is::: 6
# Placeholder
pl = tf.placeholder(tf.float32, name="p")
pi = tf.constant(3.) c = tf.add(pl, pi)
print (c)
Tensor("Add_1:0", dtype=float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # always need to initialize the var iables
writer = tf.train.SummaryWriter("output", sess.graph)
#print("The placeholder value passed:::", sess.run(pl, {pl:3}))
print("The calculation result is:::", sess.run(c, {pl:3}))
writer.close()
WARNING:tensorflow:From <ipython‐input‐15‐4c5578691c20>:3 in <module>.: SummaryWriter.__init__ (from tensorflow.python.training.summary_io) is deprecated and will be
removed after 2016‐11‐30.
Instructions for updating:
Please switch to tf.summary.FileWriter. The interface and behavior is the same; thi s is just a rename.
The calculation result is::: 6.0
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- ryyc.cn 版权所有 湘ICP备2023022495号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务