回归的Tensorflow多维输出(预测)

时间:2018-12-11 20:55:42

标签: python-3.x tensorflow neural-network deep-learning

在给定一个输入数组的情况下,我无法解决如何返回多个预测的问题。想象一下用给定输入数组来预测xy坐标数组的用例。我创建了一个简单的脚本来确切说明我要实现的目标(请注意注释):

from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf

input = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
output_1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
output_2 = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])

learning_rate = 0.001
training_epochs = 10000

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

weights = {
    "w1": tf.Variable(tf.random_normal([10], 0, 0.1), tf.float32),
    "w2": tf.Variable(tf.random_normal([10], 0, 0.1), tf.float32),
    "out": tf.Variable(tf.random_normal([10], 0, 0.1), tf.float32)
}

biases = {
    "b1": tf.Variable(tf.random_normal([10], 0, 0.1), tf.float32),
    "b2": tf.Variable(tf.random_normal([10], 0, 0.1), tf.float32),
    "out": tf.Variable(tf.random_normal([10], 0, 0.1), tf.float32)
}

layer_1_tensor_1 = tf.nn.tanh(tf.add(tf.multiply(weights["w1"], x), biases["b1"]))
layer_2_tensor_1 = tf.nn.tanh(tf.add(tf.multiply(weights["w2"], layer_1_tensor_1), biases["b2"]))
layer_out = tf.add(tf.multiply(weights["out"], layer_2_tensor_1), biases["out"])

prediction = layer_out
loss = tf.reduce_mean(tf.square(prediction - y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate)
train = optimizer.minimize(loss)

init = tf.global_variables_initializer()

tf.summary.scalar("loss", loss)
with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        feed_dict = {
            x: input,
            y: output_1
        }
        _, l  = sess.run([train, loss], feed_dict)
        print('Step %i: Loss: %f' % (epoch, l))
    fig = plt.figure()
    ax = Axes3D(fig)
    # Should look like this scatter plot:
    ax.scatter(output_1, output_2, input, marker='.')
    predicted = sess.run([prediction], feed_dict={x: input})
    # I want to anchieve something like: ax.scatter(predicted[0], predicted[1], input, marker='o').
    # Currently my y axis is unknown, thus np.zeros(10).
    ax.scatter(predicted, np.zeros(10), input, marker='o')
    plt.show()

我能够预测output_1output_2,但不能两者都预测。如何正确执行?我尝试摆弄形状,但遇到许多错误,并使用了两个输出张量,这也是不成功的尝试。

注意:我不是在尝试建立最佳模型。我对如何返回二维预测的技术细节感兴趣。至少到文章或类似问题/问题的某些链接会很有帮助。

0 个答案:

没有答案