【发布时间】: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