深度学习——tensorflow2.0搭建分类和回归模型

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 tf

from 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))

model.add(keras.layers.Dense(10, activation="softmax"))
model.compile(loss="sparse_categorical_crossentropy", optimizer = "sgd", metrics = ["accuracy"])

model.summary()

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 100)               78500     
_________________________________________________________________
dense_1 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_2 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_3 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_4 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_5 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_6 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_7 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_8 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_9 (Dense)              (None, 100)               10100     
_________________________________________________________________
dense_10 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_11 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_12 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_13 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_14 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_15 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_16 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_17 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_18 (Dense)             (None, 100)               10100     
_________________________________________________________________
dense_19 (Dense)             (None, 100)               10100     
_________________________________________________________________
alpha_dropout (AlphaDropout) (None, 100)               0         
_________________________________________________________________
dense_20 (Dense)             (None, 10)                1010      
=================================================================
Total params: 271,410
Trainable params: 271,410
Non-trainable params: 0
_________________________________________________________________

# 步骤4:训练和验证模型

logdir = ".\dnn-callbacks" 
if not os.path.exists(logdir):
    os.mkdir(logdir)
output_model_file = os.path.join(logdir,"fashion_mnist_model.h5")

callbacks = [
    keras.callbacks.TensorBoard(logdir),
    keras.callbacks.ModelCheckpoint(output_model_file,save_best_only = True),
    keras.callbacks.EarlyStopping(patience=5, min_delta=1e-3),
]

history = model.fit(x_train_scaled, y_train, epochs=10,validation_data=(x_valid_scaled, y_valid),callbacks = callbacks)

 

Train on 55000 samples, validate on 5000 samples
Epoch 1/10
55000/55000 [==============================] - 9s 171us/sample - loss: 1.2599 - accuracy: 0.5799 - val_loss: 0.8497 - val_accuracy: 0.7878
Epoch 2/10
55000/55000 [==============================] - 8s 150us/sample - loss: 0.7253 - accuracy: 0.7650 - val_loss: 0.7718 - val_accuracy: 0.8244
Epoch 3/10
55000/55000 [==============================] - 9s 156us/sample - loss: 0.5915 - accuracy: 0.8106 - val_loss: 0.7111 - val_accuracy: 0.8424
Epoch 4/10
55000/55000 [==============================] - 9s 157us/sample - loss: 0.5271 - accuracy: 0.8299 - val_loss: 0.6823 - val_accuracy: 0.8500
Epoch 5/10
55000/55000 [==============================] - 8s 150us/sample - loss: 0.4831 - accuracy: 0.8416 - val_loss: 0.6299 - val_accuracy: 0.8562
Epoch 6/10
55000/55000 [==============================] - 8s 140us/sample - loss: 0.4503 - accuracy: 0.8527 - val_loss: 0.6326 - val_accuracy: 0.8602
Epoch 7/10
55000/55000 [==============================] - 8s 153us/sample - loss: 0.4278 - accuracy: 0.8588 - val_loss: 0.6135 - val_accuracy: 0.8642
Epoch 8/10
55000/55000 [==============================] - 8s 152us/sample - loss: 0.4086 - accuracy: 0.8654 - val_loss: 0.6081 - val_accuracy: 0.8670
Epoch 9/10
55000/55000 [==============================] - 8s 154us/sample - loss: 0.3898 - accuracy: 0.8704 - val_loss: 0.6020 - val_accuracy: 0.8672
Epoch 10/10
55000/55000 [==============================] - 8s 154us/sample - loss: 0.3757 - accuracy: 0.8741 - val_loss: 0.6161 - val_accuracy: 0.8688

model.evaluate(x_test_scaled, y_test)

10000/10000 [==============================] - 1s 70us/sample - loss: 0.6765 - accuracy: 0.8572

Out[ ]:

[0.6764720353841782, 0.8572]

3.回归模型

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 tf

from tensorflow import keras

print(tf.__version__)

2.0.0-alpha0

from sklearn.datasets import fetch_california_housing

from sklearn.model_selection import train_test_split

housing = fetch_california_housing()

x_train_all, x_test, y_train_all, y_test = train_test_split(housing.data, housing.target, random_state = 6)
x_train, x_valid, y_train, y_valid = train_test_split(x_train_all, y_train_all, random_state = 7)
print(x_train.shape, y_train.shape)
print(x_valid.shape, y_valid.shape)
print(x_test.shape, y_test.shape)

(11610, 8) (11610,)
(3870, 8) (3870,)
(5160, 8) (5160,)

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
x_valid_scaled = scaler.transform(x_valid)
x_test_scaled = scaler.transform(x_test)

def build_model(hidden_layers = 1, layer_size = 30, learning_rate = 3e-3):
    model = keras.models.Sequential()
    model.add(keras.layers.Dense(layer_size, activation='relu', input_shape=x_train.shape[1:]))
    for _ in range(hidden_layers - 1):
        model.add(keras.layers.Dense(layer_size, activation = 'relu'))
    model.add(keras.layers.Dense(1))
    optimizer = keras.optimizers.SGD(learning_rate)
    model.compile(loss = 'mse', optimizer = optimizer)
    return model

sklearn_model = keras.wrappers.scikit_learn.KerasRegressor(build_fn = build_model)
callbacks = [keras.callbacks.EarlyStopping(patience=5, min_delta=1e-2)]
history = sklearn_model.fit(x_train_scaled, y_train,validation_data = (x_valid_scaled, y_valid),epochs = 100,callbacks = callbacks)

