-1

トレーニングデータセットを100%使用してニューラルネットワークを訓練しました。今私は元のデータセットに含まれていない新しいデータセットでネットワークをテストしたいと思います。新しいデータセットでTensorflowで訓練されたニューラルネットワークをテストする方法

from __future__ import absolute_import 
from __future__ import division 
from __future__ import print_function 
from scipy.io import loadmat 
%matplotlib inline 
import tensorflow as tf 
from tensorflow.contrib import learn 

import sklearn 
import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sns 
from warnings import filterwarnings 
filterwarnings('ignore') 
sns.set_style('white') 
from sklearn import datasets 
from sklearn.preprocessing import scale 
from sklearn.cross_validation import train_test_split 
from sklearn.datasets import make_moons 

X = np.array(loadmat("Data/DataIn.mat")['TrainingDataIn']) 
Y = np.array(loadmat("Data/DataOut.mat")['TrainingDataOut']) 

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=1, random_state=42) 
total_len = X_train.shape[0] 
# Parameters 
learning_rate = 0.001 
training_epochs = 2500 
batch_size = 100 
display_step = 1 
dropout_rate = 0.9 
# Network Parameters 
n_hidden_1 = 19 # 1st layer number of features 
n_hidden_2 = 26 # 2nd layer number of features 
n_input = X_train.shape[1] 
n_classes = 1 

# tf Graph input 
X = tf.placeholder("float32", [None, 37]) 
Y = tf.placeholder("float32", [None, 1]) 

def multilayer_perceptron(X, weights, biases): 
    # Hidden layer with RELU activation 
    layer_1 = tf.add(tf.matmul(X, weights['h1']), biases['b1']) 
    layer_1 = tf.nn.relu(layer_1) 

    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2']) 
    layer_2 = tf.nn.relu(layer_2) 

    # Output layer with linear activation 
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out'] 
    return out_layer 


# Store layers weight & bias 
weights = { 
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1], 0, 0.1)), 
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], 0, 0.1)), 
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes], 0, 0.1)) 
} 

biases = { 
    'b1': tf.Variable(tf.random_normal([n_hidden_1], 0, 0.1)), 
    'b2': tf.Variable(tf.random_normal([n_hidden_2], 0, 0.1)), 
    'out': tf.Variable(tf.random_normal([n_classes], 0, 0.1)) 
} 

# Construct model 
pred = multilayer_perceptron(X, weights, biases) 
tf.shape(pred) 
tf.shape(Y) 
print("Prediction matrix:", pred) 
print("Output matrix:", Y) 

# Define loss and optimizer 
cost = tf.reduce_mean(tf.square(pred-Y)) 
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) 

# Launch the graph 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 

    # Training cycle 
    for epoch in range(training_epochs): 
     avg_cost = 0. 
     total_batch = int(total_len/batch_size) 
     print(total_batch) 
     # Loop over all batches 
     for i in range(total_batch-1): 
      batch_x = X_train[i*batch_size:(i+1)*batch_size] 
      batch_y = Y_train[i*batch_size:(i+1)*batch_size] 
      # Run optimization op (backprop) and cost op (to get loss value) 
      _, c, p = sess.run([optimizer, cost, pred], feed_dict={X: batch_x, 
                  Y: batch_y}) 
      # Compute average loss 
      avg_cost += c/total_batch 

     # sample prediction 
     label_value = batch_y 
     estimate = p 
     err = label_value-estimate 
     print ("num batch:", total_batch) 
     print ("num training samples", total_len) 


     # Display logs per epoch step 
     if epoch % display_step == 0: 
      print ("Epoch:", '%04d' % (epoch+1), "cost=", \ 
       "{:.9f}".format(avg_cost)) 
      print ("[*]----------------------------") 
      for i in range(10): 
       print ("label value:", label_value[i], \ 
        "estimated value:", estimate[i]) 
      print ("[*]============================") 

    print ("Optimization Finished!") 

    # Test model 
    correct_prediction = tf.equal(tf.argmax(pred), tf.argmax(Y)) 
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
    print ("Accuracy:", accuracy.eval({X: X_test, Y: Y_test})) 

結果がここにいる私のコードはここに与えられている

... ...

Epoch: 2500 cost= 43.952847526 
[*]---------------------------- 
label value: [120] estimated value: [ 123.91127777] 
label value: [120] estimated value: [ 119.02476501] 
label value: [200] estimated value: [ 204.662323] 
label value: [120] estimated value: [ 124.79893494] 
label value: [60] estimated value: [ 62.79090881] 
label value: [20] estimated value: [ 18.09486198] 
label value: [200] estimated value: [ 203.56544495] 
label value: [20] estimated value: [ 17.48654938] 
label value: [20] estimated value: [ 21.10329819] 
label value: [60] estimated value: [ 60.81886673] 
[*]============================ 
Optimization Finished! 
Accuracy: 1.0 

あなたはすなわちtest_size = 1を使用、100%のデータで見ることができるように。私は新しいデータセットX_newとY_newを持っていると言うことができます、新しいデータセットをテストするために訓練されたモデルをどのように呼びますか?

+0

これまでに何を試しましたか?最後に与えられたコード行で 'X_test'と' Y_test'をX_newとY_newに置き換えることができます。 – B1T0

答えて

1

OK。 modleと変数の値を保存する必要があります:あなたのコンテンツを入れてください。あなたは 'osをインポートする'必要があります。

NN_name= <Name of model> 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    file_path = './' + NN_name + '/' 
    saver = tf.train.Saver() 
    saver.restore(sess, file_path+ 'model.checkpoint') 
    print('Model loaded') 
    <Sess run model accuracy on test dataset> 

注変数が保存され、負荷(他の特徴は缶)間で変更することはできませんモデルの構成:

NN_name= <Name of model> 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    <Train model> 
    file_path= './'+ NN_name + '/' 
    if not os.path.exists(file_path): 
     os.mkdir(file_path) 
    saver = tf.train.Saver() 
    saver.save(sess, file_path+ 'model.checkpoint') 
    print('Model saved') 

次に、テストをロードします。

前の返信: テストデータを入力して精度メトリックを再実行する必要があります。コードの追加の終わりに

_, c, p = sess.run([optimizer, cost, pred], feed_dict={X: X_new , 
                 Y: Y_new}) 
correct_prediction = tf.equal(tf.argmax(pred), tf.argmax(Y)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 
print ("Accuracy:", accuracy.eval({X: X_test, Y: Y_test})) 

それは計算グラフにまだあることを確認します(つまり、まだインデント。)。

パラメータの重みを保存する方法(https://www.tensorflow.org/programmers_guide/variables)を参照して、パラメータ/重みの値を保存し、グラフを閉じてテストやその他の予測のために読み込むことができます。

+0

@ James Shiztarご意見ありがとうございます。実際には、新しいデータセットX_newが与えられます。訓練されたモデルからこのデータセットから予測を行うにはどうすればいいですか? – Bright

0

一つの簡単なライン:

test_prediction = sess.run(pred, feed_dict={X: batch_test_x}) 

私はあなたが同じようにあなたのデータをロードし、それがbatch_test_xという変数を持っていることを前提としています。

動作するかどうか教えてください!

関連する問題