TensorFlow - 无法获得预测
TensorFlow - Unable to get Prediction
我正在尝试解决 Titanic Problem on Kaggle 但我不确定如何获取给定测试数据的输出。
我成功地训练了网络并调用了方法make_prediction(x, test_x)
x = tf.placeholder('float', [None, ip_features])
...
def make_prediction(x, test_data):
with tf.Session() as sess :
sess.run(tf.global_variables_initializer())
prediction = sess.run(y, feed_dict={x: test_data})
return prediction
我不确定如何在这种情况下传递 np.array
test_data
以取回包含预测 0/1
的 np.array
我将您的 train_neural_network
和 make_prediction
函数合并为一个函数。将tf.nn.softmax
应用到模型函数会使值范围从0~1(解释为概率),然后tf.argmax
提取概率较高的列号。请注意,在这种情况下,y
的 placeholder
需要进行单热编码。 (如果你不是在这里对 y 进行单热编码,那么 pred_y=tf.round(tf.nn.softmax(model))
会将 softmax
的输出转换为 0 或 1)
def train_neural_network_and_make_prediction(train_X, test_X):
model = neural_network_model(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(model, y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
pred_y=tf.argmax(tf.nn.softmax(model),1)
ephocs = 10
with tf.Session() as sess :
tf.initialize_all_variables().run()
for epoch in range(ephocs):
epoch_cost = 0
i = 0
while i< len(titanic_train) :
start = i
end = i+batch_size
batch_x = np.array( train_x[start:end] )
batch_y = np.array( train_y[start:end] )
_, c = sess.run( [optimizer, cost], feed_dict={x: batch_x, y: batch_y} )
epoch_cost += c
i+=batch_size
print("Epoch",epoch+1,"completed with a cost of", epoch_cost)
# make predictions on test data
predictions = pred_y.eval(feed_dict={x : test_X})
return predictions
我正在尝试解决 Titanic Problem on Kaggle 但我不确定如何获取给定测试数据的输出。
我成功地训练了网络并调用了方法make_prediction(x, test_x)
x = tf.placeholder('float', [None, ip_features])
...
def make_prediction(x, test_data):
with tf.Session() as sess :
sess.run(tf.global_variables_initializer())
prediction = sess.run(y, feed_dict={x: test_data})
return prediction
我不确定如何在这种情况下传递 np.array
test_data
以取回包含预测 0/1
np.array
我将您的 train_neural_network
和 make_prediction
函数合并为一个函数。将tf.nn.softmax
应用到模型函数会使值范围从0~1(解释为概率),然后tf.argmax
提取概率较高的列号。请注意,在这种情况下,y
的 placeholder
需要进行单热编码。 (如果你不是在这里对 y 进行单热编码,那么 pred_y=tf.round(tf.nn.softmax(model))
会将 softmax
的输出转换为 0 或 1)
def train_neural_network_and_make_prediction(train_X, test_X):
model = neural_network_model(x)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(model, y) )
optimizer = tf.train.AdamOptimizer().minimize(cost)
pred_y=tf.argmax(tf.nn.softmax(model),1)
ephocs = 10
with tf.Session() as sess :
tf.initialize_all_variables().run()
for epoch in range(ephocs):
epoch_cost = 0
i = 0
while i< len(titanic_train) :
start = i
end = i+batch_size
batch_x = np.array( train_x[start:end] )
batch_y = np.array( train_y[start:end] )
_, c = sess.run( [optimizer, cost], feed_dict={x: batch_x, y: batch_y} )
epoch_cost += c
i+=batch_size
print("Epoch",epoch+1,"completed with a cost of", epoch_cost)
# make predictions on test data
predictions = pred_y.eval(feed_dict={x : test_X})
return predictions