180509 tensorflow-gpu显存分配与InternalError (see above for traceback): Blas SGEMM launch failed

tensorflow以及更新了若干版本,早期教材《Tensorflow:实战Google深度学习框架》基于tensorflow1.0的LSTM代码已经更新1.4后的LSTM代码均不适用于tensorlfow1.1-1.3.【TensorFlow】LSTM(使用TFLearn预测正弦sin函数)针对上述问题,对原来的代码进行了修改。可适用于上述及以上版本,1.2/1.5测试没问题。

如果你的程序报类似这样的错,说明你在使用 GPU 计算(默认行为)且你的 GPU 可用显存不足,TensorFlow 总是试图为自己分配全部显存,例如你的显存是 2GB,那么他就会试图为自己分配 2GB,但是一般情况下你的显存不会一点都不被其他程序占用的,导致 TensorFlow 分配显存失败。


这里写图片描述

InternalError (see above for traceback): Blas SGEMM launch failed

当出现上述错误时,可尝试指定GPU显存配置的方式解决:

# 建立深层循环网络模型
config = tf.contrib.learn.RunConfig(gpu_memory_fraction=0.3, log_device_placement=True)
regressor = SKCompat(learn.Estimator(model_fn=lstm_model, model_dir='model/',config=config))

【TensorFlow】LSTM(使用TFLearn预测正弦sin函数)中模型定义如下:

# LSTM结构单元
def LstmCell():
    lstm_cell = tf.contrib.rnn.BasicLSTMCell(HIDDEN_SIZE)
    return lstm_cell


def lstm_model(X, y):
    # 使用多层LSTM,不能用lstm_cell*NUM_LAYERS的方法,会导致LSTM的tensor名字都一样
    cell = tf.contrib.rnn.MultiRNNCell([LstmCell() for _ in range(NUM_LAYERS)])

    # 将多层LSTM结构连接成RNN网络并计算前向传播结果
    output, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
    output = tf.reshape(output, [-1, HIDDEN_SIZE])

    # 通过无激活函数的全联接层计算线性回归,并将数据压缩成一维数组的结构
    predictions = tf.contrib.layers.fully_connected(output, 1, None)

    # 将predictions和labels调整为统一的shape
    y = tf.reshape(y, [-1])
    predictions = tf.reshape(predictions, [-1])

    # 计算损失值
    loss = tf.losses.mean_squared_error(predictions, y)

    # 创建模型优化器并得到优化步骤
    train_op = tf.contrib.layers.optimize_loss(
        loss,
        tf.train.get_global_step(),
        optimizer='Adagrad',
        learning_rate=0.1)

    return predictions, loss, train_op

猜你喜欢

转载自blog.csdn.net/qq_33039859/article/details/80259254