List(vector)与tensorflow要求的形状不匹配

时间:2020-03-09 08:48:03

标签: python tensorflow machine-learning keras

背景: 我正在基于机器学习模型构建防病毒软件。 我建立一个程序,使List(vector)具有486个值。

例如,这是缩短版本的外观(原始矢量具有486个值):

[1.00000000e+00 1.00000000e+00 1.00000000e+00 0.00000000e+00
 1.00000000e+00 1.00000000e+00 1.00000000e+00 1.00000000e+00
 1.00000000e+00 1.00000000e+00 1.00000000e+00 5.33333333e-01
 0.00000000e+00 5.88235294e-02 3.49019608e-01 7.96078431e-01
 5.88235294e-02 3.49019608e-01 8.07843137e-01 5.88235294e-02
 3.45098039e-01 9.45098039e-01 5.88235294e-02 1.56862745e-01
 8.00000000e-01 5.88235294e-02 7.60784314e-01 5.09803922e-02
 2.50980392e-01 3.92156863e-03 5.33333333e-01 0.00000000e+00
 2.35294118e-02 5.88235294e-02 3.13725490e-01 7.56862745e-01
 5.88235294e-02 3.41176471e-01 9.49019608e-01 5.21568627e-01
 7.52941176e-01 4.58823529e-01 2.78431373e-01 2.00000000e-01
 1.00000000e+00 5.21568627e-01 1.00000000e+00 4.58823529e-01
 5.88235294e-02 5.88235294e-02 1.56862745e-01 7.76470588e-01
 5.05882353e-01 7.68627451e-01 9.72549020e-01 0.00000000e+00
 0.00000000e+00 0.00000000e+00 3.56862745e-01 3.72549020e-01
 5.45098039e-01 8.98039216e-01 3.64705882e-01 7.64705882e-01
 5.88235294e-02 1.60784314e-01 1.56862745e-02 1.41176471e-01
 5.88235294e-02 1.60784314e-01 4.54901961e-01 1.41176471e-01
 2.50980392e-01 4.54901961e-01 9.01960784e-01 1.45357859e-01
 1.27626518e-02 5.25954600e-03 5.29496962e-03 1.07052096e-02
 3.08893971e-03 3.26031344e-03 2.54954328e-03 9.53584717e-03
 1.62738028e-03 4.26500391e-03 1.86711202e-03 6.17443280e-03
 2.24815529e-03 1.28635612e-03 1.18420206e-02 1.02462344e-02]

我完成了使用数据库(从每个行提取的xlsx文件)构建模型的过程。

问题: 现在,我试图预测文件是否是病毒,我为此文件构建了一个载体,并尝试使用 model.predict(pe)函数(pe是我的载体),我收到一条错误消息,说我的向量与预期的形状不匹配:

' but received input with shape ' + str(shape))
ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 486 but received input with shape [None, 1]

这是我的代码:

from tensorflow import keras
import vector_build #custom code that I made

model = keras.models.load_model("anti_virus_model.h5")

pe = vector_build.encode_pe("C:\\Windows\\System32\\calc.exe")

print(model.predict(pe))

我真的不知道该怎么做以及如何更改向量的形状。 也许当我从.xlsx文件中提取数据时,它没有将其识别为相同格式的向量?

如果您需要我添加任何更清晰的内容,请告诉我!

很想听听您的想法! 预先感谢!

1 个答案:

答案 0 :(得分:1)

您可能对大量数据进行了模型训练?也就是说,您的输入的形状为from flask import Flask, request, jsonify, make_response from flask_restful import Api from flask_swagger_ui import get_swaggerui_blueprint app = Flask(__name__, static_folder='swagger') if app.config["ENV"] == "production": app.config.from_object("config.ProductionConfig") else: app.config.from_object("config.DevelopmentConfig") # swagger specific swagger_url = '/swagger' static_url = '/static/api.yaml' SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint( swagger_url, static_url, config={ 'app_name': "Test" } ) app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=swagger_url) # end swagger specific db = SQLAlchemy(app) jwt = JWTManager(app) api = Api(app, prefix='/api') # middleware @app.before_request def before_request_callback(): """ This is callback will run before each and every endpoint . :return: """ # Checking jwt-authentication for every request except login if str(request.url_rule) == '/api/v1/login': # do execute some code elif str(request.url_rule) != '/api/v1/user/create' and \ # do execute some code elif str(request.url_rule) == '/swagger': # problem is here 。 Keras模型总是期望输入中有一个批处理轴,即使它只是一个示例。因此,请确保输入具有批处理轴。

就目前而言(我假设),您的输入只是形状[batch_size, 486],即它没有批处理轴-您只需要添加一个即可。您可以这样操作:[486]。这样会在前面添加一个大小为1的轴,从而使输入形状为[1,486]。