# Two-layered neural network model
# for learning logic judgments

# Set up input as a matrix of 4 stimuli consisting of 3 features
# the last feature is bias node
input<-matrix(c(1,1,-1,
                1,-1,-1,
                -1,1,-1,
                -1,-1,-1),4,3,byrow=T)

input<-matrix(c(1,1,1,-1,
                1,-1,-1,-1,
                -1,1,-1,-1,
                -1,-1,1,-1),4,4,byrow=T)



# Set up target as a vector of 4 output values
# in this case only when f1 and f2 both are 1 the target is 1,
# namely AND operation
target<-matrix(c(1,-1,-1,-1),1,4)
target<-matrix(c(1,1,1,-1),1,4)
target<-matrix(c(-1,1,1,-1),1,4)

# Set up associative weights
W<-matrix(0,1,3)
W<-matrix(0,1,4)

# Set up learning rate & number of iteration
eta<-0.01
NIt<-10

# Declare error vector in iterations
E<-rep(-1,NIt)

# Training iteration
for(i in 1:NIt){
  # Generate output
  output<-W%*%t(input)
  h<-ifelse(output>0,1,-1)
  # Compute error
  error<-target-h
  E[i]<-0.5*sum(error^2)
  # Update associative weights
  dW<--eta*(error%*%input)
  W<-W-dW
}

# Plot error curve
plot(1:NIt,E,type='l')
output
h
W
target
