代码之家  ›  专栏  ›  技术社区  ›  Nenntron

Python训练和加载Tensorflow/Keras模型

  •  0
  • Nenntron  · 技术社区  · 1 年前

    嘿,我想保存并加载我训练过的模型,但这样做会遇到一些问题。

    在训练python脚本中,在训练模型后,它对字符串“nobody like you”进行预测,并给出正确的结果“Spam:96.0959%”。

    然而,当我在python测试代码中尝试它时,我用相同的权重加载相同的模型,对于相同的文本,它会给我一个完全不同的读数,例如“批准:49%”。

    所以我不确定,但我认为我以错误的方式保存或加载了经过训练的模型。

    我想要实现的是,首先训练一个模型,保存该模型,并能够使用训练后的模型进行文本预测,而无需每次都对其进行重新训练。

    我的培训代码片段:

    # Convert labels to numerical format
    label_to_index = {
        'Approve': 0,
        'Spam': 1,
        ...
    }
    
    # Tokenization and Padding
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(posts)
    num_unique_words = len(tokenizer.word_index)
    tokenizer.num_words = num_unique_words
    sequences = tokenizer.texts_to_sequences(posts)
    max_sequence_length = 7500;
    
    X = pad_sequences(sequences, maxlen=max_sequence_length)
    y = np.array([label_to_index[label] for label in labels])
    
    # Split data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
    # Neural Network Architecture
    model = Sequential()
    vocab_size = tokenizer.num_words
    model.add(Embedding(input_dim=vocab_size, output_dim=50, input_length=max_sequence_length))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dense(19, activation='softmax'))
    
    # Model Compilation
    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    # Model Training
    history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_test, y_test))
    
    model_architecture = model.to_json()
    with open('model_architecture_old.json', 'w') as json_file:
        json_file.write(model_architecture)
    
    model.save_weights('model_weights_old.h5')
    

    我加载并测试节省的权重和json,这样做:

    # Load the model architecture from the JSON file
    with open('model_architecture_old.json', 'r') as json_file:
        model_architecture = json_file.read()
    
    # Create a new model instance using the loaded architecture
    loaded_model = model_from_json(model_architecture)
    
    # Load the saved weights from the checkpoint file
    loaded_model.load_weights('model_weights_old.h5')
    
    # Compile the model before using it (same compilation as during training)
    loaded_model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    
    # Get user input for testing
    num_texts = int(input("Enter the number of texts you want to test: "))
    texts = []
    for i in range(num_texts):
        text = input(f"Enter text {i+1}: ")
        texts.append(text)
    
    # Tokenization and Padding
    tokenizer = Tokenizer()
    tokenizer.fit_on_texts(texts)  # Replace 'texts' with your text data
    sequences = tokenizer.texts_to_sequences(texts)
    max_sequence_length = 7500;
    X = pad_sequences(sequences, maxlen=max_sequence_length)
    
    # Make predictions using the loaded model
    predictions = loaded_model.predict(X)
    
    0 回复  |  直到 1 年前