ValueError:检查输入时出错:预期input_2的形状为(8,),但数组的形状为(1,)

李寅成

我是一名正在与keras学习自动编码器的学生。我使用mnist数据集作为输入(784个节点),并制作了8个节点作为隐藏层。我想做的是任意调整隐藏层的值。但是,当我在隐藏层中任意输入shape(8,)的输入时,

ValueError:检查输入时出错:预期input_2的形状为(8,),但数组的形状为(1,)

发生错误。我输入的矩阵是np.array([0,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()
女嘴

调试代码时,我发现“ encoded_imgs”的形状为(10000,8)。数组[0, 0, 0, 0, 0, 0, 0, 0]的形状确实为,(8,)但它只是一维数组,而您的解码器.predict方法期望的形状为(10000,8)。如果您只想用零尝试一下,请改用它:

decoded_imgs = decoder.predict(np.zeros(shape=(10000, 8)))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ValueError:检查输入时出错:预期density_1_input的形状为(8,),但数组的形状为(1,)

来自分类Dev

ValueError:检查输入时出错:预期conv2d_1_input具有形状(224,224,1),但数组的形状为(224,224,8)

来自分类Dev

ValueError:检查输入时出错:预期conv2d_input具有4维,但数组的形状为(None,1)

来自分类Dev

ValueError:检查输入时出错:预期density_13_input具有形状(3,),但数组的形状为(1,)

来自分类Dev

ValueError:检查输入时出错:预期conv2d_5_input具有形状(6705,20,130),但数组形状为(20,130,1)

来自分类Dev

ValueError:检查输入时出错:预期 conv2d_9_input 具有形状 (64, 64, 3) 但得到形状为 (32, 32, 1) 的数组

来自分类Dev

ValueError:检查输入时出错:预期输入_1的形状为(224、224、3),但数组的形状为(3、224、224)

来自分类Dev

ValueError:检查输入时出错:预期lstm_13_input具有3维,但数组的形状为(1,1)

来自分类Dev

ValueError:检查输入时出错:预期dense_1_input有2维,但得到了形状为(60000, 28, 28)的数组

来自分类Dev

检查输入时出错:预期density_203_input具有形状(1202),但数组形状为(1,)

来自分类Dev

检查输入时出错:预期dense_1_input 具有形状(3773,) 但得到形状为(111,) 的数组

来自分类Dev

检查输入时出错:预期 conv2d_1_input 具有形状 (64, 64, 3) 但得到形状为 (64, 64, 4) 的数组

来自分类Dev

model.fit提供ValueError:检查输入时出错:预期的conv2d获得了形状为()的数组

来自分类Dev

ValueError:检查输入时出错:预期lstm_16_input具有3个维,但数组的形状为(836,400,3,1)

来自分类Dev

ValueError:检查输入时出错:预期time_distributed_55_input具有5个维,但数组的形状为(10,48,48,1)

来自分类Dev

检查输入时出错:预期lstm_input具有3个维,但数组的形状为(4,1)

来自分类Dev

检查输入时出错:预期conv2d_17_input具有4个维,但数组的形状为(28,28,1)

来自分类Dev

Python神经网络-检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(700,128,33)

来自分类Dev

检查输入时出错:预期embedding_Embedding1_input具有形状[,1103],但得到形状为[1103,1]的数组

来自分类Dev

简单的机器学习算法不起作用:ValueError:检查输入时出错:预期dense_4_input具有形状(无,5)但得到形状为(5,1)的数组

来自分类Dev

ValueError:检查时出错:预期dense_1_input有2维,但得到形状为(1, 16, 16, 512)的数组

来自分类Dev

Tensorflow / keras错误:ValueError:检查输入时出错:预期的lstm_input具有3个维,但数组的形状为(4012,42)

来自分类Dev

检查输入时出错:预期input_3具有3个维度,但数组的形状为(860,11)

来自分类Dev

检查输入时出错:预期lstm_input具有3维,但数组的形状为(160,1000)

来自分类Dev

Keras:检查输入时出错:预期输入_1的形状为(299,299,3),但数组的形状为(229,229,3)

来自分类Dev

使用conv1D“检查输入时出错:预期conv1d_input具有3维,但数组的形状为(213412,36)”

来自分类Dev

ValueError:检查目标时出错:预期density_3具有形状(1,),但数组形状为(2,)角

来自分类Dev

ValueError:检查目标时出错:预期输出具有形状 (1,) 但得到形状为 (2,) 的数组

来自分类Dev

ValueError:检查目标时出错:预期dense_2具有形状(1,)但得到形状为(14,)的数组

Related 相关文章

  1. 1

    ValueError:检查输入时出错:预期density_1_input的形状为(8,),但数组的形状为(1,)

  2. 2

    ValueError:检查输入时出错:预期conv2d_1_input具有形状(224,224,1),但数组的形状为(224,224,8)

  3. 3

    ValueError:检查输入时出错:预期conv2d_input具有4维,但数组的形状为(None,1)

  4. 4

    ValueError:检查输入时出错:预期density_13_input具有形状(3,),但数组的形状为(1,)

  5. 5

    ValueError:检查输入时出错:预期conv2d_5_input具有形状(6705,20,130),但数组形状为(20,130,1)

  6. 6

    ValueError:检查输入时出错:预期 conv2d_9_input 具有形状 (64, 64, 3) 但得到形状为 (32, 32, 1) 的数组

  7. 7

    ValueError:检查输入时出错:预期输入_1的形状为(224、224、3),但数组的形状为(3、224、224)

  8. 8

    ValueError:检查输入时出错:预期lstm_13_input具有3维,但数组的形状为(1,1)

  9. 9

    ValueError:检查输入时出错:预期dense_1_input有2维,但得到了形状为(60000, 28, 28)的数组

  10. 10

    检查输入时出错:预期density_203_input具有形状(1202),但数组形状为(1,)

  11. 11

    检查输入时出错:预期dense_1_input 具有形状(3773,) 但得到形状为(111,) 的数组

  12. 12

    检查输入时出错:预期 conv2d_1_input 具有形状 (64, 64, 3) 但得到形状为 (64, 64, 4) 的数组

  13. 13

    model.fit提供ValueError:检查输入时出错:预期的conv2d获得了形状为()的数组

  14. 14

    ValueError:检查输入时出错:预期lstm_16_input具有3个维,但数组的形状为(836,400,3,1)

  15. 15

    ValueError:检查输入时出错:预期time_distributed_55_input具有5个维,但数组的形状为(10,48,48,1)

  16. 16

    检查输入时出错:预期lstm_input具有3个维,但数组的形状为(4,1)

  17. 17

    检查输入时出错:预期conv2d_17_input具有4个维,但数组的形状为(28,28,1)

  18. 18

    Python神经网络-检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(700,128,33)

  19. 19

    检查输入时出错:预期embedding_Embedding1_input具有形状[,1103],但得到形状为[1103,1]的数组

  20. 20

    简单的机器学习算法不起作用:ValueError:检查输入时出错:预期dense_4_input具有形状(无,5)但得到形状为(5,1)的数组

  21. 21

    ValueError:检查时出错:预期dense_1_input有2维,但得到形状为(1, 16, 16, 512)的数组

  22. 22

    Tensorflow / keras错误:ValueError:检查输入时出错:预期的lstm_input具有3个维,但数组的形状为(4012,42)

  23. 23

    检查输入时出错:预期input_3具有3个维度,但数组的形状为(860,11)

  24. 24

    检查输入时出错:预期lstm_input具有3维,但数组的形状为(160,1000)

  25. 25

    Keras:检查输入时出错:预期输入_1的形状为(299,299,3),但数组的形状为(229,229,3)

  26. 26

    使用conv1D“检查输入时出错:预期conv1d_input具有3维,但数组的形状为(213412,36)”

  27. 27

    ValueError:检查目标时出错:预期density_3具有形状(1,),但数组形状为(2,)角

  28. 28

    ValueError:检查目标时出错:预期输出具有形状 (1,) 但得到形状为 (2,) 的数组

  29. 29

    ValueError:检查目标时出错:预期dense_2具有形状(1,)但得到形状为(14,)的数组

热门标签

归档