tensorflow 基本CNN分类模型存在UnimplementedError:图形执行错误:

5vf7fwbs  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(230)

我尝试了一个样本代码CNN应用程序上的MNIST数据分类从书中:

from keras import layers
from keras import models

model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (28, 28, 1)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation = 'relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation = 'relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation = 'relu'))
model.add(layers.Dense(10, activation = 'softmax'))
model.summary()

#Test this model on mnist
from keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000,28,28,1))
train_images = train_images.astype('float32')/255
test_images = test_images.reshape((10000,28,28,1))
test_images = test_images.astype('float32')/255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)

代码应该是正确的,但当我运行代码时发生了一个错误:
未实现错误:图形执行错误:
我认为这个问题可能是由不同版本的tensorflow引起的(我的tensorflow是2.8,而示例代码是在tensorflow 2.0中运行的)。有人能告诉我如何解决这个问题吗?

0ve6wy6x

0ve6wy6x1#

看起来你需要在形状上执行,而不是在单一数字的所有晶粒。

[样品]:

import tensorflow as tf

import tensorflow_datasets as tfds
from tensorflow.keras.utils import to_categorical

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSets
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
ds = tfds.load('mnist', split='train', shuffle_files=True)
ds = ds.shuffle(1024).batch(64).prefetch(tf.data.experimental.AUTOTUNE)
assert isinstance(ds, tf.data.Dataset)

for example in ds.take(1):
    image, label = example["image"], example["label"]
    #################################################
    image = tf.cast( image, dtype=tf.float32 )
    image = tf.math.divide_no_nan( image, 255 )
    #################################################
    
train_images = image
train_labels = to_categorical(label)
test_labels = to_categorical(label)

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, (3,3), activation = 'relu', input_shape = (28, 28, 1)))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64, (3,3), activation = 'relu'))
model.add(tf.keras.layers.MaxPooling2D((2,2)))
model.add(tf.keras.layers.Conv2D(64, (3,3), activation = 'relu'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64, activation = 'relu'))
model.add(tf.keras.layers.Dense(10, activation = 'softmax'))
model.summary()

model.compile(optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(train_images, train_labels, epochs=5, batch_size=64)

[输出]:enter image description here

相关问题