Train on 11610 samples, validate on 3870 samples
Epoch 1/100
11610/11610 [==============================] - 1s 57us/sample - loss: 1.4086 - val_loss: 0.7144
Epoch 2/100
11610/11610 [==============================] - 0s 36us/sample - loss: 0.6111 - val_loss: 0.6281
Epoch 3/100
11610/11610 [==============================] - 0s 40us/sample - loss: 0.5514 - val_loss: 0.5723
Epoch 4/100
11610/11610 [==============================] - 0s 39us/sample - loss: 0.5105 - val_loss: 0.5323
Epoch 5/100
11610/11610 [==============================] - 0s 41us/sample - loss: 0.4841 - val_loss: 0.5068
Epoch 6/100
11610/11610 [==============================] - 0s 39us/sample - loss: 0.4634 - val_loss: 0.4904
Epoch 7/100
11610/11610 [==============================] - 0s 36us/sample - loss: 0.4497 - val_loss: 0.4749
Epoch 8/100
11610/11610 [==============================] - 0s 41us/sample - loss: 0.4395 - val_loss: 0.4632
Epoch 9/100
11610/11610 [==============================] - 1s 47us/sample - loss: 0.4312 - val_loss: 0.4533
Epoch 10/100
11610/11610 [==============================] - 1s 48us/sample - loss: 0.4247 - val_loss: 0.4480
Epoch 11/100
11610/11610 [==============================] - 0s 43us/sample - loss: 0.4194 - val_loss: 0.4430
Epoch 12/100
11610/11610 [==============================] - 0s 33us/sample - loss: 0.4153 - val_loss: 0.4357
Epoch 13/100
11610/11610 [==============================] - 0s 33us/sample - loss: 0.4117 - val_loss: 0.4340
Epoch 14/100
11610/11610 [==============================] - 0s 34us/sample - loss: 0.4077 - val_loss: 0.4294
Epoch 15/100
11610/11610 [==============================] - 0s 36us/sample - loss: 0.4050 - val_loss: 0.4243
Epoch 16/100
11610/11610 [==============================] - 0s 32us/sample - loss: 0.4017 - val_loss: 0.4200
Epoch 17/100
11610/11610 [==============================] - 0s 32us/sample - loss: 0.3990 - val_loss: 0.4181
Epoch 18/100
11610/11610 [==============================] - 0s 33us/sample - loss: 0.3964 - val_loss: 0.4169
Epoch 19/100
11610/11610 [==============================] - 0s 32us/sample - loss: 0.3948 - val_loss: 0.4127
Epoch 20/100
11610/11610 [==============================] - 0s 40us/sample - loss: 0.3930 - val_loss: 0.4087
Epoch 21/100
11610/11610 [==============================] - 1s 48us/sample - loss: 0.3902 - val_loss: 0.4078
Epoch 22/100
11610/11610 [==============================] - 0s 36us/sample - loss: 0.3885 - val_loss: 0.4060
Epoch 23/100
11610/11610 [==============================] - 1s 45us/sample - loss: 0.3869 - val_loss: 0.4028
Epoch 24/100
11610/11610 [==============================] - 0s 40us/sample - loss: 0.3848 - val_loss: 0.4014
Epoch 25/100
11610/11610 [==============================] - 0s 33us/sample - loss: 0.3839 - val_loss: 0.4003
Epoch 26/100
11610/11610 [==============================] - 0s 38us/sample - loss: 0.3825 - val_loss: 0.3983
Epoch 27/100
11610/11610 [==============================] - 0s 38us/sample - loss: 0.3814 - val_loss: 0.3966
Epoch 28/100
11610/11610 [==============================] - 0s 38us/sample - loss: 0.3795 - val_loss: 0.3965
Epoch 29/100
11610/11610 [==============================] - 0s 33us/sample - loss: 0.3781 - val_loss: 0.3932

from scipy.stats import reciprocal

param_distribution = {
    "hidden_layers":[1, 2, 3, 4],
    "layer_size": np.arange(1, 100),
    "learning_rate": reciprocal(1e-4, 1e-2),
}

from sklearn.model_selection import RandomizedSearchCV
random_search_cv = RandomizedSearchCV(sklearn_model,
                                      param_distribution,
                                      n_iter = 10,
                                      cv = 3,
                                      n_jobs = 1)
random_search_cv.fit(x_train_scaled, y_train, epochs = 100,validation_data = (x_valid_scaled, y_valid),callbacks = callbacks)

RandomizedSearchCV(cv=3, error_score='raise-deprecating',
                   estimator=,
                   iid='warn', n_iter=10, n_jobs=1,
                   param_distributions={'hidden_layers': [1, 2, 3, 4],
                                        'layer_size': array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
       35, 36...7, 48, 49, 50, 51,
       52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
       69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
       86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]),
                                        'learning_rate': },
                   pre_dispatch='2*n_jobs', random_state=None, refit=True,
                   return_train_score=False, scoring=None, verbose=0)

print(random_search_cv.best_params_)
print(random_search_cv.best_score_)
print(random_search_cv.best_estimator_)

{'hidden_layers': 3, 'layer_size': 84, 'learning_rate': 0.006306161764204613}
-0.33262315739032833

model = random_search_cv.best_estimator_.model
model.evaluate(x_test_scaled, y_test)

5160/5160 [==============================] - 0s 20us/sample - loss: 0.3405

Out[ ]:

0.3405195293500442

 

 

 

你可能感兴趣的:(深度学习——tensorflow2.0搭建分类和回归模型)