0

我想通過張量流解決迴歸問題。 我想預測貓/狗的圖片。Python,Tensorflow迴歸

x = tf.placeholder(tf.float32, shape=[512, 512]) #this is input nnet 
y_ = tf.placeholder(tf.float32, shape=[1, 1]) #this is output nnet 


for i in range(24): 
    img = get_image_from_file("./MOJE/koty_nauka/kot" + str(i + 1) + 
           ".jpg") 
    out = y_conv.eval(feed_dict={ 
     x: img, y_: [[1]], keep_prob: 1.0}) 
    print("----") 
    print(out) 

和輸出始終是相同的:

---- 
[[ 1.] 
[ 1.] 
[ 1.] 
[ 1.] 
[ 1.] 
[ 1.] 
[ 1.] 
[ 1.]] 

我的淨回報率只有這個值。是否可以正確學習nnet?

如果有一隻貓NNET應該返回1
如果有狗NNET應該返回0
這可能嗎?

+0

你是如何訓練自己的網絡?你使用什麼數據集進行培訓? – keveman

+0

你的優化器,會話或你的「y_conv」在哪裏。發佈相關部分的代碼,如果你正在尋找答案! – shekkizh

+0

這不是更適合作爲分類問題而不是迴歸問題嗎? – ode2k

回答

0

一旦你得到所有的X(IMG)的數據,您需要定義成本和一條龍作業:

w = init_weights([784, 10]) # #of x vec and # of labels 
py_x = tf.matmul(X, w) 

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(py_x, Y)) # compute mean cross entropy (softmax is applied internally) 
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # construct optimizer 
predict_op = tf.argmax(py_x, 1) # at predict time, evaluate the argmax of the logistic regression 

然後,它已準備好使用你的X投入到訓練模型:

# Launch the graph in a session 
with tf.Session() as sess: 
    # you need to initialize all variables 
    tf.initialize_all_variables().run() 

    for i in range(100): 
     sess.run(train_op, feed_dict={X: x, Y: y) # feed your x data and label 

請參閱https://github.com/nlintz/TensorFlow-Tutorials/blob/master/2_logistic_regression.py的完整代碼示例。

0

這是我的錯,我有整整這段代碼,我不想粘貼所有無用的代碼做

"""Import.""" 
import tensorflow as tf 
import cv2 

sess = tf.InteractiveSession() 
x = tf.placeholder(tf.float32, shape=[512, 512]) 
y_ = tf.placeholder(tf.float32, shape=[1, 1]) 


def get_image_from_file(file_name): 
    """Function get_image_from_file.""" 
    return cv2.resize(cv2.imread(file_name, 0), (512, 512), 
         interpolation=cv2.INTER_CUBIC) 


def weight_variable(shape): 
    """Foo.""" 
    initial = tf.truncated_normal(shape, stddev=0.1) 
    return tf.Variable(initial) 


def bias_variable(shape): 
    """Foo.""" 
    initial = tf.constant(0.1, shape=shape) 
    return tf.Variable(initial) 


def conv2d(x, w): 
    """Foo.""" 
    return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') 


def max_pool_2x2(x): 
    """Max pool 2x2.""" 
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], 
          strides=[1, 2, 2, 1], padding='SAME') 


if __name__ == "__main__": 
    # 1st layer 
    w_conv1 = weight_variable([5, 5, 1, 16]) 
    b_conv1 = bias_variable([16]) 

    x_image = tf.reshape(x, [-1, 512, 512, 1]) 

    h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1) 
    h_pool1 = max_pool_2x2(h_conv1) 

    # 2nd layer 
    w_conv2 = weight_variable([5, 5, 16, 32]) 
    b_conv2 = bias_variable([32]) 

    h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2) 
    h_pool2 = max_pool_2x2(h_conv2) 

    # connection layer 
    w_fc1 = weight_variable([32 * 32 * 64, 128]) 
    b_fc1 = bias_variable([128]) 

    h_pool2_flat = tf.reshape(h_pool2, [-1, 32 * 32 * 64]) 
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1) 

    # zapobieganie przeuczeniu 
    keep_prob = tf.placeholder(tf.float32) 
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) 

    # output layer 
    w_fc2 = weight_variable([128, 1]) 
    b_fc2 = bias_variable([1]) 

    y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2) 
    cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) 
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) 
    correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
    sess.run(tf.initialize_all_variables()) 
    for i in range(24): 
     img = get_image_from_file("./MOJE/koty_nauka/kot" + str(i + 1) + 
            ".jpg") 

     out = y_conv.eval(feed_dict={ 
      x: img, y_: [[1]], keep_prob: 1.0}) 
     print("----") 
     print(out) 
+0

在示例https://github.com/nlintz/TensorFlow-Tutorials/blob/master/2_logistic_regression.py 爲什麼是 'Y = tf.placeholder( 「浮動」,[無,10])' 我想從神經網絡的輸出中得到標量嗎? –