美文网首页tensorflow
tensorflow笔记(张量、计算图、会话)-mooc(北京大

tensorflow笔记(张量、计算图、会话)-mooc(北京大

作者: Jasmine晴天和我 | 来源:发表于2019-05-22 14:15 被阅读0次

基于tensorflow的NN,用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型。

张量(tensor):多维数组(列表),阶(张量的维数),可以表示0阶到n阶数组(列表)

维数 名字 例子
0-D 0 标量scalar s=1 2 3
1-D 1 向量vector v=[1 2 3]
2-D 2 矩阵matrix m = [[1,2,3],[4,5,6],[7,8,9]]
n-D n 张量tensor t = [[[...(n个)
import tensorflow as tf 
a = tf.constant([1.0,2.0]) #常数
b = tf.constant([3.0,4.0])
result = a+b 
print(result) #是一个张量

"add"节点名,“0”第0个输出,shape维度,(2,)一维数据,长度是2,dtype数据类型

计算图(graph):承载多个计算节点,搭建神经网络的计算过程,只搭建,不运算。

x = tf.constant([[1.0, 2.0]])
w = tf.constant([[3.0], [4.0]])
y = tf.matmul(x,w)
print(y)

会话(session):执行计算图中的节点运算,来得到运算结果

with tf.Session() as sess:
    print(sess.run(y))
with tf.Session() as sess:
    print(sess.run(y))
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))

参数:即神经网络上神经元的权重,用变量表示,随机给初值。

w = tf.Variable(tf.random_normal([2,3], stddev=2, mean=0, seed=1)) #随机生成参数,正态分布,矩阵2*3,标准差是2,均值为0

神经网络的层数等于隐藏层加输出层,输入层不算在神经网络层数中。

相关文章

网友评论

    本文标题:tensorflow笔记(张量、计算图、会话)-mooc(北京大

    本文链接:https://www.haomeiwen.com/subject/sdzyzqtx.html