搭建简单神经网络
深度学习的优化方法: 应该看更多的代码,了解这些优化方法的使用方式, 在层中的位置 设计出巧妙的结构(函数或类),可以使参数的设置更加自如
随机梯度下降:SGD,可以设置参数为动量梯度下降
Adagrad、RMSprop、Adam
tf.keras.optimizers.Adam
学习率设置?
正则化:
L2正则化,直接在层里面加
kernel_regularizer=regularizers.l2(0.01)
Dropout正则化:在每一层后面+
model.add(Dropout(0.3))
批标准化(BN层)
Earlystopping:
在model.fit,或者gridsearch.fit的时候用
callbacks_list = [EarlyStopping(monitor='val_loss', patience=5,min_delta=0.0001)]
grid_search.fit(X_sca, Y,callbacks=callbacks_list)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation,BatchNormalization
from keras.wrappers.scikit_learn import KerasRegressor,KerasClassifier
from tensorflow.keras import utils
from tensorflow.keras import regularizers
data = pd.read_csv("11_train_classification.csv")
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold,GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
from keras.callbacks import EarlyStopping
from keras.callbacks import EarlyStopping
dataset = data.values
print(dataset)
X=dataset[:,0:13]
Y=dataset[:,13]
def larger_model(ndense = 2,a=20,b=10,c=0):
model = Sequential()
cell = [a,b,c]
for i in range(ndense):
if i == 0:
model.add(Dense(units=cell[i],input_dim=13,activation="relu",
kernel_regularizer=regularizers.l2(0.01),kernel_initializer="he_normal"))
model.add(Dropout(0.3))
else:
model.add(Dense(units=cell[i],activation="relu",
kernel_regularizer=regularizers.l2(0.01),kernel_initializer="he_normal"))
model.add(Dropout(0.3))
model.add(Dense(units=3,activation="softmax"))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
return model
def cross_validation(model,X,Y,cv=5):
kf = KFold(cv, shuffle=True, random_state=1)
train_acc=[]
test_acc=[]
t=0
for train_index, test_index in kf.split(X):
train_X=X[train_index]
train_y=Y[train_index]
test_X=X[test_index]
test_y=Y[test_index]
model.fit(train_X,train_y)
acc=accuracy_score(model.predict(train_X),train_y)
train_acc.append(acc)
acc=accuracy_score(model.predict(test_X),test_y)
test_acc.append(acc)
return np.array(test_acc)
seed = 7
np.random.seed(seed)
sca=StandardScaler()
X_sca=sca.fit_transform(X)
param_grid = [{'ndense':[1],'a':list(range(10,31,2)),'epochs': [500], 'batch_size': [4]},
{'ndense':[2],'a':list(range(1,31,2)),'b':list(range(1,21,2)),'epochs': [500], 'batch_size': [4]},
{'ndense':[3],'a':list(range(1,31,2)),'b':list(range(1,31,2)),'c':list(range(5,20,2)),'epochs': [500], 'batch_size': [4]}]
model=KerasClassifier(build_fn=larger_model)
callbacks_list = [EarlyStopping(monitor='val_loss', patience=5,min_delta=0.0001)]
grid_search = GridSearchCV(model, param_grid, cv=5,
verbose=2,n_jobs=-1)
grid_search.fit(X_sca, Y,callbacks=callbacks_list)
print(grid_search.best_params_)
AlexNet
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation,BatchNormalization
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPool2D
from keras.wrappers.scikit_learn import KerasRegressor,KerasClassifier
from tensorflow.keras import utils
from tensorflow.keras import regularizers
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold,GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
from keras.callbacks import EarlyStopping
from tensorflow.keras.datasets import mnist
def net():
model = Sequential()
model.add(Conv2D(filters=96,kernel_size=11,strides=4,activation="relu",))
model.add(MaxPool2D(pool_size=3,strides=2))
model.add(Conv2D(filters=256,kernel_size=5,strides=1,padding="same",activation="relu"))
model.add(MaxPool2D(pool_size=3,strides=2))
model.add(Conv2D(filters=384,kernel_size=3,padding="same",activation="relu"))
model.add(Conv2D(filters=384, kernel_size=3, padding="same", activation="relu"))
model.add(Conv2D(filters=256, kernel_size=3, padding="same", activation="relu"))
model.add(MaxPool2D(pool_size=3,strides=2))
model.add(tf.keras.layers.Flatten())
model.add(Dense(4096,activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(4096,activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(10,activation="softmax"))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['acc'])
return model
(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
train_images = np.reshape(train_images,(train_images.shape[0],train_images.shape[1],train_images.shape[2],1))
test_images = np.reshape(test_images,(test_images.shape[0],test_images.shape[1],test_images.shape[2],1))
def get_train(size):
index = np.random.randint(0,train_images.shape[0],size)
resized_image = tf.image.resize_with_pad(train_images[index],227,227,)
return resized_image.numpy(),train_labels[index]
def get_test(size):
index = np.random.randint(0,np.shape(test_images)[0],size)
resized_image = tf.image.resize_with_pad(test_images[index],227,227,)
return resized_image.numpy(),test_labels[index]
train_images,train_labels = get_train(10000)
test_images,test_labels = get_test(10000)
net = net()
x = tf.random.uniform((1,227,227,1))
y=net(x)
net.fit(train_images,train_labels,batch_size=500,epochs=6,validation_split=0.1,verbose=1)
|