lesson13 张量、计算图、会话

https://www.bilibili.com/video/av22530538/?p=13

#lesson13  张量、计算图,会话
#搭建第一个神经网络,总结搭建八股
#基于Tensorflow的NN:用张量表示数据,用计算图搭建神经网络,
#用会话执行计算图,优化线上的权重(参数),得到模型。
#
#张量:多维数组(列表)  阶:张量的维数
#维数       阶          名字          列子
#0-D        0         标量scalar     s=1 2 3
#1-D        1         向量vector     v=[1,2,3]
#2-D        2         矩阵matrix     v=[1,2,3],[4,5,6],[7,8,9]
#n-D        n         向量vector     v=[[...]]  n个

#张量可以表示0阶到n阶数组(列表)

#数据类型:tf.float32  tf.int32  ...

#import tensflow as tf
#a = tf.constant([1.0,2.0])
#b = tf.constant([3.0,4.0])
#result =a + b
#print result

#显示:Tensor("add:0",shape=(2,),dtype=float32)
#        节点名  第0个输出  维度  一维数组长度2  数据类型

import tensorflow as tf
a = tf.constant([1.0,2.0])
b = tf.constant([3.0,4.0])
result =a + b
print(result)


#Tensor("add_1:0", shape=(2,), dtype=float32)
#计算图(Graph):搭建神经网络的计算过程,只搭建,不运算。


import tensorflow as tf
x = tf.constant([[1.0,2.0]])
w = tf.constant([[3.0],[4.0]])
y = tf.matmul(x,w)
print(y)
#Tensor("MatMul_34:0", shape=(1, 1), dtype=float32)
#会话(Session):执行计算图中的节点运算。
import tensorflow as tf
x = tf.constant([[1.0,2.0]])
w = tf.constant([[3.0],[4.0]])
y = tf.matmul(x,w)
print(y)
with tf.Session() as sess:
    print sess.run(y)
    
#Tensor("MatMul_35:0", shape=(1, 1), dtype=float32)
#[[11.]]

猜你喜欢

转载自blog.csdn.net/ldinvicible/article/details/82826058