import torch
print(torch.__version__)
torch.rand(1, device="cuda:0")
# 如果用的tf 2.x版本
import tensorflow as tf
with tf.device('/gpu:0'):
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2])
c = tf.matmul(a, b)
print(c)
# 如果用的tf 1.x版本
import tensorflow as tf
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.4,
allow_growth=True)
config = tf.ConfigProto(gpu_options=gpu_options)
with tf.device('/gpu:0'):
a = tf.get_variable('var_a', initializer=tf.constant(15.0))
b = tf.get_variable('var_b', initializer=tf.constant(25.0))
with tf.device('/cpu:0'):
c = a + b
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
result = sess.run(c)
print(f"\n计算结果: {result:.1f}")