1.环境安装
步骤1:打开anaconda prompt
步骤2:conda create -n tf2-gpu-py3 python=3.6.7 anaconda
conda activate tf2-gpu-py3
步骤3:python -m pip install --upgrade pip
步骤4:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu==2.0.0-alpha0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==1.16.0
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib sklearn pandas jupyter
步骤5:https://developer.nvidia.com/cuda-toolkit-archive下载CUDA,完成后点击安装
步骤6:输入cmd,进入终端,输入set path,查看CUDA的目录,如C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin
步骤7:https://developer.nvidia.com/rdp/cudnn-archive下载对应CUDA版本的cuDNN,如Download cuDNN v7.6.1 (June 24, 2019), for CUDA 10.0,解压后,复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0目录,下载cuDNN需要登陆账号,可以选择使用微信或QQ登陆
步骤8:添加到系统环境变量path中:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include
步骤9:重启电脑,打开anaconda prompt,conda activate tf2-gpu-py3
步骤10:python
>>> import tensorflow as tf
>>> print(tf.__version__)
2.0.0-alpha0
>>> tf.test.is_gpu_available()
2019-08-26 16:12:05.591836: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-08-26 16:12:05.760799: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll
2019-08-26 16:12:06.323888: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1467] Found device 0 with properties:
name: GeForce GTX 965M major: 5 minor: 2 memoryClockRate(GHz): 1.15
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.33GiB
2019-08-26 16:12:06.336264: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1546] Adding visible gpu devices: 0
2019-08-26 16:12:17.835996: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1015] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-08-26 16:12:17.842351: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1021] 0
2019-08-26 16:12:17.846232: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1034] 0: N
2019-08-26 16:12:17.853620: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1149] Created TensorFlow device (/device:GPU:0 with 3035 MB memory) -> physical GPU (device: 0, name: GeForce GTX 965M, pci bus id: 0000:01:00.0, compute capability: 5.2)
True
>>> exit()
步骤11:jupyer notebook
ctrl+c
conda deactivate
2.分类模型
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tffrom tensorflow import keras
print(tf.__version__)
2.0.0-alpha0
# 步骤1:数据导入
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
(5000, 28, 28) (5000,) (55000, 28, 28) (55000,) (10000, 28, 28) (10000,)
# 步骤2:数据预处理
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train.astype(np.float32).reshape(-1, 784)).reshape(-1, 28, 28)
x_valid_scaled = scaler.transform(x_valid.astype(np.float32).reshape(-1, 784)).reshape(-1, 28, 28)
x_test_scaled = scaler.transform(x_test.astype(np.float32).reshape(-1, 784)).reshape(-1, 28, 28)
# 步骤3:模型搭建
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
for _ in range(20):
model.add(keras.layers.Dense(100, activation="selu"))
model.add(keras.layers.AlphaDropout(rate=0.5))