1

私は5列のデータセットを持っていますが、最初の3列は入力とし、残りの2列は出力として供給しています。Tensorflow APIデータをテストするため

私はプログラムを正常に実行しましたが、自分の値を入力として与え、モデルから予測される出力を得ることでモデルをテストする方法がわかりません。

誰でも助けてください。実際に訓練が終わったら、自分の価値でモデルを実際にテストできますか?

私はPythonでTensorflowを使用しています。私はテストの精度を表示することができていますが、私はいくつかのランダムな入力を渡すかどうかはどのように実際の値と予測します(ここでは、私は2つの出力値を取得するために3つの入力値を渡す必要があります)

ここに私のコードです:

# Implementation of a simple MLP network with one hidden layer. Tested on the iris data set. 
# Requires: numpy, sklearn>=0.18.1, tensorflow>=1.0 

# NOTE: In order to make the code simple, we rewrite x * W_1 + b_1 = x' * W_1' 
# where x' = [x | 1] and W_1' is the matrix W_1 appended with a new row with elements b_1's. 
# Similarly, for h * W_2 + b_2 
import tensorflow as tf 
import numpy as np 
from sklearn import datasets 
from sklearn.model_selection import train_test_split 
import pandas as pd 

RANDOM_SEED = 1000 
tf.set_random_seed(RANDOM_SEED) 


def init_weights(shape): 
    """ Weight initialization """ 
    weights = tf.random_normal(shape, stddev=0.1) 
    return tf.Variable(weights) 

def forwardprop(X, w_1, w_2): 
    """ 
    Forward-propagation. 
    IMPORTANT: yhat is not softmax since TensorFlow's softmax_cross_entropy_with_logits() does that internally. 
    """ 
    h = tf.nn.sigmoid(tf.matmul(X, w_1)) # The \sigma function 
    yhat = tf.matmul(h, w_2) # The \varphi function 
    return yhat 

def get_iris_data(): 
    """ Read the iris data set and split them into training and test sets """ 
    df = pd.read_csv("H:\MiniThessis\Sample.csv") 
    train_X = np.array(df[df.columns[0:3]]) 
    train_Y = np.array(df[df.columns[3:]]) 
    print(train_X) 

    # Convert into one-hot vectors 
    #num_labels = len(np.unique(train_Y)) 
    #all_Y = np.eye(num_labels)[train_Y] # One liner trick! 
    #print() 
    return train_test_split(train_X, train_Y, test_size=0.33, random_state=RANDOM_SEED) 

def main(): 
    train_X, test_X, train_y, test_y = get_iris_data() 

    # Layer's sizes 
    x_size = train_X.shape[1] # Number of input nodes: 4 features and 1 bias 
    h_size = 256    # Number of hidden nodes 
    y_size = train_y.shape[1] # Number of outcomes (3 iris flowers) 

    # Symbols 
    X = tf.placeholder("float", shape=[None, x_size]) 
    y = tf.placeholder("float", shape=[None, y_size]) 

    # Weight initializations 
    w_1 = init_weights((x_size, h_size)) 
    w_2 = init_weights((h_size, y_size)) 

    # Forward propagation 
    yhat = forwardprop(X, w_1, w_2) 
    predict = tf.argmax(yhat, axis=1) 

    # Backward propagation 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=yhat)) 
    updates = tf.train.GradientDescentOptimizer(0.01).minimize(cost) 

    # Run SGD 
    sess = tf.Session() 
    init = tf.global_variables_initializer() 
    sess.run(init) 

    for epoch in range(3): 
     # Train with each example 
     for i in range(len(train_X)): 
      sess.run(updates, feed_dict={X: train_X[i: i + 1], y: train_y[i: i + 1]}) 

     train_accuracy = np.mean(np.argmax(train_y, axis=1) == sess.run(predict, feed_dict={X: train_X, y: train_y})) 
     test_accuracy = np.mean(np.argmax(test_y, axis=1) ==sess.run(predict, feed_dict={X: test_X, y: test_y})) 

     print("Epoch = %d, train accuracy = %.2f%%, test accuracy = %.2f%%" 
       % (epoch + 1, 100. * train_accuracy, 100. * test_accuracy)) 


    correct_Prediction = tf.equal((tf.arg_max(predict,1)),(tf.arg_max(y,1))) 
    best = sess.run([predict], feed_dict={X: np.array([[20.14, 46.93, 1014.66]])}) 
    #print(correct_Prediction) 
    print(best) 


    sess.close() 

if __name__ == '__main__': 
    main() 

答えて

0

注:この回答には、元の質問では入力しなかった入力に関するいくつかの前提があります。

あなたの宿題にはもう少し作業が必要です。 4番目と5番目の列はおそらく花びらの幅(実際の値)とアイリスの種類(カテゴリとしてコード化されている)です。結果として、実際には価値ある出力(ペタルの幅)とカテゴリの予測が必要になることを意味します。つまり、あなたのsoftmax_cross_entry_with_logitsは花びら幅でうまく動かないでしょう。また、予測の一部としてargmaxを適用しています。この値は、最も高い値(この場合はペタルの幅または文字化してエンコードされた値)を持つインデックスも返します。だから、デバッグ支援としてで起動しない理由:

print(sess.run([yhat], feed_dict={X: np.array([[20.14, 46.93, 1014.66]])}) 

これは、入力[20.14, 46.93, 1014.66]に基づいて(、引数yhatを出力します(つまり、完全にデータセット内のものとは異なり、大規模な花だ)と2つの出力を含んでいます

+0

基本的に、私のデータは次の通りです:14.96,220,300,400,500 ...........すべての入力(最初の3つの列のi、e ,.値)出力(i、e、次の2列の値も数字です)ですので、基本的には3つの入力番号に対して2つの出力番号を出力する必要があります –

+0

最後の2つの列のデータは連続か離散ですか?両者の関係は? –

+0

最後の2つのデータカラム完全disceteで次のように私のデータは、次のとおり、V、AP列は\t V \t AP RH \t PE 14.96 \t 41.76 \t 1024.07 \t 73.17 \t 463.26 25.18 \t 62.96 \t 1020.04 \t 59.08 \t 444.37 5.11 AT、入力Aであります\t 39.4 \t 1012.16 \t 92.14 \t 488.56 20.86 \t 57.32 \t 1010.24 \t 76.64 \t 446.48 10.82 \t 37.5 66.43 \t 478.42 44.71 \t 1019.12 75.24 \t 467.35 9.48 \t 43.96 \t 1014.02 \t \t 58.77 \t 443.67 15.89 26.27 59.44 \t \t 1012.23 1009.\t 96.62 \t 473.9 。あなたの最初の質問の非常にオフトピックを行く64 1021.78 \t 41.25 \t 475.98 11.74 \t 43.56 \t 1015.14 \t 70.72 \t 477.5 –

関連する問題