《TensorFlow实战》TensorFlow上手代码

《TensorFlow实战》TensorFlow上手代码_第1张图片

#导入TensorFlow 并简写成tf
import tensorflow as tf

#定义变量
a = tf.constant([1.0, 2.0], name = 'a')

b = tf.constant([2.0, 3.0], name = 'b')

result = a + b

#打印变量 将会输出
#Tensor("a:0", shape=(2,), dtype=float32)
#Tensor("b:0", shape=(2,), dtype=float32)
#Tensor("add:0", shape=(2,), dtype=float32)

print (a)
print (b)
print (result)

#创建会话
sess = tf.Session()

#执行计算
sum = sess.run(result)

#打印计算后的张量
#将会输出:[ 3.  5.]
print (sum)

你可能感兴趣的:(TensorFlow)