TensorFlow Session使用的两种方法小结

yipeiwu_com6年前Python基础

TensorFlow Session

在TensorFlow中是通过session进行交互的,使用session有两种方法。下面通过一个简单的例子(两个矩阵相乘)说一下

{[3,1] 与{[5,2] 相乘

[1,2]} [2,4]}

代码

#encoding=utf-8
import tensorflow as tf

matrix1 = tf.constant([[3,1],[1,2]])
matrix2 = tf.constant([[5,2],[2,4]])

product = tf.matmul(matrix1,matrix2) #矩阵乘法

#session method 1
sess = tf.Session()
result = sess.run(product)
print(result)
sess.close()

#session method 2
with tf.Session() as sess2:
 result2 = sess2.run(product)
 print(result2)

结果

以上这篇TensorFlow Session使用的两种方法小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

完美解决Pycharm无法导入包的问题 Unresolved reference

完美解决Pycharm无法导入包的问题 Unresolved reference

如下所示: Unresolved reference 'ERROR_CODE_INPUT_ERROR' less... (Ctrl+F1) This inspection dete...

python目录与文件名操作例子

1、操作目录与文件名 #!/usr/bin/env python #-*- coding: utf-8 -*- import os,re import shutil import...

实现Python与STM32通信方式

实现Python与STM32通信方式

断断续续学了几周Stm32后,突然想实现上位机和下位机的通信,恰好自己学过一点python,便想通过python实现通信. 在网上看见python库pyserial可以实现此功能,便去官...

python实现的简单文本类游戏实例

本文实例讲述了python实现的简单文本类游戏实现方法。分享给大家供大家参考。具体实现方法如下: ##########################################...

对Python 2.7 pandas 中的read_excel详解

导入pandas模块: import pandas as pd 使用import读入pandas模块,并且为了方便使用其缩写pd指代。 读入待处理的excel文件: df =...