# # Written by: # -- # John L. Weatherwax 2009-04-21 # # email: wax@alum.mit.edu # # Please send comments and especially bug reports to the # above email address. # # EPage 770 # #----- sigmoid = function(x){ 1/(1+exp(-x)) } sigmoid_prime = function(x){ s = sigmoid(x) s * ( 1 - s ) } perceptron_build_model = function( X, y, W_0=NULL, alpha=0.01, max_error=1.e-3 ){ # # n_samples = dim(X)[1] # the number of samples n_features = dim(X)[2] # the number of features # initialize weights (add in bias which will be the last element) if( is.null(W_0) ){ W = rnorm( n_features+1 ) }else{ W = W_0 } W_t = W error = Inf while( error > max_error ){ for( ii in 1:n_samples ){ activation = sum( X[ii,] * W_t[1:n_features] ) + W_t[n_features+1] err = y[ii] - sigmoid(activation) W_t[1:n_features] = W_t[1:n_features] + alpha * err * sigmoid_prime(activation) * as.double(X[ii,]) W_t[n_features+1] = W_t[n_features+1] + alpha * err * sigmoid_prime(activation) } error = max(abs(W - W_t)) W = W_t } W } perceptron_predict = function( model, X ){ # # Make predictions based on the perceptron learning algorithm # n_samples = dim(X)[1] # the number of samples n_features = dim(X)[2] # the number of features predictions = c() for( ii in 1:n_samples ){ activation = sigmoid( sum( as.double(X[ii,]) * model[1:n_features] ) + model[n_features+1] ) predictions = c(predictions,activation) } predictions > 0.5 }