---
# System prepended metadata

title: Tensorflow

---

# Anaconda環境建置
## 下載最新版本的anaconda
https://www.anaconda.com/download
1. 建立虛擬環境
python選擇最新版本的前幾個版本(我選python3.9.18)
![image](https://hackmd.io/_uploads/rkEn7k_tp.png)
2. 從anaconda虛擬環境開啟terminal並安裝tensorflow gpu
![image](https://hackmd.io/_uploads/r1b_kN6Fp.png)
 ```
 conda install tensorflow-gpu
 ```
3. 測試tensorflow gpu是否被成功調用
安裝jupyter lab並launch，進行測試如下圖
![image](https://hackmd.io/_uploads/ry3x91OKa.png)
4. 一樣從anaconda虛擬環境開啟terminal並安裝keras
```
conda install keras
```
4. AttributeError: module 'numpy' has no attribute 'object'解决方法
一樣從anaconda虛擬環境開啟terminal，輸入以下
```
pip install numpy==1.23.4
```
5. 回到jupyter lab並執行以下程式碼測試
```
from __future__ import absolute_import, division, print_function, unicode_literals
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
# import matplotlib.pyplot as plt

print(tf.__version__)

fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0

model = keras.Sequential([
 keras.layers.Flatten(input_shape=(28, 28)),
 keras.layers.Dense(128, activation='relu'),
 keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
 loss='sparse_categorical_crossentropy',
 metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)

print('\nTest accuracy:', test_acc)
```
![image](https://hackmd.io/_uploads/Hy4XakuK6.png)
6. 有缺失套件時，打開該環境之terminal進行安裝。
```
conda install 你要的套件==某個版本
```
7. 查看已安裝套件與版本
```
pip list
```
![image](https://hackmd.io/_uploads/H19Ee4pFT.png)
可以看到tensorflow-gpu 2.6.0版本