[深度学习笔记]TensorFlow-思想

版权声明:未经博主允许不得转载(https://github.com/ai-word) https://blog.csdn.net/BaiHuaXiu123/article/details/89162908

在进行TensorFlow编程时一定要按照规则来进行,TensorFlow程序中包含两部分:

  • 建计算图的部分
  • 建好的计算图放在一个 Sesstion 会话中的执行部分

如下图所示:
在这里插入图片描述
解释

  • 构建计算图: 定义变量、初始化数据,建立运算关系。
  • 把计算图放到一个Sesstion中执行得到执行结果。

例子
实现矩阵乘法y = W*x

w = [[3.0,5.5],[1.0,7.7]]; 
x = [1.0,1.0]

实现如下:

import tensorflow as tf

###-----必须先构建计算图(y=w*x)---------###
graph1 = tf.Graph()                        #定义图graph1
with graph1.as_default():                  #指定图 graph1为默认图
   w = tf.Variable([[3.0,2.5],[1.0,2.7]]) #图中边Variabel的定义
   x = tf.constant(1.0,shape=[1,2])       #图中边x_tensor的生成

   y = tf.matmul(w,tf.transpose(x))       #节点:变量的乘法和转置操作(operation)

   init_op = tf.global_variables_initializer()   #节点: Variable的initialize_operation

###------建好的计算图放在一个Sesstion中执行-------###
with tf.Session(graph=graph1) as sess:    # 打开Session,把graph1添加到默认会话sess中
   sess.run(init_op)                     # 执行init_op操作,此时tf.matmul并没有run  
   print(sess.run(y))                    # 在会话中执行tf.matmul操作, 打印y_tensor

注:

-张量(tensor):多维数组,类似于 numpy 中的 narray

  • Operation::比如矩阵乘法操作。

资源下载
资源下载
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/BaiHuaXiu123/article/details/89162908