0% found this document useful (0 votes)
30 views

MLP Sous Keras: A. MLP Pour Une Classification Binaire

This document discusses using multilayer perceptrons (MLPs) for classification and regression with Keras. For classification, it shows code to create an MLP model for binary classification on dummy data with multiple dense layers, dropout regularization, and sigmoid activation. For regression, it loads housing price data and defines a baseline MLP model to predict prices with dense and linear activation layers, evaluating it with cross-validation. It provides questions about the role of dropout and changing hyperparameters, and links to additional resources for continuing the regression example.

Uploaded by

Soufiane Biggi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

MLP Sous Keras: A. MLP Pour Une Classification Binaire

This document discusses using multilayer perceptrons (MLPs) for classification and regression with Keras. For classification, it shows code to create an MLP model for binary classification on dummy data with multiple dense layers, dropout regularization, and sigmoid activation. For regression, it loads housing price data and defines a baseline MLP model to predict prices with dense and linear activation layers, evaluating it with cross-validation. It provides questions about the role of dropout and changing hyperparameters, and links to additional resources for continuing the regression example.

Uploaded by

Soufiane Biggi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

INE2 SmartICT TP3 Deep Learning : MLP sous keras

MLP sous keras


A. MLP pour une classification binaire :
import numpy as np

from keras.models import Sequential

from keras.layers import Dense, Dropout

# Generate dummy data

x_train = np.random.random((1000, 20))

y_train = np.random.randint(2, size=(1000, 1))

x_test = np.random.random((100, 20))

y_test = np.random.randint(2, size=(100, 1))

model = Sequential()

model.add(Dense(64, input_dim=20, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(64, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',

optimizer='rmsprop',

metrics=['accuracy'])

model.fit(x_train, y_train,

epochs=20,

batch_size=128)

score = model.evaluate(x_test, y_test, batch_size=128)

Questions :

- Quel est le rôle de l’ajout de : model.add(Dropout(..))

- Variez le nombre des epochs et interprétez les résultats.

- Changez l’optimizer et interprétez les résultats.

B. MLP pour une regression pour la prédiction des prix des maison

from pandas import read_csv

E. Ibn Elhaj 2019-2020 1


INE2 SmartICT TP3 Deep Learning : MLP sous keras

from keras.models import Sequential

from keras.layers import Dense

from keras.wrappers.scikit_learn import KerasRegressor

from sklearn.model_selection import cross_val_score

from sklearn.model_selection import KFold

# load dataset

dataframe = read_csv("housing.csv", delim_whitespace=True, header=None)

dataset = dataframe.values

# split into input (X) and output (Y) variables

X = dataset[:,0:13]

Y = dataset[:,13]

# define base model

def baseline_model():

# create model

model = Sequential()

model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu'))

model.add(Dense(1, kernel_initializer='normal'))

# Compile model

model.compile(loss='mean_squared_error', optimizer='adam')

return model

# evaluate model

estimator = KerasRegressor(build_fn=baseline_model, epochs=100, batch_size=5, verbose=0)

kfold = KFold(n_splits=10)

results = cross_val_score(estimator, X, Y, cv=kfold)

print("Baseline: %.2f (%.2f) MSE" % (results.mean(), results.std()))

Télécharger datasource et faire la suite du travail en utilisant le lien suivant :

https://www.machinecurve.com/index.php/2019/07/30/creating-an-mlp-for-regression-with-keras/

E. Ibn Elhaj 2019-2020 2

You might also like