尝试交给我一个适配tensorflow -gpu 2.6.0的适配代码
时间: 2023-06-12 09:03:27 浏览: 62
好的,以下是一个适配 TensorFlow 2.6.0 + GPU 的代码示例:
```python
import tensorflow as tf
# 检查GPU是否可用
if not tf.test.is_gpu_available():
print("GPU not available.")
else:
print("GPU available.")
# 构建模型
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10)
])
# 编译模型
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
# 加载数据
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train = tf.one_hot(y_train, depth=10)
y_test = tf.one_hot(y_test, depth=10)
# 训练模型
model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))
```
这段代码首先检查GPU是否可用,然后构建一个简单的神经网络模型,使用Adam优化算法和分类交叉熵损失函数来编译模型,然后加载MNIST数据集并对模型进行训练。
请注意,如果您的TensorFlow安装不支持GPU,或者您的计算机没有GPU,那么您将看到“GPU not available.”的输出。如果您的计算机有GPU且TensorFlow支持GPU,那么您将看到“GPU available.”的输出,并且模型将在GPU上进行训练。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)