keras学习笔记(一)

模型最佳超参数选择

二分类问题

参考tutorial-first-neural-network-python-keras

数据集Pima+Indians+Diabetes,保存为pima-indians-diabetes.csv,放在工作目录下

# Create first network with Keras
from keras.models import Sequential
from keras.layers import Dense
import numpy
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(1, init='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, nb_epoch=150, batch_size=10,  verbose=2)
# calculate predictions
predictions = model.predict(X)
# round predictions
rounded = [round(x[0]) for x in predictions]
print(rounded)


多分类问题

参考multi-class-classification-tutorial-keras-deep-learning-library

数据集Iris,保存为iris.csv。

import numpy
import pandas
from keras.models import Sequential
from keras.layers import Dense
from keras.wrappers.scikit_learn import KerasClassifier
from keras.utils import np_utils
from sklearn.cross_validation import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# load dataset
dataframe = pandas.read_csv("iris.csv", header=None)
dataset = dataframe.values
X = dataset[:,0:4].astype(float)
Y = dataset[:,4]
# encode class values as integers
encoder = LabelEncoder()
encoder.fit(Y)
encoded_Y = encoder.transform(Y)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(encoded_Y)
# define baseline model
def baseline_model():
	# create model
	model = Sequential()
	model.add(Dense(8, input_dim=4, kernel_initializer='normal', activation='relu'))
	model.add(Dense(3, kernel_initializer='normal', activation='sigmoid'))
	# Compile model
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
	return model
estimator = KerasClassifier(build_fn=baseline_model, epochs=200, batch_size=5, verbose=0) 
#* evaluation
'''
kfold = KFold(n_splits=10, shuffle=True, random_state=seed)
results = cross_val_score(estimator, X, dummy_y, cv=kfold)
print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
'''

#* prediction
X_train, X_test, Y_train, Y_test = train_test_split(X, dummy_y, test_size=0.33, random_state=seed)
estimator.fit(X_train, Y_train)
predictions = estimator.predict(X_test)
print(predictions)
print(encoder.inverse_transform(predictions))

参考load-machine-learning-data-python

参考python-machine-learning-mini-course

参考compare-machine-learning-algorithms-python-scikit-learn

参考save-load-keras-deep-learning-models

参考a-gentle-introduction-to-scikit-learn-a-python-machine-learning-library

参考time-series-prediction-with-deep-learning-in-python-with-keras

参考ensemble-machine-learning-algorithms-python-scikit-learn

参考dropout-regularization-deep-learning-models-keras

参考using-learning-rate-schedules-deep-learning-models-python-keras

参考evaluate-performance-machine-learning-algorithms-python-using-resampling

参考evaluate-performance-deep-learning-models-keras

参考spot-check-classification-machine-learning-algorithms-python-scikit-learn

参考spot-check-regression-machine-learning-algorithms-python-scikit-learn

参考feature-selection-machine-learning-python

参考prepare-data-machine-learning-python-scikit-learn

参考image-augmentation-deep-learning-keras

参考machinelearningmastery.com/predict-sentiment-movie-reviews-using-deep-learning

参考metrics-evaluate-machine-learning-algorithms-python

参考visualize-machine-learning-data-python-pandas

参考understand-machine-learning-data-descriptive-statistics-python

参考https://www.datacamp.com/community/tutorials/deep-learning-python#gs.lxFHtvw

你可能感兴趣的:(keras,深度学习)