patchSize = 8
visibleSize = patchSize * patchSize  # number of input units
hiddenSize = 25  # number of hidden units
sparsityParam = 0.01  # desired average activation of the hidden units.
lambdaParam = 0.0001  # weight decay parameter
betaParam = 3  # weight of sparsity penalty term


def sparseAutoencoderCostCallback(x):
    return sparse_autoencoder.cost(x, visibleSize, hiddenSize, lambdaParam,
                                   sparsityParam, betaParam, patches)


patches = getPatches(numPatches=10000, patchSize=patchSize)

thetaParam = sparse_autoencoder.initializeParameters(hiddenSize, visibleSize)

options = {
    'maxiter': 400,
    'disp': True,
}

result = optimize.minimize(sparseAutoencoderCostCallback,
                           thetaParam,
                           method='L-BFGS-B',
                           jac=True,
                           options=options)

W1 = result.x[0:hiddenSize * visibleSize].reshape(hiddenSize, visibleSize)

image_filename = 'images.png'
from visualize_network import visualizeNetwork
import sparse_autoencoder

patchSize=8
visibleSize = patchSize*patchSize		# number of input units 
hiddenSize = 25							# number of hidden units 
sparsityParam = 0.01					# desired average activation of the hidden units.
lambdaParam = 0.0001					# weight decay parameter       
betaParam = 3							# weight of sparsity penalty term

def sparseAutoencoderCostCallback(x):
	return sparse_autoencoder.cost(x, visibleSize, hiddenSize, lambdaParam, sparsityParam,
				betaParam, patches) 

patches = getPatches(numPatches=10000, patchSize=patchSize)

thetaParam = sparse_autoencoder.initializeParameters(hiddenSize, visibleSize)

options = {
		'maxiter': 400,
		'disp': True,
	}

result = optimize.minimize(sparseAutoencoderCostCallback, thetaParam, method='L-BFGS-B', jac=True, options=options)

W1 = result.x[0:hiddenSize*visibleSize].reshape(hiddenSize, visibleSize)

image_filename = 'images.png'
print 'Saving learned features to %s' % image_filename
visualizeNetwork(W1.T, image_filename)
# Train the first sparse autoencoder
options = {
		'maxiter': 400,
		'disp': True,
	}

sae1OptThetaFilename = results_dir + 'sae1OptTheta.npy'

if os.path.exists(sae1OptThetaFilename):
	sae1OptTheta = load(sae1OptThetaFilename)
else:
	def sparseAutoencoderCostCallbackL1(x):
		return sparse_autoencoder.cost(x, inputSize, hiddenSizeL1, lambdaParam, sparsityParam,
					betaParam, trainData, corruptionLevel)
	
	sae1Theta = sparse_autoencoder.initializeParameters(hiddenSizeL1, inputSize)
	result = optimize.minimize(sparseAutoencoderCostCallbackL1, sae1Theta, method='L-BFGS-B', jac=True, options=options)
	
	sae1OptTheta = result.x
	save(sae1OptThetaFilename, sae1OptTheta)
	
	W1 = sae1OptTheta[0:hiddenSizeL1*inputSize].reshape(hiddenSizeL1, inputSize)
	visualizeNetwork(W1.T, results_dir + 'sae1.png')

# Train the second sparse autoencoder
sae1Features = sparse_autoencoder.feedForward(sae1OptTheta, hiddenSizeL1, inputSize, trainData)

sae2OptThetaFilename = results_dir + 'sae2OptTheta.npy'

if os.path.exists(sae2OptThetaFilename):
	sae2OptTheta = load(sae2OptThetaFilename)