import tensorflow as tf
# 创建一个常量操作符
hello = tf.constant('Hello, TensorFlow!')
# 启动默认图
with tf.Session() as sess:
# 运行图,获取操作结果
print(sess.run(hello))
# 创建两个张量 (tensor)
a = tf.constant(10)
b = tf.constant(32)
# 定义加法操作
add_op = tf.add(a, b)
# 启动默认图
with tf.Session() as sess:
# 运行加法操作并打印结果
print("Addition result:", sess.run(add_op))
tf.constant
用于创建一个常量操作符。这里我们创建了一个包含字符串 "Hello, TensorFlow!" 的常量。tf.Session()
来启动一个会话(session),在会话中运行图中的操作。sess.run(hello)
来执行图中的操作,并获取结果。a
和 b
,并使用 tf.add
创建了一个加法操作。注意:以上代码适用于 TensorFlow 1.x 版本。如果你使用的是 TensorFlow 2.x 版本,代码会有所不同,因为 TensorFlow 2.x 默认启用了 eager execution 模式,不需要显式创建和管理会话。
上一篇:python+=代表什么
下一篇:python concat
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站