如何使用新的数据集在Tensorflow中测试经过训练的神经网络

问题描述:

我已经训练了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 

正如你可以使用100%的数据,即test_size = 1看看。比方说,我有一个新的数据集X_new和Y_new,我该如何调用训练好的模型来测试新的数据集?

+0

到目前为止您尝试过什么?你可以尝试在你最后给出的代码行中用X_new和Y_new来代替'X_test'和'Y_test'。 – B1T0

好的。您需要通过以下方式保存模型和变量值:放入内容。你需要'导入os'。

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') 

然后加载和测试:

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> 

注意模型变量的配置不能改变的保存和加载(其它特征)。

上一个回答: 您需要提供测试数据并重新运行准确度指标。

在添加代码的末尾:

_, 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),其将使得能够节省paramters /权重值,关闭图形并加载用于测试或任何其它预测。

+0

@ James Shiztar感谢您的评论。我实际上的意思是给定一个新的数据集X_new,我如何根据我的训练模型中的数据集做出预测? – Bright

一个简单的一行:

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

我假设你加载数据以同样的方式,它有一个名为batch_test_x变量。

让我知道它是否有效!