""" 'mlp_demo.py' Classify the XOR data pattern David Pan, UAH """ import numpy as np x1 = np.array([1, 1]) x2 = np.array([-1, -1]) x3 = np.array([-1, 1]) x4 = np.array([1, -1]) X = np.vstack([x1, x2, x3, x4]) # Features are along the row y1 = np.array([1, 0]) y2 = np.array([1, 0]) y3 = np.array([0, 1]) y4 = np.array([0, 1]) y = np.vstack([y1, y2, y3, y4]) from sklearn.neural_network import MLPClassifier clf = MLPClassifier( hidden_layer_sizes = (2), activation='logistic', random_state= 100, alpha = 0.001, solver='lbfgs', max_iter=10000000 ) clf.fit(X, y) clf.n_layers_ clf.loss_ clf.predict(X) # Weight matrix for the hidden layer clf.coefs_[0] # Bias vector for the hidden layer clf.intercepts_[0] # Output layer weights and biases clf.coefs_[1] clf.intercepts_[1] # Verification of the final output Z2 = np.matmul(X,clf.coefs_[0]) + clf.intercepts_[0] A2 = 1/(1 + np.exp(-Z2)) Z3 = np.matmul(A2,clf.coefs_[1]) + clf.intercepts_[1] A3 = 1/(1 + np.exp(-Z3)) A3 clf.predict_proba(X)