通过张量流中的预训练模型预测新数据

时间:2017-12-13 15:14:57

标签: python tensorflow machine-learning deep-learning prediction

我在张量流中训练了LSTM模型,我想基于它预测新数据。但是,我一直收到一个我无法理解的错误:

这是模型:

def __init__(self , snapshot_dir, save_dir ):
    self.save_dir = save_dir
    self.snapshot_dir =  snapshot_dir
    self.learning_rate = params["learning_rate"]
    self.training_steps = params["training_steps"]
    self.batch_size = params["batch_size"]
    self.display_step = params["display_step"]
    self.num_input = params["num_input"]  
    self.timesteps = params["timesteps"]  
    self.num_hidden = params["num_hidden"]  
    self.num_classes = params["num_classes"] 
    self.define_graph()

def define_graph(self):

    self.X = tf.placeholder("float", [None, self.timesteps, self.num_input], name="X")
    self.Y = tf.placeholder("float", [None, self.num_classes], name="Y")
    self.weights = {'out': tf.Variable(tf.random_normal([self.num_hidden, self.num_classes]))}
    self.biases = {'out': tf.Variable(tf.random_normal([self.num_classes]))}

def RNN(self):

    x = tf.unstack(self.X, self.timesteps, 1)
    lstm_cell = rnn.BasicLSTMCell(self.num_hidden, forget_bias=1.0, reuse=tf.get_variable_scope().reuse)
    outputs, states = rnn.static_rnn(lstm_cell, x , dtype=tf.float32)
    return tf.matmul(outputs[-1], self.weights['out']) + self.biases['out']

def run(self, X_train, y_train):

    logits = self.RNN()
    self.prediction = tf.nn.softmax(logits, name="prediction")
    loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
        logits=logits, labels=self.Y))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)
    train_op = optimizer.minimize(loss_op)
    correct_pred = tf.equal(tf.argmax(self.prediction, 1), tf.argmax(self.Y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
    init = tf.global_variables_initializer()
    with tf.Session() as sess:

        sess.run(init)
        saver = tf.train.Saver()
        for step in range(1, self.training_steps + 1):
            start_idx = random.randint(0, len(X_train) - self.timesteps - 1)
            batch_x = self.generate_x_batch_data(start_idx, X_train, self.batch_size)
            batch_y = self.generate_y_batch_data(start_idx, y_train, self.batch_size)

            sess.run(train_op, feed_dict={self.X: batch_x, self.Y: batch_y})
        saver.save(sess, self.save_dir, global_step=step)

预测部分:

    sess = tf.Session()
    saver = tf.train.import_meta_graph("{}.meta".format(self.newest_snapshot))
    saver.restore(sess, self.newest_snapshot)
    X_as_batch = self.aux.generate_x_batch_data(0,X, len(X))
    pred = tf.get_default_graph().get_operation_by_name("prediction").outputs[0]
    feed_dict = {tf.get_default_graph().get_operation_by_name('X').outputs[0]: X_as_batch}
    prediction = sess.run(pred,
                          feed_dict=feed_dict)

X_as_batch的形状符合pred的预期(其形状为[5126, 60, 39]pred的形状为[?, 60, 39])< / p>

但是我收到了错误

  

InvalidArgumentError(请参阅上面的回溯):您必须提供值   对于占位符张量&#39; X_1&#39; dtype浮点数和形状[?,1024]
  [[Node:X_1 = Placeholderdtype = DT_FLOAT,shape = [?,1024],   _device =&#34; /作业:本地主机/复制:0 /任务:0 / CPU:0&#34;]]

可能是什么原因?

1 个答案:

答案 0 :(得分:0)

您有X_1张量的原因是您在旧图表仍然存在时已恢复图表。为了避免使用两个名为X的TensorFlow变量,新的变量会附加_1

我无法确定,因为您还没有包含所有代码,但如果您在一次执行中完成所有工作,则可能不需要恢复任何内容。或者,您可能只需要使用saver之类的内容恢复saver.restore(sess, './model.ckpt')创建的检查点文件,并将'./model.ckpt'替换为ckpt文件的路径。