微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

损失函数超过 32 个小批量使用 tensorflow 1.1X

如何解决损失函数超过 32 个小批量使用 tensorflow 1.1X

我一直试图在 32 个小批量上实现损失。我的总数据集为 1024,我将数据分成 32 个不同的小批量。我试图从它的标签(1,69)中减去 minibatch 的每一行(1row x 69 列)。我在下面放了一小段代码。我收到有关未初始化变量的错误。我会很感激这个错误的可能解决方

#This is the function that calculates the loss function

def _build_loss_function(self,label,mini batch):
    error_i = tf.map_fn(lambda row: tf.square(label - row),mini batch)
    square_difference = tf.reduce_sum(error_i,1)
    self.loss = tf.reduce_mean(square_difference,name='loss')
    self.loss = tf.cast(self.loss,tf.float32)




#this is the main file where the mini batch is calculated
data = np.random.random([1056,69])
agent = Agent.Control()

if __name__ == '__main__':
    mini_batch_size = 32
    num_epochs = 500
    training_data_size = 1024
    test_data_size = 32

    # data set to train and test data sets
    e_training_data = data[0:1024,:]
    e_test_data = data[1024:,:]

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

        train_losses = []
        test_losses = []

        for i in range(num_epochs):
            # randomize the expert_training data
            np.random.shuffle(e_training_data)
            e_frames_batches = np.split(e_training_data,int(training_data_size / mini_batch_size))
            epoch_loss = 0

            for mini_batch in e_frames_batches:
                loss,predicted_angles,gradients,_ = agent.train(sess,mini_batch)
                epoch_loss += loss

            epoch_loss /= len(e_frames_batches)
            test_loss = agent.predict_loss(e_test_data.astype('float32'))
            train_losses.append(epoch_loss)
            test_losses.append(test_loss)

            print("\nEpoch: {},loss: {},test_loss: {} ".format(i,epoch_loss,test_loss))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。