python-3.x - 如何在 Tensorflow 中输出预测?

标签 python-3.x neural-network tensorflow

我正在尝试将 Tensorflow DNN 用于 Kaggle Competion 。该数据大约有 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,所以如果您对此有任何建议,或者有任何更改我的代码的方法,我会更乐意听取任何想法。 提前致谢

最佳答案

要获得预测,您只需评估 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以及必要的输入。

关于python-3.x - 如何在 Tensorflow 中输出预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40002084/

相关文章:

python - 模块代码运行不正确

python - 如何在 Outlook (2010) 全局地址列表中搜索多个姓名?

python - 按字符拆分泰语文本

neural-network - 使用预训练的词嵌入 - 如何为未知/OOV token 创建向量?

neural-network - 对于YOLO损失函数,为了得到项1objij的值,进行了什么计算?

tensorflow - PyCaret ValueError - 单元格为空

python-3.x - bool pandas.Series 之间的操作对称性不等,索引不等

python - 值错误 : at least one array or dtype is required

tensorflow - 如何恢复 LSTM 层

python - Tensorflow:将灰度图像转换为RGB