tensorflow 两种运行方式

import tensorflow as tf
matrix1=tf.constant([[3,3]])
matrix2=tf.constant([[1],[2]])
product=tf.matmul(matrix1,matrix2)

##method1
sess=tf.Session()
#result=sess.run(product)
#上下的两种方式完全一样,但eval()中必须有参数
#注意tf.Session中的S为大写,下行的session=sess中的s为小写,否则报错
result2=product.eval(session=sess)
print(result2)
sess.close()

#method2
with tf.Session() as sess:
#result2=sess.run(product)
#上下的两种方式完全一样,但由于在session块中,eval()中可以没有参数,采用默认参数
result2=product.eval()
print(result2)

发布了34 篇原创文章 · 获赞 1 · 访问量 4267

猜你喜欢

转载自blog.csdn.net/u010879745/article/details/102151595