keras自动编码器资源耗尽错误

keras自动编码器资源耗尽错误,keras,autoencoder,Keras,Autoencoder,我有一个数据集,其中样本数为25000,特征数为24995。我正试图在这个数据上训练一个keras自动编码器模型,并面临OOM错误。该模型的一些细节如下: Input matrix shape : (25000, 24995) 该输入矩阵被划分为验证集,作为培训和测试数据 Train Matrix shape : (18750, 24995) Test Matrix shape : (6250, 24995) 培训代码是 from keras.layers import Input, Den

我有一个数据集,其中样本数为25000,特征数为24995。我正试图在这个数据上训练一个keras自动编码器模型,并面临OOM错误。该模型的一些细节如下:

Input matrix shape : (25000, 24995)
该输入矩阵被划分为验证集,作为培训和测试数据

Train Matrix shape : (18750, 24995)
Test Matrix shape : (6250, 24995)
培训代码是

from keras.layers import Input, Dense
input_layer = Input(shape=(train_matrix.shape[1],))

encoding_hlayer1_dims = 12500
encoding_hlayer1 = Dense(encoding_hlayer1_dims, activation='relu', trainable=True, name="layer1")(input_layer)

decoding_hlayer1 = Dense(train_matrix.shape[1], activation='relu')(encoding_hlayer1)

autoencoder = Model(input_layer, decoding_hlayer1)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
该模型的摘要如下所示

Layer (type)                 Output Shape              Param #   
=================================================================
input_2 (InputLayer)         (None, 24995)             0         
_________________________________________________________________
layer1 (Dense)               (None, 12500)             312450000 
_________________________________________________________________
dense_1 (Dense)              (None, 24995)             312462495 
=================================================================
Total params: 624,912,495
Trainable params: 624,912,495
Non-trainable params: 0
代码来训练模型

## Train
history = autoencoder.fit(train_matrix.toarray(), train_matrix.toarray(),
                epochs=50,
                batch_size=64,
                shuffle=True,
                validation_data=(test_matrix.toarray(), test_matrix.toarray()))
当我开始训练模式时,出现以下错误:

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[24995,12500]
     [[Node: mul_3 = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](beta_1/read, Variable/read)]]
我使用的是2台Nvidia Tesla K40c Gpu,每台12克。据我所知,该模型在内存中的容量应为25000*12500*2=0.625 GB。此外,输入矩阵数据类型为numpy.float32

有人能指出我到底做错了什么吗

更新:完整的错误日志

Train on 18750 samples, validate on 6250 samples
Epoch 1/100


ResourceExhaustedErrorTraceback (most recent call last)
<ipython-input-8-503b20168fa5> in <module>()
      6                 batch_size=4096,
      7                 shuffle=True,
----> 8                 validation_data=(test_matrix.toarray(), test_matrix.toarray()))
      9 #     autoencoder.save("/tmp/Models/sae_models/epochs_" + str(epochs) + ".model", include_optimizer=True)
     10 

/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, **kwargs)
   1428                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
   1429                               callback_metrics=callback_metrics,
-> 1430                               initial_epoch=initial_epoch)
   1431 
   1432     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, epochs, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
   1077                 batch_logs['size'] = len(batch_ids)
   1078                 callbacks.on_batch_begin(batch_index, batch_logs)
-> 1079                 outs = f(ins_batch)
   1080                 if not isinstance(outs, list):
   1081                     outs = [outs]

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in __call__(self, inputs)
   2263                 value = (indices, sparse_coo.data, sparse_coo.shape)
   2264             feed_dict[tensor] = value
-> 2265         session = get_session()
   2266         updated = session.run(self.outputs + [self.updates_op],
   2267                               feed_dict=feed_dict,

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in get_session()
    166     if not _MANUAL_VAR_INIT:
    167         with session.graph.as_default():
--> 168             _initialize_variables()
    169     return session
    170 

/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in _initialize_variables()
    339     if uninitialized_variables:
    340         sess = get_session()
--> 341         sess.run(tf.variables_initializer(uninitialized_variables))
    342 
    343 

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    787     try:
    788       result = self._run(None, fetches, feed_dict, options_ptr,
--> 789                          run_metadata_ptr)
    790       if run_metadata:
    791         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    995     if final_fetches or final_targets:
    996       results = self._do_run(handle, final_targets, final_fetches,
--> 997                              feed_dict_string, options, run_metadata)
    998     else:
    999       results = []

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1130     if handle is None:
   1131       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1132                            target_list, options, run_metadata)
   1133     else:
   1134       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
   1150         except KeyError:
   1151           pass
-> 1152       raise type(e)(node_def, op, message)
   1153 
   1154   def _extend_graph(self):

