Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x tensorflow隐藏状态不';似乎没有改变_Python 3.x_Tensorflow_Rnn - Fatal编程技术网

Python 3.x tensorflow隐藏状态不';似乎没有改变

Python 3.x tensorflow隐藏状态不';似乎没有改变,python-3.x,tensorflow,rnn,Python 3.x,Tensorflow,Rnn,我正试图遵循这一点,在进行过程中对其进行重构。当我运行代码时,它似乎起作用,但当我试图打印出当前状态变量以查看神经网络内部发生的情况时,我得到了所有1s。这是预期的行为吗?是否由于某种原因未更新状态?据我所知,当前状态应该包含所有批次的隐藏层中的最新值,因此它肯定不应该是所有1s。任何帮助都将不胜感激 def __train_minibatch__(self, batch_num, sess, current_state): """ Trains one minibatch.

我正试图遵循这一点,在进行过程中对其进行重构。当我运行代码时,它似乎起作用,但当我试图打印出
当前状态
变量以查看神经网络内部发生的情况时,我得到了所有
1
s。这是预期的行为吗?是否由于某种原因未更新状态?据我所知,
当前状态
应该包含所有批次的隐藏层中的最新值,因此它肯定不应该是所有
1
s。任何帮助都将不胜感激

def __train_minibatch__(self, batch_num, sess, current_state):
    """
    Trains one minibatch.

    :type batch_num: int
    :param batch_num: the current batch number.

    :type sess: tensorflow Session
    :param sess: the session during which training occurs.

    :type current_state: numpy matrix (array of arrays)
    :param current_state: the current hidden state

    :type return: (float, numpy matrix)
    :param return: (the calculated loss for this minibatch, the updated hidden state)
    """
    start_index = batch_num * self.settings.truncate
    end_index = start_index + self.settings.truncate

    batch_x = self.x_train_batches[:, start_index:end_index]
    batch_y = self.y_train_batches[:, start_index:end_index]
    total_loss, train_step, current_state, predictions_series = sess.run(
        [self.total_loss_fun, self.train_step_fun, self.current_state, self.predictions_series],
        feed_dict={
            self.batch_x_placeholder:batch_x, 
            self.batch_y_placeholder:batch_y, 
            self.hidden_state:current_state
        })
    return total_loss, current_state, predictions_series
# End of __train_minibatch__()

def __train_epoch__(self, epoch_num, sess, current_state, loss_list):
    """
    Trains one full epoch.

    :type epoch_num: int
    :param epoch_num: the number of the current epoch.

    :type sess: tensorflow Session
    :param sess: the session during training occurs.

    :type current_state: numpy matrix
    :param current_state: the current hidden state.

    :type loss_list: list of floats
    :param loss_list: holds the losses incurred during training.

    :type return: (float, numpy matrix)
    :param return: (the latest incurred lost, the latest hidden state)
    """
    self.logger.info("Starting epoch: %d" % (epoch_num))

    for batch_num in range(self.num_batches):
        # Debug log outside of function to reduce number of arguments.
        self.logger.debug("Training minibatch : ", batch_num, " | ", "epoch : ", epoch_num + 1)
        total_loss, current_state, predictions_series = self.__train_minibatch__(batch_num, sess, current_state)
        loss_list.append(total_loss)
    # End of batch training

    self.logger.info("Finished epoch: %d | loss: %f" % (epoch_num, total_loss))
    return total_loss, current_state, predictions_series
# End of __train_epoch__()

def train(self):
    """
    Trains the given model on the given dataset, and saves the losses incurred
    at the end of each epoch to a plot image.
    """
    self.logger.info("Started training the model.")
    self.__unstack_variables__()
    self.__create_functions__()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        loss_list = []

        current_state = np.zeros((self.settings.batch_size, self.settings.hidden_size), dtype=float)
        for epoch_idx in range(1, self.settings.epochs + 1):
            total_loss, current_state, predictions_series = self.__train_epoch__(epoch_idx, sess, current_state, loss_list)
            print("Shape: ", current_state.shape, " | Current output: ", current_state)
            # End of epoch training

    self.logger.info("Finished training the model. Final loss: %f" % total_loss)
    self.__plot__(loss_list)
    self.generate_output()
# End of train()
更新


在完成并使用内置RNNAPI后,问题消失了,这意味着要么我使用
当前状态变量的方式有问题,要么对tensorflow api的更改导致了一些奇怪的事情发生(不过我很确定这是前者)。如果有人有一个明确的答案,就把这个问题留待讨论

首先,您应该确保“它似乎有效”是真的,并且您的测试错误真的越来越小

我的一个假设是,最后一批数据在末尾被零损坏,因为数据的长度
总序列长度/batch大小
不是
截断的长度的倍数。(我没有检查它是否真的发生了,它是用零填充的。教程中的代码太旧,无法在我的tf版本上运行,我们没有您的代码。)最后一个小批量,末尾只有零,可能导致最终的
当前状态
收敛到所有的1。在任何其他小批量
上,当前的\u状态
不是全部


每次运行sess.run时,您可以尝试在
列车小批量中打印
当前状态。或者每1000个小批量打印一次

首先,您应该确保“它似乎有效”是真的,并且您的测试错误真的越来越小

我的一个假设是,最后一批数据在末尾被零损坏,因为数据的长度
总序列长度/batch大小
不是
截断的长度的倍数。(我没有检查它是否真的发生了,它是用零填充的。教程中的代码太旧,无法在我的tf版本上运行,我们没有您的代码。)最后一个小批量,末尾只有零,可能导致最终的
当前状态
收敛到所有的1。在任何其他小批量
上,当前的\u状态
不是全部


每次运行sess.run时,您可以尝试在
列车小批量中打印
当前状态。或者每1000个小批量打印一次

测试误差确实会降低,从~6.7降至~3.4。我尝试了你的建议,在小批量级别查看当前状态,我得到了每个小批量中的所有1。如果有帮助的话,我的代码是打开的(我正试图让教程在基于文本的数据集上运行,所以我不得不对教程代码进行了一些调整)。此外,我的矩阵行的末尾确实有“pad”数据,但即使我将数据转换为一个长数组并将其重新格式化为小批量,问题仍然存在。测试误差确实会降低,从~6.7降到~3.4。我尝试了你的建议,在小批量级别查看当前状态,我得到了每个小批量中的所有1。如果有帮助的话,我的代码是打开的(我正试图让教程在基于文本的数据集上运行,所以我不得不对教程代码进行了一些调整)。此外,我的矩阵行的末尾确实有“pad”数据,但即使我将数据转换为一个长数组并将其重新格式化为小批量,问题仍然存在。