此处描述的神经网络也是为图像分类而创建的,用于使用TensorFlow API简单计算1个神经元的隐藏层的梯度检查 .

下面的代码基于TensorFlow Test Gradient Check example的参考

def check_gradients(feed_dict, num_features, neurons , output_size, cost):
    batch = len(feed_dict[images_placeholder])
    e = 1e-4

    all_params = [
        images_placeholder, #shape = (?,1024) ? == batch
        tf.get_default_graph().get_tensor_by_name('Layer_Hidden/weights:0'), #shape=(1024, 1) 
        tf.get_default_graph().get_tensor_by_name('Layer_Hidden/biases:0'), #shape=(1,)
        tf.get_default_graph().get_tensor_by_name('Layer_Output/weights:0'), #shape=(1, 10)
        tf.get_default_graph().get_tensor_by_name('Layer_Output/biases:0'), #shape=(10,)
    ]

    print (all_params[0])
    param_sizes = [
        [batch, num_features], # [100, 1024]
        [num_features, neurons],  # [1024, 1]
        [neurons],  # [1]
        [neurons, output_size],  # [1,10]
        [output_size], # [10]
    ]

    for param_index in range(len(all_params)):
        diff = tf.test.compute_gradient_error(
            all_params[param_index], 
            param_sizes[param_index], 
            cost,
            [batch],
            delta=e,
            extra_feed_dict=feed_dict)
        print('level:', param_index, ', Gradient Error:', diff)

显示了一些重要的代码段代码,以提高对提议结构的理解:

images_placeholder = tf.placeholder(tf.float32, shape=[None, 1024])
 batch = 100

此计算的错误与 images_placeholder 形状有关,如上所述 .

InvalidArgumentError:重塑的输入是一个具有100个值的张量,但请求的形状有1 [[Node:gradients_1 / Loss / Add_grad / Reshape = Reshape [T = DT_FLOAT,Tshape = DT_INT32,_device =“/ job:localhost / replica :0 /任务:0 / cpu:0“](gradients_1 / Loss / Add_grad / Sum,gradients_1 / Loss / Add_grad / Shape)]]

我无法弄清楚出了什么问题,因为我在这里解决了这个问题 .

谢谢