ResourceExhaustedError: OOM when allocating tensor with shape[24995,12500]
     [[Node: layer1/kernel/Assign = Assign[T=DT_FLOAT, _class=["loc:@layer1/kernel"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](layer1/kernel, layer1/random_uniform)]]

Caused by op u'layer1/kernel/Assign', defined at:
  File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/local/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2822, in run_ast_nodes
    if self.run_code(code, result):
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-ee2fe8e92d7c>", line 4, in <module>
    encoding_hlayer1 = Dense(encoding_hlayer1_dims, activation='relu', trainable=True, name="layer1")(input_layer)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 569, in __call__
    self.build(input_shapes[0])
  File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 825, in build
    constraint=self.kernel_constraint)
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 391, in add_weight
    weight = K.variable(initializer(shape), dtype=dtype, name=name)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py", line 321, in variable
    v = tf.Variable(value, dtype=_convert_string_dtype(dtype), name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 200, in __init__
    expected_shape=expected_shape)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 309, in _init_from_args
    validate_shape=validate_shape).op
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/state_ops.py", line 271, in assign
    validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_state_ops.py", line 45, in assign
    use_locking=use_locking, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[24995,12500]
     [[Node: layer1/kernel/Assign = Assign[T=DT_FLOAT, _class=["loc:@layer1/kernel"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/gpu:0"](layer1/kernel, layer1/random_uniform)]]
对18750个样本进行训练,对6250个样本进行验证
纪元1/100
ResourceExhausterRorTraceback(最近一次调用上次)
在()
6批次尺寸=4096,
7 shuffle=True,
---->8验证数据=(test_matrix.toarray(),test_matrix.toarray())
9#自动编码器.save(“/tmp/Models/sae#Models/epochs#”+str(epochs)+.model”,include#optimizer=True)
10
/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in-fit(self、x、y、批量大小、历元、冗余、回调、验证拆分、验证数据、无序排列、类权重、样本权重、初始历元、**kwargs)
1428 val_f=val_f,val_ins=val_ins,shuffle=shuffle,
1429回调度量=回调度量,
->1430初始_历元=初始_历元)
1431
1432 def评估(自身、x、y、批次大小=32、详细程度=1、样本重量=无):
/usr/local/lib/python2.7/dist-packages/keras/engine/training.pyc in_-fit_循环(self、f、ins、out_标签、批大小、历元、详细、回调、val_f、val_-ins、随机、回调度量、初始历元)
1077批处理日志['size']=len(批处理ID)
1078回调。在批处理开始时(批处理索引、批处理日志)
->1079 outs=f(ins\U批次)
1080如果不存在(输出,列表):
1081输出=[输出]
/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow\u backend.pyc in\uuuuu调用(self,输入)
2263值=(索引、稀疏coo.data、稀疏coo.shape)
2264进给量[tensor]=值
->2265会话=获取会话()
2266 updated=session.run(self.outputs+[self.updates\u op],
2267馈送指令=馈送指令,
/get_session()中的usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc
166如果不是手动变量初始化:
167带有session.graph.as_default():
-->168_初始化_变量()
169返回会议
170
/usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.pyc in_initialize_variables()
339如果未初始化的_变量:
340 sess=获取会话()
-->341 sess.run(tf.variables\u初始值设定项(未初始化的\u变量))
342
343
/运行中的usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc(self、fetches、feed\u dict、options、run\u元数据)
787尝试:
788结果=self.\u运行(无、取数、输入、选项、,
-->789运行(元数据)
790如果运行\u元数据:
791 proto_data=tf_session.tf_GetBuffer(run_metadata_ptr)
/运行中的usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc(self、handle、fetches、feed、dict、options、run\u元数据)
995如果最终_获取或最终_目标:
996 results=self.\u do\u run(句柄、最终目标、最终获取、,
-->997提要内容(字符串、选项、运行元数据)
998其他:
999结果=[]
/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc in_do_运行(self、handle、target_列表、fetch_列表、feed_dict、options、run_元数据)
1130如果句柄为“无”:
1131返回self.\u do\u call(\u run\u fn,self.\u session,feed\u dict,fetch\u list,
->1132目标\u列表、选项、运行\u元数据)
1133其他:
1134返回self.\u do.\u调用(\u prun\u fn,self.\u会话,句柄,提要\u dict,
/调用中的usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.pyc(self,fn,*args)
1150键错误除外:
1151通行证
->1152提升类型(e)(节点定义、操作、消息)
1153
1154定义扩展图(自):
ResourceExhausterRor:OOM在使用形状分配张量时[2499512500]
[[Node:layer1/kernel/Assign=Assign[T=DT\u FLOAT,[u class=[“lock:@layer1/kernel”],使用\u locking=true,验证\u shape=true,[u device=“/job:localhost/replica:0/task:0/gpu:0”](layer1/kernel,layer1/random\u uniform)]]
由操作u'layer1/kernel/Assign'引起,定义于:
文件“/usr/lib/python2.7/runpy.py”,第174行,在运行模块中作为主模块
“\uuuuu main\uuuuuuuuuuuuuuuuuuuuuuuuu”,fname,loader,pkg\u name)
文件“/usr/lib/python2.7/runpy.py”,第72行,在运行代码中
run_globals中的exec代码
文件“/usr/local/lib/python2.7/dist packages/ipykernel_launcher.py”,第16行,在
app.launch_new_instance()
文件“/usr/local/lib/python2.7/dist packages/traitlets/config/application.py”,第658行,在launch_实例中
app.start()
文件“/usr/local/lib/python2.7/dist-packages/ipykernel/kernelapp.py”,第477行,开头
ioloop.ioloop.instance().start()
文件“/usr/local/lib/python2.7/dist-packages/zmq/eventloop/ioloop.py”,第177行,开头
super(ZMQIOLoop,self).start()
文件“/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py”,第888行,开头
handler_func(fd_obj,事件)
文件“/usr/local/lib/python2.7/dist packages/tornado/stack\u context.py”,第277行,在空包装中
返回fn(*args,**kwargs)
文件“/usr/local/lib/python2.7/dist packages/zmq/eventloop/zmqstream.py”,第440行,在_