あなたの機能train_neural_network
とmake_prediction
機能を 1 つの機能にまとめました。モデル関数に適用するtf.nn.softmax
と、値の範囲が 0 ~ 1 (確率として解釈) になり、tf.argmax
確率が高い列番号が抽出されます。placeholder
この場合のfory
は、ワンホット エンコードする必要があることに注意してください。(ここで 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