【问题标题】:Model is trained to predict whether the spellings in the sentence is correct or not模型被训练来预测句子中的拼写是否正确
【发布时间】:2021-01-04 13:24:30
【问题描述】:

当我尝试运行我的代码时,它会向我显示警告

WARNING:tensorflow:Model was constructed with shape (None, 256) for input Tensor("embedding_input:0", shape=(None, 256), dtype=float32), but it was called on an input with incompatible shape (None, 1).

W tensorflow/core/framework/op_kernel.cc:1744] OP_REQUIRES failed at cast_op.cc:124 : Unimplemented: Cast string to float is not supported

127.0.0.1 - - [17/Sep/2020 20:51:50] "POST /submit HTTP/1.1" 500 -

这是我的功能

def predict():
    input = [x for x in request.form.values()]
    prediction = model.predict(input)
    return render_template("index.html",prediction_text = 'Output is $ {}'.format(prediction))

【问题讨论】:

    标签: html python-3.x machine-learning keras tensorflow2.0


    【解决方案1】:

    如果没有完整的代码,很难说哪里出了问题。正如警告消息所暗示的那样,模型是在形状的输入(无,256)上训练的,但是在预测您使用不同的输入时。请提供与用于训练模型的输入数据具有相同特征和维度的预测函数的数据。

    我在下面提供了一个示例来模仿您所面临的错误。在这里,我在 8 列上训练了模型,但在预测期间我只使用了 1 列。

    代码 -

    %tensorflow_version 1.x
    import tensorflow as tf
    print(tf.__version__)
    # MLP for Pima Indians Dataset saved to single file
    import numpy as np
    from numpy import loadtxt
    from tensorflow.keras.models import Model
    from tensorflow.keras.layers import Dense, Input, Concatenate
    
    # load pima indians dataset
    dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
    
    # split into input (X) and output (Y) variables
    X = dataset[:,0:8]
    Y = dataset[:,8]
    
    input1 = Input(shape=(8,))
    
    # define model
    x1 = Dense(12, input_shape = (2,), activation='relu')(input1)
    x = Dense(8, activation='relu')(x1)
    x = Dense(1, activation='sigmoid')(x)
    
    model = Model(inputs=input1, outputs=x)
    
    # compile model
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    # Model Summary
    model.summary()
    
    # Fit the model
    model.fit(x=X, y=Y, epochs=150, batch_size=10, verbose=0)
    
    # evaluate the model
    scores = model.predict(dataset[:,0])
    

    输出-

    1.15.2
    Model: "model_2"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    input_3 (InputLayer)         [(None, 8)]               0         
    _________________________________________________________________
    dense_6 (Dense)              (None, 12)                108       
    _________________________________________________________________
    dense_7 (Dense)              (None, 8)                 104       
    _________________________________________________________________
    dense_8 (Dense)              (None, 1)                 9         
    =================================================================
    Total params: 221
    Trainable params: 221
    Non-trainable params: 0
    _________________________________________________________________
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-4-32d25cb68448> in <module>()
         42 
         43 # evaluate the model
    ---> 44 scores = model.predict(dataset[:,0])
    
    3 frames
    /tensorflow-1.15.2/python3.6/tensorflow_core/python/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
        570                              ': expected ' + names[i] + ' to have shape ' +
        571                              str(shape) + ' but got array with shape ' +
    --> 572                              str(data_shape))
        573   return data
        574 
    
    ValueError: Error when checking input: expected input_3 to have shape (8,) but got array with shape (1,)
    

    【讨论】:

    • 我们的最后一层有一个重复的“x”,因此你永远无法到达它。
    猜你喜欢
    • 2022-10-19
    • 2020-06-04
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多