#######  Single Hidden Layer ANN

from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense

dataset = loadtxt('labeled.csv', delimiter=',')

# split into input (X) and output (y) variables
X = dataset[:, 0:16]
y = dataset[:, 16]

# Define the keras model
# Default activator: linear
model = Sequential()
model.add(Dense(8, input_dim=16))
model.add(Dense(4))
model.add(Dense(1))

# compile the keras model
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# fit the keras model on the dataset
# model.fit(X, y, epochs=100, batch_size=25)
model.fit(X, y, epochs=100, batch_size=32)

# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy * 100), '%')
print("Training Accuracy :", model.score(x_train, y_train))
print("Testing Accuracy :", model.score(x_test, y_test))

cm = confusion_matrix(y_test, y_pred)
print('cm:', cm)

# Artificial Neural Networks
import keras
from keras.models import Sequential
from keras.layers import Dense
# creating the model
model = Sequential()

# first hidden layer
model.add(Dense(output_dim=8, init='uniform', activation='relu', input_dim=14))

# second hidden layer
model.add(Dense(output_dim=8, init='uniform', activation='relu'))

# third hidden layer
model.add(Dense(output_dim=8, init='uniform', activation='relu'))

# fourth hidden layer
model.add(Dense(output_dim=8, init='uniform', activation='relu'))

# fifth hidden layer
model.add(Dense(output_dim=8, init='uniform', activation='relu'))

# output layer
model.add(Dense(output_dim=1, init='uniform', activation='sigmoid'))
Beispiel #3
0
results

# Mo hinh mang neural

# Them thu vien keras va cac goi

import keras
from keras.models import Sequential
from keras.layers import Dense

# Khoi tao mang luoi

classifier = Sequential()
classifier.add(
    Dense(units=15,
          kernel_initializer='uniform',
          activation='relu',
          input_dim=29))
classifier.add(Dense(units=15, kernel_initializer='uniform',
                     activation='relu'))
classifier.add(
    Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))
classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])

# Phu hop mang luoi vao tap train

classifier.fit(X_train, y_train, batch_size=32, epochs=100)

# Du doan ket qua tap thu
Beispiel #4
0
# 8.1 MLP
from sklearn.neural_network import MLPClassifier
classifier = MLPClassifier(hidden_layer_sizes=[10],
                           solver='lbfgs',
                           random_state=0).fit(X_train_scaled, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test_scaled)

# 9.1 ANN
# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense
# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(
    Dense(output_dim=6, init='uniform', activation='relu', input_dim=11))
# Adding the second hidden layer
classifier.add(Dense(output_dim=6, init='uniform', activation='relu'))
# Adding the output layer
classifier.add(Dense(output_dim=1, init='uniform', activation='sigmoid'))
# Compiling the ANN
classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size=10, nb_epoch=100)
# Predicting the Test set results
y_pred = classifier.predict(X_test_scaled)