我的安装历程:
- 安装了tensorflow 2.0 CPU
- CPU运行了一个CNN之后感觉要炸了
- 新建环境安装 tensorflow 2.0 GPU版本
- 运行了一个程序之后发现我的显卡驱动和cuda版本不匹配
- 查看 cuda是10.0版本,而我的 930m 仅支持9.0
- 更换 cuda 为 9.0 同时它把我的 tensorflow 2.0 也更改为了 1.14, 好吧
- 运行了一个 CNN ,时间真的缩短了很多!
- 查看任务管理器,GPU确实有占用
打开 Anaconda Navigator
新建一个环境,搜索tensorflow, 选择想要安装的版本,apply,等待安装就好了
安装CPU版本和GPU版本都可以直接安装,GPU版本不需要提前安装CUDA等,因为tensorflow-gpu在安装的过程中会自动安装相应版本的CUDA
sorry,图片上写错了,安装不同版本不是右键,而是单击那个绿色对号,选择需要的版本,具体可以看下面这张图
安装成功之后,点击tensorflow-gpu旁边的绿色三角可以运行terminal
或者可以继续在这里搜索Jupyter notebook,apply,等待安装,然后就可以在绿色三角那里选择jupyter notebook 运行你的程序了~
如果像我一样运行了之后显示cuda版本不对:
在这里更改版本,它也会相应的将你的tensorflow版本更改的~
Tips
- 一个简单的程序供测试
import numpy as np
import tensorflow as tf
from tensorflow import keras
model = keras.Sequential([keras.layers.Dense(units = 1, input_shape = [1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))
- CNN程序供测试
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels),(test_images, test_labels) = fashion_mnist.load_data()
# 如果加载不出来,可以下载fishion_mnist数据集到本地然后这样:
#=======================================
import numpy as np
import os
import gzip
def load_data(data_folder):
files = [
'train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz'
]
paths = []
for fname in files:
paths.append(os.path.join(data_folder,fname))
with gzip.open(paths[0], 'rb') as lbpath:
y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[1], 'rb') as imgpath:
x_train = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_train), 28, 28)
with gzip.open(paths[2], 'rb') as lbpath:
y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
with gzip.open(paths[3], 'rb') as imgpath:
x_test = np.frombuffer(
imgpath.read(), np.uint8, offset=16).reshape(len(y_test), 28, 28)
return (x_train, y_train), (x_test, y_test)
(train_images, train_labels), (test_images, test_labels) = load_data('fashion')
#===============================
model = keras.Sequential([
keras.layers.Flatten(input_shape = (28, 28)),
keras.layers.Dense(128, activation = tf.nn.relu),
keras.layers.Dense(10, activation = tf.nn.softmax)
])
train_images = train_images / 255.0
test_images = test_images / 255.0
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
model.evaluate(test_images, test_labels)
我有上传这个的ipynb文件,设置的 0 C币 ,em所以应该可以免费下载叭