卷积神经网络(Tensorflow)的预测函数中的问题

时间:2018-11-14 13:21:30

标签: python tensorflow deep-learning conv-neural-network prediction

在非卷积正向传播模型的情况下,以下预测功能可以很好地发挥作用,

def predict(X, parameters):

    W1 = tf.convert_to_tensor(parameters["W1"])
    b1 = tf.convert_to_tensor(parameters["b1"])
    W2 = tf.convert_to_tensor(parameters["W2"])
    b2 = tf.convert_to_tensor(parameters["b2"])
    W3 = tf.convert_to_tensor(parameters["W3"])
    b3 = tf.convert_to_tensor(parameters["b3"])

    params = {"W1": W1,
              "b1": b1,
              "W2": W2,
              "b2": b2,
              "W3": W3,
              "b3": b3}

    x = tf.placeholder("float", [12288, 1])

    z3 = forward_propagation_for_predict(x, params)
    p = tf.argmax(z3)

    sess = tf.Session()
    prediction = sess.run(p, feed_dict = {x: X})

    return prediction

def forward_propagation_for_predict(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']
    W3 = parameters['W3']
    b3 = parameters['b3'] 

    Z1 = tf.add(tf.matmul(W1, X), b1)                      
    A1 = tf.nn.relu(Z1)                                    
    Z2 = tf.add(tf.matmul(W2, A1), b2)                     
    A2 = tf.nn.relu(Z2)                                    
    Z3 = tf.add(tf.matmul(W3, A2), b3)                     

    return Z3

我尝试更改卷积forward_propagation的代码,并在更改后应用了

def predict(X, parameters):
    X = np.reshape(X,(1,100,100,3)) 
    W1 = tf.convert_to_tensor(parameters["W1"])
    W2 = tf.convert_to_tensor(parameters["W2"])


    params = {"W1": W1,
              "W2": W2,
              }

    x = tf.placeholder("float", [1,100,100,3])

    z3 = forward_propagation_for_predict(x, params)
    p = tf.argmax(z3)
    init = tf.global_variables_initializer() 
    sess = tf.Session()
    sess.run(init)                           
    prediction = sess.run(p, feed_dict = {x: X})
    return prediction

def forward_propagation_for_predict(X, parameters): #altered implementation for convolutional NN
    # Retrieve the parameters from the dictionary "parameters" 
    W1 = parameters['W1']
    W2 = parameters['W2']


    Z1 = tf.nn.conv2d(X, W1, strides = [1,1,1,1], padding = 'SAME')
    # RELU
    A1 = tf.nn.relu(Z1)
    # MAXPOOL: window 8x8, sride 8, padding 'SAME'
    P1 = tf.nn.max_pool(A1, ksize = [1,8,8,1], strides = [1,8,8,1], padding = 'SAME')
    # CONV2D: filters W2, stride 1, padding 'SAME'
    Z2 = tf.nn.conv2d(P1, W2, strides = [1,1,1,1], padding = 'SAME')
    # RELU
    A2 = tf.nn.relu(Z2)
    # MAXPOOL: window 4x4, stride 4, padding 'SAME'
    P2 = tf.nn.max_pool(A2, ksize = [1,4,4,1], strides = [1,4,4,1], padding = 'SAME')
    # FLATTEN
    P2 = tf.contrib.layers.flatten(P2)
    # FULLY-CONNECTED without non-linear activation function (not not call softmax).
    Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn=None)

    return Z3

我有以下疑问/问题,

  1. 更改后的实现需要初始化tf参数,而在原始实现中则不需要初始化。(如果tf参数必须在使用前进行初始化,那么它如何工作原始实现)

  2. 初始化参数后,响应为全零而不是一热的向量。 (例如,array([0, 0, 0, 0, 0, 0], dtype=int64)

注意:我没有使用偏见,因此我删除了这些参数。

0 个答案:

没有答案