如何在 Tensorflow 中输出预测?

新手上路,请多包涵

我正在尝试将 Tensorflow DNN 用于 Kaggle 竞赛。数据是大约 100 列分类数据、29 列数值数据和 1 列用于输出。我所做的是使用 Scikit 的训练测试拆分函数将其分为 X 和 y 的训练和测试,其中 X 是每行的列表,没有“id”或需要预测的值,y 是需要被预测。然后我构建了模型,如下所示:

 import tensorflow as tf
import numpy as np
import time
import pickle
with open('pickle.pickle', 'rb') as f:
    trainX, trainy, testX, testy = pickle.load(f)
trainX = np.array(trainX)
trainy = np.array(trainy)
trainy = trainy.reshape(trainy.shape[0], 1)
testX = np.array(testX)
testy = np.array(testy)
print (trainX.shape)
print (trainy.shape)
testX = testX.reshape(testX.shape[0], 130)
testy = testy.reshape(testy.shape[0], 1)
print (testX.shape)
print (testy.shape)
n_nodes_hl1 = 256
n_nodes_hl2 = 256
n_nodes_hl3 = 256

n_classes = 1

batch_size = 100

# Matrix = h X w
X = tf.placeholder('float', [None, len(trainX[0])])
y = tf.placeholder('float')

def model(data):

    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([trainX.shape[1], n_nodes_hl1])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}

    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}

    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2, n_nodes_hl3])),
                      'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}

    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3, n_classes])),
                      'biases':tf.Variable(tf.random_normal([n_classes]))}

    # (input_data * weights) + biases

    l1 = tf.add(tf.matmul(data, hidden_1_layer['weights']), hidden_1_layer['biases'])
    l1 = tf.nn.sigmoid(l1)

    l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
    l2 = tf.nn.sigmoid(l2)

    l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
    l3 = tf.nn.sigmoid(l3)

    output = tf.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output

def train(x):

    pred = model(x)
    #loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(pred, y))
    loss = tf.reduce_mean(tf.square(pred - y))
    optimizer = tf.train.AdamOptimizer(0.01).minimize(loss)

    epochs = 1

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        print ('Beginning Training \n')
        for e in range(epochs):
            timeS = time.time()
            epoch_loss = 0

            i = 0
            while i < len(trainX):

                start = i
                end = i + batch_size
                batch_x = np.array(trainX[start:end])
                batch_y = np.array(trainy[start:end])

                _, c = sess.run([optimizer, loss], feed_dict = {x: batch_x, y: batch_y})
                epoch_loss += c
                i += batch_size
            done = time.time() - timeS
            print ('Epoch', e + 1, 'completed out of', epochs, 'loss:', epoch_loss, "\nTime:", done, 'seconds\n')
        correct = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1))
        acc = tf.reduce_mean(tf.cast(correct, 'float'))
        print("Accuracy:", acc.eval({x:testX, y:testy}))

train(X)

1 个时期的输出:

 Epoch 1 completed out of 1 loss: 1498498282.5
Time: 1.3765859603881836 seconds

Accuracy: 1.0

我确实意识到损失非常高,我使用 1 个 epoch 只是为了测试目的,是的,我知道我的代码很乱。但我只想打印出一个预测。我该怎么做?我知道我需要为 X 提供一个功能列表,但我只是不知道该怎么做。我也不太明白为什么我的准确度是 1.0,所以如果您对此有任何建议,或者有任何方法可以更改我的代码,我会很乐意听取任何想法。提前致谢

原文由 tgs266 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 363
1 个回答

要获得预测,您只需评估 pred ,这是定义模型输出的操作。

怎么做?与 pred.eval() 。但是你需要一个输入来评估它的预测,所以你必须提供一个 feed_dict 字典到 eval() 你想要处理的样本(或样本)。

生成的代码如下所示:

 predictions = pred.eval(feed_dict = {x:testX})

请注意这与 acc.eval({x:testX, y:testy}) 非常相似,因为想法是相同的。您有一个操作(在本例中为 acc )需要评估一些输入,您可以通过调用 acc.eval()sess.run(acc) 来评估它 feed_dict 具有必要的输入。

原文由 Guillem Cucurull 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进