python - 恢复训练有素的RNN模型

时间:2018-02-21 19:35:10

标签: python tensorflow

我训练并试图保存RNN。我可以稍后在同一个脚本中恢复它,以执行与预期结果非常匹配的预测。但是,当我重新加载元图并尝试在单独的脚本中恢复会话时,预测的结果就会消失。我在anaconda通过spyder使用python 3.6和tensorflow 1.2.1。

训练模型的代码

import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics.regression import r2_score, mean_squared_error
import pandas as pd
import csv
tf.reset_default_graph()

#Reading the dataset:
def read_dataset():
    df=pd.read_csv("Standardized_Training_Data.csv")
    X=df[df.columns[0:9]].values
    Y=df[df.columns[9]].values

    return (X,Y)

X,Y=read_dataset()

#Reshape the targer and feature data frames
X=X.reshape([12929,9]) 
Y=Y.reshape([12929,1])

#train_x, test_x, train_y, test_y=train_test_split(X,Y, test_size=0.20)
train_size=int(X.shape[0]*0.7)
train_x=X[0:train_size,:]; train_y=Y[0:train_size,:]
test_x=X[train_size:X.shape[0],:]; test_y=Y[train_size:X.shape[0],:]

learning_rate=0.01
training_epochs=50 
cost_history=np.empty(shape=[1], dtype=float)


model_path="RNN_Model"

batch_size=10
chunk_size=9 #num of features
n_chunks=1
rnn_size=100
n_class=1

x=tf.placeholder('float', [None, n_chunks, chunk_size], name='x')
y_=tf.placeholder('float', [None, n_class], name='y_')

def recurrent_nn(x):

    layer={"weights": tf.Variable(tf.random_normal([rnn_size, n_class]), name="weights"),
            "biases": tf.Variable(tf.random_normal([n_class]), name="biases")}
    tf.add_to_collection('vars', layer['weights'])
    tf.add_to_collection('vars', layer['biases'])
    with  tf.variable_scope("cell_def"):
        x= tf.transpose(x, [1,0,2])
        x=tf.reshape(x, [-1, chunk_size])
        x=tf.split(x, n_chunks, 0)
        lstm_cell=tf.nn.rnn_cell.BasicLSTMCell(rnn_size) 
    with tf.variable_scope('lstm_def'):
        outputs, states=tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

    output = tf.add(tf.matmul(outputs[-1], layer['weights']),layer['biases'],name='output')
    tf.add_to_collection('vars', output)
    return output


#Call your model defined

y=recurrent_nn(x)

cost_function =tf.reduce_mean(tf.square(y-y_)) #for regression we use mse as cost function

optimizer=tf.train.AdamOptimizer(learning_rate).minimize(cost_function)

mse_history=[]
accuracy_history=[]
overal_cost_history=[]

total_len=len(train_x)
total_batch = int(total_len/batch_size)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range (training_epochs):
        cost_history=np.empty(shape=[1], dtype=float)
        #accuracy_history=[]
        pred=[]
        for i in range(total_batch):
             x_batch=train_x[i*batch_size:(i+1)*batch_size]
             y_batch=train_y[i*batch_size:(i+1)*batch_size]
             x_batch=x_batch.reshape((batch_size,n_chunks,chunk_size))

             sess.run(optimizer, feed_dict={x:x_batch, y_:y_batch})

             cost=sess.run(cost_function, feed_dict={x:x_batch, y_:y_batch})
             cost_history=np.append(cost_history, cost)
             predict=sess.run(y, feed_dict ={x: x_batch})
             pred=np.append(pred, predict)

        pred=pred.reshape((-1,1))
        overal_cost=np.mean(cost_history)
        overal_cost_history=np.append(overal_cost_history,overal_cost)
        r_squared=r2_score(pred, train_y[0:9100])
        accuracy_history=np.append(accuracy_history, r_squared)
        mse= mean_squared_error(pred, train_y[0:9100])
        mse_history=np.append(mse_history, mse)
        print('epoch:', epoch, '-', 'cost', overal_cost, "-Train Accuracy:", r_squared,"-MSE:", mse)

    saver=tf.train.Saver()
    save_path=saver.save(sess, model_path)
    print("Model saved in file: %s" % save_path)
    plt.plot(mse_history, 'r')
    plt.show()
    plt.plot(overal_cost_history, 'b')
    plt.show()
    plt.plot(accuracy_history, 'k')
    plt.show()



#Testing:

with tf.Session() as sess:

    model_path="RNN_Model"
    saver.restore(sess, model_path)
    #sess.run(tf.global_variables_initializer())
    data_set=X
    y_data_set=Y
    test_pred=[]
    total_len=len(data_set)
    total_batch = int(total_len/batch_size)

    for i in range(total_batch):
        x_batch=data_set[i*batch_size:(i+1)*batch_size]
        x_batch=x_batch.reshape((batch_size,n_chunks,chunk_size))

        test_predict=sess.run(y, feed_dict ={x: x_batch})
