【问题标题】:Keras model fit ValueError expected input_1 to be one number larger than the size of my arrayKeras 模型适合 ValueError 预期 input_1 比我的数组大小大一个数字
【发布时间】:2019-03-29 19:59:21
【问题描述】:

我正在尝试创建一个自动编码器神经网络,用于使用 Keras TensorFlow 查找异常值,我的数据是每行一个单词的文本列表,如下所示:https://pastebin.com/hEvm6qWg 它有 139 行。

当我用我的数据拟合我的模型时,我得到了错误:

ValueError: Error when checking input: expected input_1 to have shape (139,) but got array with shape (140,)

但我不知道为什么它识别为140形状数组,我的整个代码如下:

from keras import Input, Model
from keras.layers import Dense
from keras.preprocessing.text import Tokenizer

with open('drawables.txt', 'r') as arquivo:
    dados = arquivo.read().splitlines()

tokenizer = Tokenizer(filters='')
tokenizer.fit_on_texts(dados)

x_dados = tokenizer.texts_to_matrix(dados, mode="freq")

tamanho = len(tokenizer.word_index)

x = Input(shape=(tamanho,))

# Encoder
hidden_1 = Dense(tamanho, activation='relu')(x)
h = Dense(tamanho, activation='relu')(hidden_1)

# Decoder
hidden_2 = Dense(tamanho, activation='relu')(h)
r = Dense(tamanho, activation='sigmoid')(hidden_2)

autoencoder = Model(input=x, output=r)

autoencoder.compile(optimizer='adam', loss='mse')

autoencoder.fit(x_dados, epochs=5, shuffle=False)

我完全迷路了,我什至不知道我对自动编码器网络的方法是否正确,我做错了什么?

【问题讨论】:

    标签: python-3.x machine-learning keras deep-learning


    【解决方案1】:

    word_index in Tokenizer 从 1 开始,而不是从 0 开始

    例子:

    tokenizer = Tokenizer(filters='')
    tokenizer.fit_on_texts(["this a cat", "this is a dog"])
    print (tokenizer.word_index)
    

    输出:

    {'this': 1, 'a': 2, 'cat': 3, 'is': 4, 'dog': 5}
    

    索引从 1 开始,而不是从零开始。所以当我们使用这些索引创建term frequency 矩阵时

    x_dados = tokenizer.texts_to_matrix(["this a cat", "this is a dog"], mode="freq")
    

    x_dados 的形状将是 2x6,因为 numpy 数组从 0 开始索引。

    所以没有:x_dados = 1+len(tokenizer.word_index) 中的列数

    所以要修复你的代码更改

    tamanho = len(tokenizer.word_index)
    

    到

    tamanho = len(tokenizer.word_index) + 1
    

    工作样本:

    dados = ["this is a  cat", "that is a dog and a cat"]*100
    tokenizer = Tokenizer(filters='')
    tokenizer.fit_on_texts(dados)
    
    x_dados = tokenizer.texts_to_matrix(dados, mode="freq")
    tamanho = len(tokenizer.word_index)+1
    x = Input(shape=(tamanho,))
    
    # Encoder
    hidden_1 = Dense(tamanho, activation='relu')(x)
    h = Dense(tamanho, activation='relu')(hidden_1)
    
    # Decoder
    hidden_2 = Dense(tamanho, activation='relu')(h)
    r = Dense(tamanho, activation='sigmoid')(hidden_2)
    
    autoencoder = Model(input=x, output=r)
    print (autoencoder.summary())
    
    autoencoder.compile(optimizer='adam', loss='mse')
    autoencoder.fit(x_dados, x_dados, epochs=5, shuffle=False)
    

    【讨论】:

    • 谢谢!我已经对您提到的代码进行了更改,现在我收到了 IndexError: list index out of range 错误,但我不知道原因可能是什么。
    • 拟合方法的行:autoencoder.fit(x_dados, epochs=5, shuffle=False)
    • 不应该是 autoencoder.fit(x_dados,x_dados, epochs=5, shuffle=False) 你缺少你的 y (与自动编码器中的 x 相同)
    • 我怎么能错过呢?它以某种方式起作用!现在我只需要绘制它并查看数据是否有意义,但它运行了!谢谢! ?
    • @gtbono "请不要对您的问题或回答说“谢谢”添加评论。如果您想说“谢谢”,请投票或接受那个人的回答”——来自What should I do when someone answers my question?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    相关资源
    最近更新 更多