首页 > 技术文章 > tensorflow学习笔记

luozeng 2018-04-02 09:58 原文

简介:

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

张量(tensor):多维数组(列表)   阶:张量的维数

1 维数        阶        名字                例子
2 0-D         0     标量 scalar       s=123
3 1-D         1     向量 vector       v=[1,2,3]
4 2-D         2     矩阵 matrix       m=[[1,2,3],[4,5,6],[7,8,9]]
5 n-D         n     张量 tensor       t=[[[...... n个
6 
7 张量   可以表示0阶到n阶数组(列表)

数据类型:tf.float32    tf.float64  ....

1 import tensorflow as tf
2 
3 a=tf.constant([1.0,2.0])
4 b=tf.constant([[3.0],[4.0]])
5 result=a+b
6 print result
7 
8 
9 >>>:Tensor("add:0", shape=(2, 2), dtype=float32)

结果解释:
"add:0":  add(节点名)0(第0个输出)
shape=(2, 2):  shape(维度)(2,2)二维数组长度为2
dtype=float32:  dtype(数据类型)

计算图(graph):搭建神经网络的计算过程,只搭建,不运算。

会话(session):执行计算图中的节点运算

1 import tensorflow as tf
2 
3 a=tf.constant([1.0,2.0])
4 b=tf.constant([[3.0],[4.0]])
5 y=tf.matmul(a,b)
6 print y
7 with tf.Session() as sess:
8     print sess.run(y)

参数:线上的权重W,用变量表示,随机给初值。

 

推荐阅读