0

Ich möchte Regressionsproblem über Tensorflow lösen. Ich möchte Bild von Katze/Hund vorherzusagen.Python, Tensorflow Regression

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) 

und Ausgang ist immer das gleiche:

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

Meine Nettoerträge nur dieser Wert. Ist es möglich, nnet richtig zu lernen?

Wenn es eine Katze nnet 1
zurückkehren sollte Wenn ein Hund nnet ist zurückkehren sollte 0
Ist es möglich?

+0

Wie trainieren Sie Ihr Netzwerk? Welchen Datensatz verwenden Sie für das Training? – keveman

+0

Wo ist Ihr Optimierer, Sitzung oder Ihre "y_conv". Veröffentlichen Sie relevante Teile des Codes, wenn Sie nach Antworten suchen! – shekkizh

+0

Wäre das nicht besser als Klassifizierungsproblem und nicht als Regressionsproblem geeignet? – ode2k

Antwort

0

Nachdem Sie alle x (img) Daten zu erhalten, müssen Sie Kosten und Zugbetrieb definieren:

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 

Dann ist es bereit, das Modell mit Ihrem x Eingänge zu trainieren:

# 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 

Siehe das vollständige Codebeispiel unter https://github.com/nlintz/TensorFlow-Tutorials/blob/master/2_logistic_regression.py.

0

Es ist meine Schuld, ich habe genau diesen Code, ich will nicht alle nutzlos Code einzufügen zu tun, dass

"""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

In Beispiel https://github.com/nlintz/TensorFlow-Tutorials/blob/master/2_logistic_regression.py warum ist 'Y = tf.placeholder ("float", [None, 10])' I möchte skalar in der Ausgabe vom neuronalen Netzwerk bekommen? –

Verwandte Themen