tensorflow基础知识学习

一:基础单位
张量:多维数组(列表) 阶:表示张量的维度
标量:0阶数组 例子:s=1,2,3
向量:1阶数组 例子:v=[1,2,3]
矩阵:2阶数组 例子:m=[[1,2,3],[4,5,6],[7,8,9]]
张量:n阶 例子:t=[[…n个

二:简单的实现代码

import tensorflow as tf

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

result = tf.matmul(x,w)
print(result)
Tensor("MatMul:0", shape=(1, 1), dtype=float32)

result是MatMul:0的张量,是一维长度为1的数组,数据类型是float32
值得注意的是这里并没有输出result的值,而只是返回了result的类型,也就是说计算图只描述运算过程,但未计算运算结果(只搭建网络,不运算)
tensorflow基础知识学习
那么如何才能计算出输出结果呢?这里需要引入session会话:

import tensorflow as tf

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

result = tf.matmul(x,w)
print(result)

with tf.Session() as sess:
    print(sess.run(result))
Tensor("MatMul:0", shape=(1, 1), dtype=float32)
[[11.]]