当我尝试训练模型时,Keras出现“ <未注册内核>”错误

时间:2019-09-18 20:01:31

标签: python tensorflow keras

当我尝试创建模型时,当我尝试训练它时会崩溃 代码来创建模型,然后对其进行训练

model = Sequential()
model.add(CuDNNLSTM(128, input_shape=(train_x.shape[1:]), return_sequences=True))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(CuDNNLSTM(128, return_sequences=True))
model.add(Dropout(0.1))
model.add(BatchNormalization())

model.add(CuDNNLSTM(128))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(2, activation='softmax'))


opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)

# Compile model
model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=opt,
    metrics=['accuracy'])

# program crashes hear
history = model.fit(
    train_x, train_y,
    batch_size=BATCH_SIZE,
    epochs=EPOCHS,
    validation_data=(validation_x, validation_y),
    callbacks=[tensorboard, checkpoint] )

我收到以下错误

  

[种子= 0,辍学= 0,T = DT_FLOAT,input_mode =“ linear_input”,   direction =“ unidirectional”,rnn_mode =“ lstm”,is_training = true,   seed2 = 0]已注册的设备:[CPU]已注册的内核:

完整的堆栈跟踪

    K.batch_set_value([(v, 0) for v in self.variables])
  File "C:\python37-32\lib\site-packages\tensorflow\python\keras\backend.py", line 3071, in batch_set_value
    get_session().run(assign_ops, feed_dict=feed_dict)
  File "C:\python37-32\lib\site-packages\tensorflow\python\keras\backend.py", line 462, in get_session
    _initialize_variables(session)
  File "C:\python37-32\lib\site-packages\tensorflow\python\keras\backend.py", line 879, in _initialize_variables
    [variables_module.is_variable_initialized(v) for v in candidate_vars])
  File "C:\python37-32\lib\site-packages\tensorflow\python\client\session.py", line 950, in run
    run_metadata_ptr)
  File "C:\python37-32\lib\site-packages\tensorflow\python\client\session.py", line 1173, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\python37-32\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_run
    run_metadata)
  File "C:\python37-32\lib\site-packages\tensorflow\python\client\session.py", line 1370, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: No OpKernel was registered to support Op 'CudnnRNN' used by node cu_dnnlstm/CudnnRNN (defined at /Users/tpottel/programming/java/eclipse projects/BBitBotCoinUsing2/pythonproject/deeplearning/lstm-bitcoin.py:128) with these attrs: [seed=0, dropout=0, T=DT_FLOAT, input_mode="linear_input", direction="unidirectional", rnn_mode="lstm", is_training=true, seed2=0]
Registered devices: [CPU]
Registered kernels:
  <no registered kernels>
\

1 个答案:

答案 0 :(得分:1)

根据我的评论,因为这似乎有所帮助:看来您没有在系统上设置CUDA / CuDNN,或者可能没有GPU。您可以使用LSTM层而不是CuDNNLSTM来运行,但是如果GPU加速很重要,则需要配置GPU。那是在操作系统方面,而不是Keras或Python。

要使用LSTM层:

from keras.layers import LSTM

model = Sequential()
model.add(LSTM(128, input_shape=(train_x.shape[1:]), return_sequences=True))
model.add(Dropout(0.2))
# ...

如果您有GPU并想使用它,请尝试使用pip安装tensorflow-gpu并查看是否无法使GPU加速正常工作。那完全是一回事,所以我鼓励您在线进行研究,然后在遇到特定问题时再回到SO。