TensorFlow基本计算单元:代码示例

1,代码示例:

import tensorflow as tf
a = 3
#创建变量
w = tf.Variable([[0.6,1.2]])#创建行向量
x = tf.Variable([[2.1],[4.2]]) #创建列向量
y = tf.matmul(w, x) #矩阵相乘

init_op = tf.global_variables_initializer()# 初始化
with tf.Session() as sess:
   sess.run(init_op)
   print(y.eval()) #输出y的值

2,代码解析:

调用TensorFlow中相应的函数模块(API),需要将变量转换成TensorFlow支持的格式(tf.Variable),使用tf.Variable时,创建的是相应变量的内存区域(没有具体的值),在TensorFlow中需要进一步调用tf.global_variables_initializer,对全局的变量进行初始化操作(完成赋值操作),所有的上述操作需要在tf.Session定义的计算图中完成。

猜你喜欢

转载自blog.csdn.net/qq_43660987/article/details/92062065