【问题标题】:Why do my Tensorflow neural networks act so slow after I start training it?为什么我的 Tensorflow 神经网络在我开始训练后表现如此缓慢?
【发布时间】:2019-12-02 16:15:38
【问题描述】:

我的 Tensorflow 神经网络在训练时会逐渐变慢。每次我训练网络时,它的动作都会越来越慢。

经过 30 次左右的训练迭代后,速度变得难以忍受,几乎无法使用。到第 60 次左右迭代时,程序停止响应。

我没想到这个神经网络这么复杂。这是一个简单的三层网络,与 Tensorflow 结合在一起。

你们知道如何解决这个问题吗?

import tensorflow as tf

hidden_1_layer = {'weights': tf.Variable(tf.random_normal([37500, 500])),
                  'biases': tf.Variable(tf.random_normal([500]))}
hidden_2_layer = {'weights': tf.Variable(tf.random_normal([500, 250])),
                  'biases': tf.Variable(tf.random_normal([250]))}
hidden_3_layer = {'weights': tf.Variable(tf.random_normal([250, 125])),
                  'biases': tf.Variable(tf.random_normal([125]))}
output_layer = {'weights': tf.Variable(tf.random_normal([125, 1])),
                'biases': tf.Variable(tf.random_normal([1]))}

class ImageNN():
    def train(self, array, target):
        x = tf.placeholder('float', name='x')
        l1 = tf.add(tf.matmul(x, hidden_1_layer['weights']), hidden_1_layer['biases'])
        l1 = tf.nn.relu(l1)
        l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
        l2 = tf.nn.relu(l2)
        l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
        l3 = tf.nn.relu(l3)
        output = tf.add(tf.matmul(l3, output_layer['weights']), output_layer['biases'])
        output = tf.nn.sigmoid(output)
        cost = tf.square(output-target)
        optimizer = tf.train.AdamOptimizer().minimize(cost)
        array = array.reshape(1, 37500)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            sess.run(optimizer, feed_dict={x: array})
            sess.close()
        del x, l1, l2, output, cost, optimizer

    #Do computations with our artificial nueral network
    def predict(self, data):          #Input data is of size (37500,)
        x = tf.placeholder('float', name='x')    #get data into the right rank (dimensions), this is just a placeholder, it has no values
        l1 = tf.add(tf.matmul(x, hidden_1_layer['weights']), hidden_1_layer['biases'])
        l1 = tf.nn.relu(l1)
        l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']), hidden_2_layer['biases'])
        l2 = tf.nn.relu(l2)
        l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']), hidden_3_layer['biases'])
        l3 = tf.nn.relu(l3)
        output = tf.add(tf.matmul(l3, output_layer['weights']), output_layer['biases'])
        output = tf.nn.sigmoid(output)
        data = data.reshape(1, 37500)
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            theOutput = sess.run(output, feed_dict={x: data})
            sess.close()
        del x, l1, l2, output, data
        return theOutput

【问题讨论】:

  • 1) 首先尝试训练一个简单的工作 mlp github.com/aymericdamien/TensorFlow-Examples/blob/master/… ,这证明您的环境设置正确。 2) 将简单 mlp 中的输入数据大小提高到与实际输入数据大小相当的输入大小,这将测试您的硬件是否能够进行您期望的数字运算。如果训练一直很慢,那么它只是更新硬件的问题。如果训练没有那么慢,那么您的代码中很可能存在错误。

标签: python tensorflow machine-learning neural-network


【解决方案1】:

这听起来像是内存问题。您没有在 train 方法中删除 l3 或 array ,或者在 predict 方法中删除 l3 。我不认为这是问题的原因,因为无论如何我相信这应该被 Python 解释器丢弃。

你怎么称呼这门课?可能是您将输出保留在内存中并且它变得非常大。

【讨论】:

  • 我这样称呼班级。 NN = ImageNN() 及更高版本,NN.train(array=trainingArray, target=1) 和 NN.predict(a)。保持内存中的输出是什么意思?
【解决方案2】:

似乎每次调用train() 时,tf 都在重建图形。尝试在train() 函数以及predict() 函数之外定义您的网络、损失和训练器。

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2020-10-02
    • 2017-10-12
    • 1970-01-01
    • 2023-03-21
    • 2020-08-16
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    相关资源
    最近更新 更多