从零学习量化交易004GPU开发测试

(1)打开Spyder,编写 2_GPU_Development_Test 程序,保存程序到 D:\zwPython\zwrk\1_Quant_TensorFlow

# -*- coding: utf-8 -*-
# 声明文件的编码格式为 UTF-8,确保文件中包含的非 ASCII 字符(如中文等)能被正确处理

import tensorflow as tf
# 导入 TensorFlow 库,TensorFlow 是一个广泛用于机器学习和深度学习的开源框架

#-----------------
# 分隔线,用于区分不同的代码功能模块

#1
print('\n#1')
# 打印换行符和编号 #1,用于标识当前执行的测试部分
print('tf.__version__:', tf.__version__)
# 打印当前导入的 TensorFlow 库的版本号,方便确认使用的 TensorFlow 版本
hello = tf.constant('Hello, TensorFlow!')
# 使用 tf.constant 函数创建一个常量张量 hello,其值为字符串 'Hello, TensorFlow!'
print(hello.numpy())
# 使用 numpy() 方法将 TensorFlow 张量 hello 转换为 NumPy 数组并打印,
# 在 TensorFlow 2.x 的即时执行模式下,可以直接获取张量的值

#3
print('\n#3')
# 打印换行符和编号 #3,标识当前测试部分
a = tf.constant(11)
# 使用 tf.constant 函数创建一个常量张量 a,其值为整数 11
b = tf.constant(22)
# 使用 tf.constant 函数创建一个常量张量 b,其值为整数 22
ds2 = a + b
# 对常量张量 a 和 b 进行加法运算,结果存储在变量 ds2 中,这是即时执行的操作
print(ds2.numpy())
# 使用 numpy() 方法将结果张量 ds2 转换为 NumPy 数组并打印

#4
print('\n#4')
# 打印换行符和编号 #4,标识当前测试部分
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
# 使用 tf.constant 函数创建一个形状为 [2, 3] 的常量张量 a,其值为指定的一维数组,
# 并为该张量命名为 'a'
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
# 使用 tf.constant 函数创建一个形状为 [3, 2] 的常量张量 b,其值为指定的一维数组,
# 并为该张量命名为 'b'
c = tf.matmul(a, b)
# 使用 tf.matmul 函数对张量 a 和 b 进行矩阵乘法操作,结果存储在张量 c 中
print(c.numpy())
# 使用 numpy() 方法将矩阵乘法结果张量 c 转换为 NumPy 数组并打印

输出结果为:

#1
tf.__version__: 2.1.0
b'Hello, TensorFlow!'

#3
33

#4
[[22. 28.]
 [49. 64.]]

发布者:股市刺客,转载请注明出处:https://www.95sca.cn/archives/907181
站内所有文章皆来自网络转载或读者投稿,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!

(0)
股市刺客的头像股市刺客
上一篇 3分钟前
下一篇 1分钟前

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注