KerasTF Conv2D模型运行时无响应型号.fi

2024-06-24 11:28:30 发布

您现在位置:Python中文网/ 问答频道 /正文

与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层的问题。我还运行keras2.0.8


Tags: fromtestimportaddbackendlabelsmodelis