【问题标题】:Segmentation fault using tf.RunOptions使用 tf.RunOptions 的分段错误
【发布时间】:2019-05-17 08:57:20
【问题描述】:

我正在开发一个相当大的模型,我需要使用tf.RunOptions 或其他调试器来精简我的代码,因为我遇到了非常小的批量大小的 OOM 错误。但是我在使用tf.RunOptions 后遇到了段错误。

我不认为这是模型问题,因为以下代码也会出现问题(而没有runopt 的相同代码正在工作):

    import tensorflow as tf
    import tensorflow.keras.models as mm
    import tensorflow.keras.layers as ll
    import numpy as np

    model = mm.Sequential([
        ll.Dense(27,input_shape=(1,)),
        ll.Activation('relu'),
        ll.Dense(27),
        ll.Activation('softmax')
        ])

    runopt = tf.RunOptions(report_tensor_allocations_upon_oom = True)

    model.compile(optimizer='sgd',
                  loss='mean_squared_error',
                  metrics=['accuracy'],
                  options=runopt)

    a = np.zeros(27)*10

    model.fit(a,a,epochs=10)

在 Linux 18.04 上遇到同样的错误(tensorflow-gpu 安装了 pip,tf version 1.13.1python version 3.6.7CUDA 9.1.85GeForce GTX 980 4GB)macOS 10.12.6(tensorflow-cpu 安装了 pip,tf version 1.13.1python version 3.7.2)

【问题讨论】:

    标签: python tensorflow keras tf.keras


    【解决方案1】:

    要使用tf.RunOptions,您必须使用tf.RunMetadata()

    这解决了这个问题:

    import tensorflow as tf
    import tensorflow.keras.models as mm
    import tensorflow.keras.layers as ll
    import numpy as np
    
    model = mm.Sequential([
        ll.Dense(27,input_shape=(1,)),
        ll.Activation('relu'),
        ll.Dense(27),
        ll.Activation('softmax')
        ])
    
    runopt = tf.RunOptions(report_tensor_allocations_upon_oom = True)
    runmeta = tf.RunMetadata()
    
    model.compile(optimizer='sgd',
                  loss='mean_squared_error',
                  metrics=['accuracy'],
                  options=runopt,
                  run_metadata=runmeta)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 2015-11-16
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多