【问题标题】:Convert classification to regression in tensorflow在张量流中将分类转换为回归
【发布时间】:2018-09-30 11:23:17
【问题描述】:

我可以使用此代码执行分类。它输出每个输出标签的概率。但我需要转换它以便它可以预测值。也就是说,我想在最后添加一个回归层而不是softmax。我怎样才能做到这一点?例如,我为标签 1、2、3、4、5 训练了模型。但我希望模型能够预测这 5 个标签之外的值。例如,给定输入,模型可能预测 1.3 或 2.5 等。我想要连续输出而不是离散输出。

更新

我正在尝试从这个问题中获得建议的解决方案 Here

假设我有一个训练数据。我针对 1、2、3、4、5 度等整数温度训练模型。基本上,这些输出温度是标签。如何预测介于两个温度(如 2.5 度)之间的值。不可能针对每个温度值进行训练。我怎样才能做到这一点?

我的模型给出了每个类别的预测概率

Temp  Probability   
1  .01
2  .05
3  .56
4  .24
5  .14

我希望我的模型预测温度值,例如 1.2、2.7 等,而不是预测每个类别的概率。

input_height = 1 # 1-Dimensional convulotion
input_width = 90 #window
num_labels = 5 #output labels
num_channels = 8 #input columns

batch_size = 10
kernel_size = 60
depth = 60
num_hidden = 1000

learning_rate = 0.0001
training_epochs = 8

total_batches = train_x.shape[0] # batch_size

X = tf.placeholder(tf.float32, shape=[None,input_height,input_width,num_channels],name="input")
# X = tf.placeholder(tf.float32, shape=[None,input_width * num_channels], name="input")
# X_reshaped = tf.reshape(X,[-1,1,90,3])
Y = tf.placeholder(tf.float32, shape=[None,num_labels])

c = apply_depthwise_conv(X,kernel_size,num_channels,depth)
p = apply_max_pool(c,20,2)
c = apply_depthwise_conv(p,6,depth*num_channels,depth//10)

shape = c.get_shape().as_list()
c_flat = tf.reshape(c, [-1, shape[1] * shape[2] * shape[3]])

f_weights_l1 = weight_variable([shape[1] * shape[2] * depth * num_channels * (depth//10), num_hidden])
f_biases_l1 = bias_variable([num_hidden])
f = tf.nn.tanh(tf.add(tf.matmul(c_flat, f_weights_l1),f_biases_l1))

out_weights = weight_variable([num_hidden, num_labels])
out_biases = bias_variable([num_labels])
y_ = tf.nn.softmax(tf.matmul(f, out_weights) + out_biases,name="y_")

loss = -tf.reduce_sum(Y * tf.log(y_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(loss)

correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1)) #difference between correct output and expected output
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

cost_history = np.empty(shape=[1], dtype=float)


with tf.Session() as session:
    tf.global_variables_initializer().run()
    for epoch in range(training_epochs):
        for b in range(total_batches):
            offset = (b * batch_size) % (train_y.shape[0] - batch_size)
            batch_x = train_x[offset:(offset + batch_size), :, :, :]
            batch_y = train_y[offset:(offset + batch_size), :]
            _, c = session.run([optimizer, loss], feed_dict={X: batch_x, Y: batch_y})
            cost_history = np.append(cost_history, c)
        print "Epoch: ", epoch, " Training Loss: ", c, " Training Accuracy: ",session.run(accuracy, feed_dict={X: train_x, Y: train_y})
        print "Testing Accuracy:", session.run(accuracy, feed_dict={X: test_x, Y: test_y})

【问题讨论】:

  • 如果这是一个愚蠢的问题,我很抱歉。如果tf.matmul(f, out_weights) + out_biases 对你来说不够好,你在寻找什么类型的层(密集层)?
  • 代替softmax,我想添加一个回归层
  • tf.matmul(f, out_weights) + out_biases 不是可接受的回归层吗?
  • 我不确定。我需要一个答案。我希望我的模型能够预测超出给定标签的输出。如果您认为您的答案是正确的,请告诉我如何更新我的代码以添加回归

标签: python tensorflow deep-learning regression


【解决方案1】:

如果您想预测检测到哪个类,只需在输出上执行 arg_max。概率最高的是检测到的类别。

predict = tf.argmax(y_)

【讨论】:

  • 假设我训练了标签 1、2、3、4、5 的模型。但我希望模型能够预测这 5 个标签之外的值。例 1.3。我已经更新了我的问题
  • 你有数据吗?你能训练你的网络来预测这些值吗?如果不是您要查找的内容,如果您学习的类是 1、2、3、4、5,为什么您的网络应该输出 1.3 之类的值?您必须了解您的网络无法使用回归层神奇地输出值。就像 Y. Luo 说的,一个简单的线性回归是tf.matmul(f, out_weights) + out_biases。但是你需要提供样本来训练这一层。如果没有,您可以进行权重总和或您的概率和类别。但我不知道你想达到什么目的。
  • 我在这里stackoverflow.com/questions/49699964/…问了一个类似的问题。我得到了添加回归层的建议。
  • 您能回复一下吗?
  • 首先,我不为您服务,所以不需要回答。其次,您似乎只是不知道什么是回归,什么是分类。机器学习不是魔术,它可以回答非常精确的一类问题,并且通过了解不同技术(如分类和回归)之间的差异,可以帮助您选择使用什么。没有关于您要达到的目标、您的数据是什么等信息......因此,我们无法为您提供帮助。
猜你喜欢
  • 2022-09-29
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多