使用Keras的Python代码在调用model.fit时崩溃,没有错误代码 [英] Python code using Keras crashes on call to model.fit with no error code

查看:502
本文介绍了使用Keras的Python代码在调用model.fit时崩溃,没有错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功实现了自动编码器,并对图像数据(MNIST数字)运行了自动编码器.我通过Anaconda Navigator使用Spyder.我正在运行Python 3.7.1.

I have successfully implemented and run an autoencoder on image data (MNIST digits). I use Spyder through Anaconda Navigator. I'm running Python 3.7.1.

我根据经过审查的示例构建了一个简单的CNN.我的代码通过模型的完成和训练数据的加载(在本例中为CIFAR10)执行.当我调用model.fit()时,代码崩溃,没有错误,并且内核中没有任何变量.

I constructed a simple CNN following vetted examples. My code executes through completion of the model and loading of training data (in this case, CIFAR10). When I call model.fit() the code crashes with no error and leaving no variables in the kernel.

  1. 我如何监视此代码的执行以更好地了解其崩溃的原因?
  2. 我是否对导致崩溃的代码进行了错误编码?还是这是环境或内存错误?

我从大概可以工作的CNN示例中复制了类似的代码,并用已发布的代码复制了行为(尽管我的自动编码器代码在相同的环境中运行).

I have copied similar code from presumably working CNN examples and replicated the behavior with published code (Although my autoencoder code works in the same environment).

这是我代码的相关部分:

Here is the relevant section of my code:

from keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D
from keras.models import Model
from keras.utils import to_categorical
from keras.datasets import cifar10

proceedtofit = True

#define input shape

input=Input(shape=(32,32,3))

#define layers
predictions=Conv2D(16,(3,3),activation='relu',padding='same')(input)
predictions=MaxPooling2D(pool_size=(2,2),strides=None,padding='same')(predictions)
predictions=Conv2D(4,(3,3),activation='relu',padding='same')(predictions)
predictions=MaxPooling2D(pool_size=(2,2),strides=None,padding='same')(predictions)
predictions=Flatten()(predictions)  
predictions=Dense(32,activation='relu')(predictions)
predictions=Dense(10,activation='sigmoid')(predictions)

#integrate into model

model=Model(inputs=input,outputs=predictions)
#print("Succesfully integrated model.")
model.summary()

#compile (choose optimizer and loss function)
model.compile(loss='categorical_crossentropy',metrics=['accuracy'],optimizer='adam')

#input training and test data
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Convert class vectors to binary class matrices.
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

#train model

if proceedtofit:
    model.fit(x_train, y_train, batch_size=10, epochs=50, shuffle=True,
          validation_data=(x_test, y_test))

print("Finished fit.")

代码在内核中执行,并产生预期的模型摘要.如果proceedtofitFalse,则代码正常退出.如果proceedtofitTrue,则代码将调用model.fit()方法并崩溃.详细输出开始到结束是:

The code executes in the kernel and produces the expected model summary. If proceedtofit is False, then the code exits gracefully. If proceedtofit is True, then the code calls the model.fit() method and crashes. The verbose output start to finish is:

Python 3.7.0 (default, Jun 28 2018, 07:39:16)
Type "copyright", "credits" or "license" for more information.

IPython 7.2.0 -- An enhanced Interactive Python.

runfile('/Users/Fox/Documents/Python Machine Learning/convclass.py', wdir='/Users/Fox/Documents/Python Machine Learning')
WARNING:tensorflow:From /Applications/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
Using TensorFlow backend.
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 32, 32, 3)         0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 32, 32, 16)        448       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 16)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 16, 16, 4)         580       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 4)           0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 256)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                8224      
_________________________________________________________________
dense_2 (Dense)              (None, 10)                330       
=================================================================
Total params: 9,582
Trainable params: 9,582
Non-trainable params: 0
_________________________________________________________________
(50000, 1)
(50000, 10)
WARNING:tensorflow:From /Applications/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
Train on 50000 samples, validate on 10000 samples
Epoch 1/50
2019-08-04 16:32:52.400023: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
2019-08-04 16:32:52.400364: I tensorflow/core/common_runtime/process_util.cc:71] Creating new thread pool with default inter op setting: 8. Tune using inter_op_parallelism_threads for best performance.

这时,代码退出并将我返回到内核提示符.培训(拟合)未执行,并且未返回任何错误.该模型不再存在于内存中.也就是说,在提示符下调用model.summary()会产生以下错误:

At this point, the code exits and returns me to the kernel prompt. Training (fitting) did not execute, and returned no error. The model is no longer present in memory. That is, calling model.summary() at the prompt yields the following error:

[1]:model.summary()
Traceback (most recent call last):

  File "<ipython-input-1-5f15418b3570>", line 1, in <module>
    model.summary()

NameError: name 'model' is not defined

在评论之后,我在终端中运行了代码.我确实得到了更多详细的输出和一个错误报告.我还不了解,但至少这是一个起点.有什么想法吗? (请参见下文.)

Following a comment, I ran the code in a terminal. I did get more verbose output and an error report. I don't understand it yet, but at least it is a place to start. Thoughts? (See below.)

   OMP: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized.
    OMP: Hint: This means that multiple copies of the OpenMP runtime have been 
linked into the program. That is dangerous, since it can degrade performance or 
cause incorrect results. The best thing to do is to ensure that only a single 
OpenMP runtime is linked into the process, e.g. by avoiding static linking of the 
OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround 
you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program 
to continue to execute, but that may cause crashes or silently produce incorrect 
results. For more information, please see 
http://www.intel.com/software/products/support/.
Abort trap: 6

我发现了这个. 错误#15:初始化libiomp5.dylib,但是发现libiomp5.dylib已经初始化看起来很有希望.我将探讨所提供的建议,然后将该问题与其他讨论结合起来?

I found this. Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized Looks promising. I will explore the suggestions offered and then perhaps the question should be combined with the other discussion?

推荐答案

在命令外壳(而不是Spyder)中运行代码之后,我捕获了错误并确定了一个已经解决的相关问题.

After running the code in a command shell rather than Spyder, I captured the error and identified a related question that had already been answered.

基于以下内容中的讨论:错误#15:初始化libiomp5.dylib,但是发现libiomp5.dylib已经初始化我使用conda remove tensorflow删除了tensorflow,然后使用

Based on the discussion in: Error #15: Initializing libiomp5.dylib, but found libiomp5.dylib already initialized I removed tensorflow using conda remove tensorflow and then reinstalled tensorflow and keras using

conda install -c tensorflow

conda install -c keras

然后我重新运行代码,一切在命令外壳程序和Spyder中均正常工作.

I then reran the code and everything worked in both the command shell and in Spyder.

这篇关于使用Keras的Python代码在调用model.fit时崩溃,没有错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