Example #1
0
def testConvolution(conv_images,W,b,zca_white,patch_mean):
    """
    函数功能:测试卷积操作是否正常
    """
    # step1:检测卷积过程
    convolved_features = cnn.cnn_convolve(patch_dim, hidden_size, conv_images,
                        W, b, zca_white, patch_mean)
    for i in range(1000):
        # 随机选取图像上的起始位置
        feature_num = np.random.randint(0,hidden_size)
        image_num = np.random.randint(0,8)
        image_row = np.random.randint(0,image_dim - patch_dim + 1)
        image_col = np.random.randint(0,image_dim - patch_dim + 1)
        # 根据其实位置截取一块特征图像
        patch = conv_images[image_row:image_row + patch_dim,
                            image_col:image_col + patch_dim,
                            :,image_num]
        # 重构特征图像结构,消除通道维度
        patch = np.concatenate((patch[:,:,0].flatten(),patch[:,:,1].flatten(),patch[:,:,2].flatten()))
        patch = np.reshape(patch,(patch.size,1))
        # 零均值化
        patch = patch - np.tile(patch_mean, (patch.shape[1], 1)).transpose()
        # ZCA白化
        patch = np.dot(zca_white,patch)
        # 将特征图像映射到特征空间(隐藏层)
        W1 = W
        b1 = b.reshape(hidden_size,1)
        features = sparse_autoencoder.sigmoid(np.dot(W1,patch) + b1)
        # 检测卷积获得的特征值与编码器编码得到的特征值是否在误差允许范围内相等
        if abs(features[feature_num,0] - convolved_features[feature_num,image_num,image_row,image_col]) > 1:
            print('Convolved feature does not match activation from autoencoder')
            print('Feature Number      :', feature_num)
            print('Image Number        :', image_num)
            print('Image Row           :', image_row)
            print('Image Column        :', image_col)
            print('Convolved feature   :', convolved_features[feature_num, image_num, image_row, image_col])
            print('Sparse AE feature   :', features[feature_num, 0])
            sys.exit("Convolved feature does not match activation from autoencoder. Exiting...")
    print("Congratulations! Your convolution code passed the test.")
def predict(thetaParam, inputSize, hiddenSize, numClasses, netConfig, data):
	"""Takes a trained theta and a test data set, and returns the predicted labels for each example.
	
	Keyword arguments:
	thetaParam -- trained weights from the autoencoder
	inputSize -- the number of input units
	hiddenSize -- the number of hidden units *at the 2nd layer*
	numClasses -- the number of categories
	netConfig - configuration of the neural network
	data -- our matrix containing the training data as columns.  So, data[i,:] is the i-th training example.
	
	"""

	softmaxTheta = thetaParam[0:hiddenSize*numClasses].reshape(numClasses, hiddenSize)
	stack = params2stack(thetaParam[hiddenSize*numClasses:], netConfig)

	activation = data
	for layer in stack:
		activation = sparse_autoencoder.sigmoid(activation.dot(layer.W.T) + layer.b)
		
	h_data = exp(softmaxTheta.dot(activation.T))
	h_data = h_data / sum(h_data, 0)
	return argmax(h_data, axis=0)
def cost(thetaParam, inputSize, hiddenSize, numClasses, netConfig, lambdaParam, data, labels, corruptionLevel=0.0):
	"""Takes a trained softmaxTheta and a training data set with labels, and returns cost
	and gradient using a stacked autoencoder model. Used for finetuning.
	
	Keyword arguments:
	thetaParam -- trained weights from the autoencoder
	visibleSize -- the number of input units
	hiddenSize --  the number of hidden units *at the 2nd layer*
	numClasses --  the number of categories
	netConfig --   the network configuration of the stack
	lambdaParam -- the weight regularization penalty
	data -- our matrix containing the training data as columns.  So, data[i,:] is the i-th training example. 
	labels -- a vector containing labels, where labels[i] is the label for the i-th training example
	corruptionLevel -- how much of the input will get corrupted (denoising autoencoder)
	
	"""
	
	# We first extract the part which compute the softmax gradient
	softmaxTheta = thetaParam[0:hiddenSize*numClasses].reshape(numClasses, hiddenSize)
	stack = params2stack(thetaParam[hiddenSize*numClasses:], netConfig)
	
	m = data.shape[0]
	groundTruth = array(csc_matrix( (ones(m),(labels,range(m))), shape=(numClasses,m) ).todense())

	activation = data
	
	# Corrupt input data (so that denoising autoencoder can fix it)
	if corruptionLevel > 0.0:
		corruptionMatrix = random.binomial(1,1-corruptionLevel, size=activation.shape)
		activation = activation * corruptionMatrix

	# Forward propagation
	activations = []
	for layer in stack:
		activations.append(activation)
		activation = sparse_autoencoder.sigmoid(activation.dot(layer.W.T) + layer.b)

	# Back propagation
	M = softmaxTheta.dot(activation.T)
	M = M - amax(M, 0)
	h_data = exp(M)
	h_data = h_data / sum(h_data, 0)
	
	cost = -1.0/numClasses * sum(multiply(groundTruth, log(h_data))) + lambdaParam/2 * sum(softmaxTheta**2)
	softmaxThetaGrad = -1.0/numClasses * ((groundTruth - h_data).dot(activation)) + lambdaParam*softmaxTheta
	
	stackGrad = []
	delta = multiply(-(softmaxTheta.T.dot(groundTruth - h_data)), (activation * (1-activation)).T)
	idx = len(activations)
	while activations != []:
		activation = activations.pop()
		layer = Layer(idx)
		layer.W = (1.0/numClasses) * delta.dot(activation)
		layer.b = (1.0/numClasses) * sum(delta, 1)
		stackGrad.insert(0, layer)

		delta = multiply(stack[idx-1].W.T.dot(delta), (activation * (1-activation)).T)
		
		idx -= 1

	(params, netConfig) = stack2params(stackGrad)
	grad = concatenate([softmaxThetaGrad.ravel(), params])
	
	return (cost, grad)