运行model.fit时,Keras-TF Conv2D模型无响应

时间:2018-04-23 15:52:35

标签: python tensorflow keras

使用keras CNN尝试一些MNIST教程,并在运行密集模型时取得了成功。但是,当我包含Conv2D图层时,fit()函数只调用Epoch 1 / X,然后在结束该过程之前暂停一段时间。

import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.utils import np_utils
from keras.layers import Conv2D, MaxPooling2D
from keras.datasets import mnist


def lol():
    model = Sequential()

    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(1,28,28), data_format='channels_first'))
    model.add(Conv2D(32, (3, 3), activation='relu'))

    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(10, activation='softmax'))
    return model

#hyper-parameters
batch_size=128
epochs=10


(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

#reshape images
train_images = train_images.reshape(60000, 1, 28, 28)
test_images = test_images.reshape(10000, 1, 28, 28)

#set type to float32
train_images = train_images.astype('float32')
test_images = test_images.astype('float32')

#normalise image values from 0-255 to 0-1
train_images = train_images/255
test_images = test_images/255

#one-hot labels
train_labels=keras.utils.to_categorical(train_labels,10)
test_labels=keras.utils.to_categorical(test_labels,10)


model=lol()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(train_images, train_labels,batch_size=batch_size,epochs= 3,verbose=1)

结果:

Using TensorFlow backend.
WARNING:tensorflow:From G:\Python35\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Use the retry module or similar alternatives.
WARNING:tensorflow:From G:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py:1205: calling reduce_prod (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
WARNING:tensorflow:From G:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py:2755: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
WARNING:tensorflow:From G:\Python35\lib\site-packages\keras\backend\tensorflow_backend.py:1290: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead
Epoch 1/3

=============================== RESTART: Shell ===============================
>>> 

我正在读它可能是Keras2.0问题,但不太确定。有没有人经历过这个?

如果我将模型更改为

,它也会正常运行
model = Sequential()
model.add(Dense(20, input_dim=784, kernel_initializer='normal', activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

这让我相信它是Conv2d层的一个问题。我也在运行Keras 2.0.8

1 个答案:

答案 0 :(得分:0)

发现我安装了“错误”版本的cuDNN。我从7.1.3回滚到7.0.4,现在一切正常!