Я студент, который изучает авто кодировщик с керасом. Я использовал набор данных mnist в качестве входных данных (784 узла) и сделал 8 узлов в качестве скрытого слоя. То, что я хочу сделать, это настроить значение скрытого слоя произвольно. Но когда я помещаю ввод shape (8,), который я сделал произвольно, в скрытый слой,
ValueError: Ошибка при проверке ввода: ожидалось, что input_2 будет иметь shape (8,), но получит массив с shape (1, )
возникает ошибка. Введенная мной матрица имеет вид np.array ([0,0,0,0,0,0,0,0]), который явно в форме (8,), а не (1,0).
Ниже приведен полный текст кода. Пожалуйста помоги. Спасибо.
from keras.layers import Input, Dense
from keras.models import Model
import matplotlib.pyplot as plt
import numpy as np
import sys
# size of hidden layer
encoding_dim = 8
# input place holder
input_img = Input(shape=(784,))
# "encoded"
encoded = Dense(encoding_dim, activation='relu')(input_img)
# "decoded" (lossy reconstruction)
decoded = Dense(784, activation='sigmoid')(encoded)
# input -> recomstructed model
autoencoder = Model(input_img, decoded)
# encoder model
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
# decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
from keras.datasets import mnist
np.set_printoptions(threshold=sys.maxsize)
(x_train, train_labels), (x_test, test_labels) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
autoencoder.fit(x_train, x_train,
epochs=30,
batch_size=256,
shuffle=True,
validation_data=(x_test, x_test))
# encoding, decodeing
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
# I want to change this as decoded_imgs = decoder.predict([0,0,0,0,0,0,0,0])
n = 10
plt.figure(num=1, figsize=(20, 3))
for i in range(n):
ax = plt.subplot(2, n, i + 1)
plt.imshow(encoded_imgs[i].reshape(2, 4).T)
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
plt.figure(num=2, figsize=(20, 3))
for i in range(n):
# original data plot
ax = plt.subplot(2, n, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_yaxis().set_visible(False)
ax.get_xaxis().set_visible(False)
# reconstructed data plot
ax = plt.subplot(2, n, i + 1 + n)
plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()