首页 > 解决方案 > 使用 Keras 制作自动编码器时出现未知错误

问题描述

from tensorflow.keras import metrics
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Reshape, Input, Dense,Flatten, Reshape
import numpy as np

↑ 导入包

from keras.datasets import mnist

(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape(60000,28,28,-1)
x_test = x_test.reshape(10000,28,28,-1)

↑ 加载数据,mnist。

x_train = x_train.astype('float32') / 255.
x_train = x_train[:,:,:,]
x_test = x_test.astype('float32') / 255.
x_test = x_train
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))  
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))  

input_img = Input(shape=(28, 28, 1))  

↑ 处理数据并制作输入层。

# encoder
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Flatten()(x)
x = Dense(64, activation='relu')(x)
x = Dense(10, activation='relu')(x)
encoded = Dense(1, activation='softmax')(x)

encoder = Model(input_img, encoded, name = "encoder")

↑ 编码器部分。我正在尝试将 mnist 图像压缩为 1 个值。

# decoder
decoder_input= Input((1))
decoder = Dense(64, activation='relu')(decoder_input)
x=  Dense(64, activation='relu')(decoder)
x=  Dense(98, activation='relu')(x)
x=  Dense(196, activation='relu')(x)
x=  Dense(392, activation='relu')(x)
x=  Dense(784, activation='relu')(x)
decoded =  Reshape([28,28,1])(x)

decoder = Model(decoder_input, decoded, name='decoder')

↑和解码器部分。从一个值制作 mnist 图像。

auto_input = Input(shape=(28,28,1))
encoded = encoder(auto_input)
decoded = decoder(encoded)

auto_encoder = Model(auto_input, decoded)
auto_encoder.compile(optimizer='adam', loss='binary_crossentropy')

↑ 连接编解码器。

auto_encoder.fit(
    x_train, 
    x_train,
    epochs=64,
    batch_size=128,
    shuffle=True,
    validation_data=(x_test, x_test)              
) 

↑ 并试图学习我的自动编码器,但它失败了。

错误信息如下。

UnknownError:获取卷积算法失败。这可能是因为 cuDNN 初始化失败,因此请尝试查看上面是否打印了警告日志消息。

我在谷歌搜索了很多时间,但我仍然无法获得线索。我制作了正确的数据形状,正确的输出形状,但显示错误。

问题的原因是什么?

标签: autoencodertf.keras

解决方案


RTX 2070 GPU 要求在最新版本的 CUDA 和 CuDNN 中将内存增长设置为 True。

将这些行添加到您运行的文件的顶部:

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)

推荐阅读