我有一個包含5列的數據集,我將前3列作爲輸入,其他2列作爲輸出。Tensorflow API用於測試數據
我已經成功執行了該程序,但我不確定如何通過將我自己的值作爲輸入並從模型中獲取預測輸出來測試模型。
任何人都可以請幫助我,我怎麼能在訓練完成後用我自己的價值測試模型?
我在Python中使用Tensorflow。我能夠顯示測試的準確度,但實際上,我怎麼用值預測,如果我通過一些隨機輸入(在這裏,我需要通過3個輸入值,獲得2個輸出值)
這裏是我的代碼:
# 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()
基本上,我的數據如下:14.96,220,300,400,500 ...........所有的輸入(前3列中的i,e值是數字)以及所有的輸入輸出(i,e,下兩列中的值也是數字)因此,基本上,我需要輸出給定的3個輸入數字的兩個輸出數字 –
並且是最後兩列中的數據是連續的還是離散的?兩者之間的關係? –
最後2位的數據列是完全discete:我的數據如下:A,V,AP列是輸入,AT \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 \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 \t 1009.23 \t \t 96.62 473.9 26.27 \t \t 59.44 1012.23 58.77 \t \t 443.67 15.89 43.96 \t \t \t 1014.02 75.24 \t 467.35 9.48 \t \t 44.71 1019.12 \t \t 66.43 478.42 14。64 1021.78 \t 41.25 \t 475.98 11.74 \t 43.56 \t 1015.14 \t 70.72 \t 477.5 –