我是Tensorflow的python和神经网络的新手,所以我不知道如何恢复保存的模型并进行预测。我已经从直线跟随器机器人的三个摄像机(X_test)收集了数据,并且模拟器为每个摄像机视图(y_test)设置了车轮速度(左右)。我需要训练一个神经网络,以根据相机视图预测车轮速度。那是代码:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
x_train = np.zeros((2392,3072))
y_train = np.zeros((2392,2))
x_test = np.zeros((2392,3072))
y_test = np.zeros((2392,2))
X_1_train = np.loadtxt('X_Treinamento_3.txt')
X_2_train = np.loadtxt('X_Treinamento_4.txt')
y_1_train = np.loadtxt('Vl_Vr_Treinamento_3.txt')
y_2_train = np.loadtxt('Vl_Vr_Treinamento_4.txt')
X_1_test = np.loadtxt('X_Teste_3.txt')
X_2_test = np.loadtxt('X_Teste_4.txt')
y_1_test = np.loadtxt('Vl_Vr_Teste_3.txt')
y_2_test = np.loadtxt('Vl_Vr_Teste_4.txt')
x_train = np.concatenate((X_1_train[4:1200,:],X_2_train[4:1200,:]))/255
x_test = np.concatenate((X_1_test[4:1200,:],X_2_test[4:1200,:]))/255
y_train = np.concatenate((y_1_train[4:1200,:],y_2_train[4:1200,:]))
y_test = np.concatenate((y_1_test[4:1200,:],y_2_test[4:1200,:]))
n_inputs = 32*32*3 #3 images 32x32 pixels
n_hidden_1 = 300 # 1st layer number of neurons
n_hidden_2 = 100 # 2nd layer number of neurons
n_outputs = 2
tf.compat.v1.disable_eager_execution()
X = tf.compat.v1.placeholder("float",[None,n_inputs])
Y = tf.compat.v1.placeholder("float",[None,n_outputs])
weights = {
'h1': tf.Variable(tf.random.normal([n_inputs, n_hidden_1],stddev=0.1)),#4 inputs 10 nodes in h1 layer
'h2': tf.Variable(tf.random.normal([n_hidden_1, n_hidden_2],stddev=0.1)),# 10 nodes in h2 layer
'out': tf.Variable(tf.random.normal([n_hidden_2, n_outputs],stddev=0.1))# 1 ouput label
}
biases = {
'b1': tf.Variable(tf.random.normal([n_hidden_1])),
'b2': tf.Variable(tf.random.normal([n_hidden_2])),
'out': tf.Variable(tf.random.normal([n_outputs]))
}
def neural_net(x):
#hidden layer 1
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.tanh(layer_1)#activation
#hideen layer 2
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.tanh(layer_2)#activation
# output layer
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return (out_layer)
Y_hat=neural_net(X)
loss_op=tf.losses.mean_squared_error(Y,Y_hat)#loss function
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=1e-3) # define optimizer # play around with learning rate
train_op = optimizer.minimize(loss_op) # minimize losss
init = tf.compat.v1.global_variables_initializer()
epoch=2000
with tf.compat.v1.Session() as sess:
sess.run(init)
saver = tf.compat.v1.train.Saver()
for i in range(0,epoch):
sess.run(train_op,feed_dict={X:x_train,Y:y_train})
loss=sess.run(loss_op,feed_dict={X:x_train,Y:y_train})
if(i%100==0):
print("epoch no "+str(i),(loss))
pred=sess.run(Y_hat,feed_dict={X:x_test})
saver.save(sess, "output")
因此,我需要您的帮助来检查我是否正确保存了模型,如果不正确,如何保存,以及如何使用保存的模型加载和预测。
答案 0 :(得分:0)
您正确地保存了它,但是,如果要保存更多的检查点(例如:也许最后一个检查点过拟合,并且先前的检查点将提供更好的测试数据结果),则不需要添加global_step(您的检查点将被保存为输出0,输出2000等),有关更多信息,请点击此处docs
saver.save(sess, "output", global_step = i)
根据您的情况进行加载和进行预测如下所示:
with tf.compat.v1.Session() as sess:
sess.run(init)
saver = tf.compat.v1.train.Saver()
saver.restore(sess, "output")
pred=sess.run(Y_hat,feed_dict={X:x_test})
“输出”是您保存的检查点的路径