#        print(test_predict)
        test_pred=np.append(test_pred, test_predict)
        #print(i, x_batch)    
    test_pred=test_pred.reshape((-1,1))
    r_squared=np.divide(np.sum(np.square(test_pred-np.mean(y_data_set))),np.sum(np.square(y_data_set-np.mean(y_data_set))))
    print("accuracy", r_squared)

#Applying the model for a new data set 

def read_dataset():
    df=pd.read_csv("Standardized_Testing_Data.csv")
    New_X=df[df.columns[0:9]].values

    return (New_X)

New_X=read_dataset()
New_X=New_X.reshape([4561,9]) 

with tf.Session() as sess:

    model_path="RNN_Model"
    saver.restore(sess, model_path)

    New_X_pred=[]
    total_len=len(New_X)
    total_batch = int(total_len/batch_size)

    for i in range(total_batch):
        x_batch=New_X[i*batch_size:(i+1)*batch_size]
        x_batch=x_batch.reshape((batch_size,n_chunks,chunk_size))

        New_X_predict=sess.run(y, feed_dict ={x: x_batch})
        New_X_pred=np.append(New_X_pred, New_X_predict)

    New_X_pred=New_X_pred.reshape((-1,1))

with open("PredictedValues.csv","w+", newline='') as my_csv:
    csvWriter = csv.writer(my_csv,delimiter=',')
    csvWriter.writerows(New_X_pred)   

重新加载模型的代码

import tensorflow as tf
from tensorflow.contrib import rnn
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics.regression import r2_score, mean_squared_error
import pandas as pd
import csv

def read_dataset():
    df=pd.read_csv("Standardized_Testing_Data.csv")
    New_X=df[df.columns[0:9]].values

    return (New_X)

New_X=read_dataset()
New_X=New_X.reshape([4561,9]) 

batch_size=10
chunk_size=9
n_chunks=1
rnn_size=100
n_class=1

x=tf.placeholder('float', [None, n_chunks, chunk_size], name='x')

def recurrent_nn(x):

    layer={"weights": tf.Variable(tf.random_normal([rnn_size, n_class]), name="weights"),
            "biases": tf.Variable(tf.random_normal([n_class]), name="biases")}
    tf.add_to_collection('vars', layer['weights'])
    tf.add_to_collection('vars', layer['biases'])
    with  tf.variable_scope("cell_def"):
        x= tf.transpose(x, [1,0,2])
        x=tf.reshape(x, [-1, chunk_size])
        x=tf.split(x, n_chunks, 0)
        lstm_cell=tf.nn.rnn_cell.BasicLSTMCell(rnn_size) 
    with tf.variable_scope('lstm_def', reuse = True): #if run right after training set reuse to true, otherwise set to false
        outputs, states=tf.contrib.rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

    output = tf.add(tf.matmul(outputs[-1], layer['weights']),layer['biases'],name='output')
    tf.add_to_collection('vars', output)
    return output

y=recurrent_nn(x)
init=tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    saver = tf.train.import_meta_graph('RNN_Model.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./'))

    New_X_pred=[]
    total_len=len(New_X)
    total_batch = int(total_len/batch_size)

    for i in range(total_batch):
        x_batch=New_X[i*batch_size:(i+1)*batch_size]
        x_batch=x_batch.reshape((batch_size,n_chunks,chunk_size))

        New_X_predict=sess.run(y, feed_dict ={x: x_batch})
        New_X_pred=np.append(New_X_pred, New_X_predict)

    New_X_pred=New_X_pred.reshape((-1,1))

with open("PredictedValues2.csv","w+", newline='') as my_csv:
    csvWriter = csv.writer(my_csv,delimiter=',')
    csvWriter.writerows(New_X_pred)   

1 个答案:

答案 0 :(得分:0)

1]从元文件中恢复

  • 仅当设备分配未更改时,才能从已保存的meta_graph重新开始培训。
  • 您可以引用此链接以使用元文件还原模型:https://www.tensorflow.org/api_docs/python/tf/train/import_meta_graph
  • 如果您使用此选项,则需要记录在运行新脚本时将使用的张量的名称
  • 在运行新脚本时,您必须使用相同的名称访问张量

2]使用tf.train.Saver()对象

  • 试试这个(我总是这样做):

    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, tf.train.latest_checkpoint('./'))
    
  • 请勿将saver变量设为tf.train.import_meta_graph()的对象,而应将其设为tf.train.Saver()的对象

  • 这对我有用

3]使用.pb文件恢复

  • 您可以做的其他事情是在训练后通过将检查点文件转换为.pb文件protocol buffer文件
  • 来冻结图表
  • protocol buffer是检查点文件的简化版本,您可以使用此新的冻结图加载会话
  • 您可以从.pb
  • 制作tensorflow.python.tools.freeze_graph个文件
  • 要了解有关如何制作.pb文件的更多信息,请参阅此github回购: https://github.com/jayshah19949596/Tensorboard-Visualization-Freezing-Graph