【问题标题】:Python, Tensorflow RegressionPython,张量流回归
【发布时间】:2016-05-05 15:22:29
【问题描述】:

我想通过 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
有可能吗?

【问题讨论】:

  • 你是如何训练你的网络的?您使用什么数据集进行训练?
  • 优化器、会话或“y_conv”在哪里。如果您正在寻找答案,请发布代码的相关部分!
  • 这不是更适合作为分类问题而不是回归问题吗?
  • 尝试从一个在不同数据集上进行训练的工作示例开始。

标签: python neural-network regression tensorflow image-recognition


【解决方案1】:

获得所有 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 上的完整代码示例。

【讨论】:

    【解决方案2】:

    这是我的错,我有这个代码,我不想粘贴所有无用的代码来做那个

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

    【讨论】:

    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 2018-06-24
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2017-08-27
    • 2023-03-15
    相关资源
    最近更新 更多