简体   繁体   English

如何保存 model,使用 Tensorflow 上保存的 model 进行加载和预测?

[英]How to save a model, load and predict with the saved model on Tensorflow?

I'm new to python and neural networks with Tensorflow, so I have no idea how to restore a saved model and make predictions.我是 python 和神经网络 Tensorflow 的新手,所以我不知道如何恢复保存的 model 并进行预测。 I have collected data from three cameras (X_test) of a line follower robot and It's wheels speed (left and right) set by simulator for each camera view (y_test).我从线跟随机器人的三个摄像头(X_test)收集了数据,它的轮速(左右)由模拟器为每个摄像头视图(y_test)设置。 I need to train a neural network to predict the wheels speed based on cameras views.我需要训练一个神经网络来根据相机视图预测车轮速度。 That's the code:那是代码:

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

So, I need your help to check if I'm saving the model correctly, if not, how to save it and after how to load and predict with the saved model.所以,我需要你的帮助来检查我是否正确保存了 model,如果没有,如何保存它以及如何使用保存的 model 加载和预测。

You are saving it correctly, but if you want save more checkpoints ( for example: maybe last checkpoint is overfitting and previous checkpoint will give better results with test data), than you need add global_step ( your checkpoints will be saved something like output-0, output-2000 and so on), for more info look here docs您正在正确保存它,但是如果您想保存更多检查点(例如:可能最后一个检查点过度拟合,而前一个检查点将使用测试数据提供更好的结果),那么您需要添加 global_step (您的检查点将被保存为 output-0 , output-2000 等等),更多信息请看这里的文档

saver.save(sess, "output", global_step = i)

Loading and making prediction in your case will look something like this:在您的情况下加载和进行预测将如下所示:

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

"output" is path to your saved checkpoint “输出”是您保存的检查点的路径

Thank you a lot, it works !非常感谢,它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM