python - 如何提取tensorflow模型的输出?

标签 python tensorflow machine-learning python-3.6

我一直在尝试像往常一样使用训练/测试数据训练模型。我能够得到我的准确性、成本 + 有效的准确性和成本。因此,我认为该模型正在运行,结果达到 85% 就足够了。

现在,在我处理完我的训练/测试数据后,我有一个 csv 文件,其数据类型和结构相同但没有一列(默认 - 指示客户是否支付或延迟)。我试图用模型预测这个值。我正在纠结如何插入这些数据并返回丢失的列。

问题部分:

这是我用于恢复和预测新数据的代码 -> (y_pred [5100x41])

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('my_test_model101.meta')
    print("Model found.")

    saver.restore(sess, tf.train.latest_checkpoint('./'))
    print("Model restored compl.")

    z = tf.placeholder(tf.float32, shape= (None,5100))

    y_pred= y_pred.as_matrix()

    output =sess.run(z,feed_dict={x: y_pred})
    print(output)

任何人都可以帮助我了解我在这里做错了什么吗?!!!

错误信息是:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_4' with dtype float and shape [?,5100]
     [[Node: Placeholder_4 = Placeholder[dtype=DT_FLOAT, shape=[?,5100], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

期待:

我的输入 [5100 x 41] 但最后一列的初始值为 Nan,我希望它具有应该为 0 或 1 的预测值。

查看经过训练的模型架构:

模型架构:

# Number of input nodes.
input_nodes = 41

# Multiplier maintains a fixed ratio of nodes between each layer.
mulitplier = 3

# Number of nodes in each hidden layer
hidden_nodes1 = 41
hidden_nodes2 = round(hidden_nodes1 * mulitplier)
hidden_nodes3 = round(hidden_nodes2 * mulitplier)

# Percent of nodes to keep during dropout.
pkeep = tf.placeholder(tf.float32)

# input
x = tf.placeholder(tf.float32, [None, input_nodes])

# layer 1
W1 = tf.Variable(tf.truncated_normal([input_nodes, hidden_nodes1], stddev = 0.15))
b1 = tf.Variable(tf.zeros([hidden_nodes1]))
y1 = tf.nn.sigmoid(tf.matmul(x, W1) + b1)

# layer 2
W2 = tf.Variable(tf.truncated_normal([hidden_nodes1, hidden_nodes2], stddev = 0.15))
b2 = tf.Variable(tf.zeros([hidden_nodes2]))
y2 = tf.nn.sigmoid(tf.matmul(y1, W2) + b2)

# layer 3
W3 = tf.Variable(tf.truncated_normal([hidden_nodes2, hidden_nodes3], stddev = 0.15))
b3 = tf.Variable(tf.zeros([hidden_nodes3]))
y3 = tf.nn.sigmoid(tf.matmul(y2, W3) + b3)
y3 = tf.nn.dropout(y3, pkeep)

# layer 4
W4 = tf.Variable(tf.truncated_normal([hidden_nodes3, 2], stddev = 0.15))
b4 = tf.Variable(tf.zeros([2]))
y4 = tf.nn.softmax(tf.matmul(y3, W4) + b4)

# output
y = y4
y_ = tf.placeholder(tf.float32, [None, 2])

构建模型后,我了解到您需要添加占位符来存储您要查找的内容。所以:

# Parameters
training_epochs = 5 # These proved to be enough to let the network learn
training_dropout = 0.9
display_step = 1 # 10
n_samples = y_train.shape[0]
batch_size = 2048
learning_rate = 0.001

# Cost function: Cross Entropy
cost = -tf.reduce_sum(y_ * tf.log(y))

# We will optimize our model via AdamOptimizer
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

# Correct prediction if the most likely value (default or non Default) from softmax equals the target value.
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

到目前为止一切正常,我保存了模型。我能够恢复这个模型(打印变量,所有的都在那里---所以恢复没问题)

最佳答案

占位符“z”中没有任何内容,也没有分配任何内容。因此,当您运行 session 时,无需执行任何操作,因为“z”不依赖于模型中的任何内容。我想你想要,

output =sess.run(y,feed_dict={x: y_pred})

因为 'y' 是输出张量。

话虽如此,我想您可能想多读一些关于 tensorflow 使用的流程图,以了解计算是如何发生的。目前,您似乎还没有完全理解占位符变量。

关于python - 如何提取tensorflow模型的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51330981/

相关文章:

python - 概括类方法,根据条件执行略有不同的操作

image-processing - 智能手机上的人体姿势估计/匹配

python - 在 tensorflow 中,如何在不实际训练的情况下评估神经元网络

java - 将新实例添加到集群器

machine-learning - 凝聚层次聚类技术

python - 检查两个二维 numpy 数组的公共(public)元素,无论是行还是列

python - 使用 python 下载大 zip 文件

python - 通过云功能访问 Google 表格 Python API 的身份验证问题

python - Tensorflow read_file() 什么都不做

python - 当 Variable 的第一个维度为 None 时使用 tf.unpack()