【问题标题】:Why does my Keras model train after I load it, even though I have not actually supplied any new training data?为什么我的 Keras 模型在我加载后会进行训练,即使我实际上并没有提供任何新的训练数据?
【发布时间】:2019-09-22 21:24:56
【问题描述】:

我正在尝试使用 tf.keras 使用 LSTM 模型进行训练和预测。我在两个不同的文件中编写了代码,LSTMTraining.py 用于训练 Keras 模型(并将其保存到文件中),Predict.py 应该加载到 Keras 模型中并使用它来进行预测。出于某种原因,当我在 Predict.py 中加载模型时,它开始训练,即使我没有在该文件中使用 model.fit() 命令。为什么会这样?

我已将模型保存为多种不同的文件格式。例如,我尝试将模型的架构保存到 JSON 文件中(使用 model_to_json()),并分别保存权重,然后分别加载这两个文件,然后将它们组合起来。我还尝试将它们一起保存到一个文件中(使用 model.save()),然后将其加载。

在 LSTMTraining.py 中创建和训练模型(注意:log_similarity_loss 只是我为模型创建的自定义损失函数):

# Machine learning
import tensorflow as tf
from tensorflow.python.keras import layers
import numpy as np

# Load/save data
import pickle
import os

# Shuffling
from sklearn.utils import shuffle

# Parameters
epochs = 5
display_step = 1000
n_input = 5
wordvec_len = 5
n_hidden = 512
recurrent_dropout = 0
dropout = 0

# Load data
with open("Vectorized_Word_By_Word.txt", "rb") as data:
    vectorized_txt = pickle.load(data)

# Prepare data into format for training (x: [prev-words], y: [next-word])
x_train, y_train = [], []
for n in range(0, len(vectorized_txt) - n_input - 1):
    prev_words = vectorized_txt[n: n+5]
    next_word = vectorized_txt[n+6]
    x_train.append(prev_words)
    y_train.append(next_word)
x_train, y_train = np.array(x_train), np.array(y_train)
x_train, y_train = shuffle(x_train, y_train, random_state=0)


def log_similarity_loss(y_actual, y_pred):
    """Log similarity loss calculation."""
    cos_similarity = tf.keras.losses.CosineSimilarity(axis=0)(y_actual, y_pred)
    scaled_similarity = tf.add(tf.multiply(0.5, cos_similarity), 0.5)
    return -0.5*tf.math.log(scaled_similarity)


log_similarity_loss(
    [0.05, 0.01, 0.05, 1.2], [0.05, -0.01, 0.05, -1.2])

model = tf.keras.Sequential([
    layers.LSTM(n_hidden, input_shape=(n_input, wordvec_len),
                dropout=dropout, recurrent_dropout=recurrent_dropout,
                return_sequences=True),
    layers.LSTM(n_hidden, dropout=dropout,
                recurrent_dropout=recurrent_dropout),
    layers.Dense(wordvec_len)
])

model.compile(loss=log_similarity_loss,
              optimizer='adam', metrics=['cosine_proximity'])

model.fit(x_train, y_train, epochs=epochs, batch_size=12)

model.save("Keras_Model.h5", include_optimizer=True, save_format='h5')

# Save model weights and architecture
model.save_weights('model_weights.h5')
with open("model_architecture.json", "w") as json_file:
    json_file.write(model.to_json())

在Predict.py中加载模型(注意:所有从“WordModel.py”导入的函数只是我写的与Keras无关的文本处理函数):

from WordModel import word_by_word, word_to_vec, vec_to_word
import gensim

import tensorflow as tf
from tensorflow.python.keras.models import load_model, model_from_json

with open('model_architecture.json', 'r') as json_file:
    model_json = json_file.read()

keras_model = model_from_json(model_json)
keras_model.load_weights("model_weights.h5")

我期待没有输出,只是要加载的模型。但是,我得到了模型的详细训练输出(在运行 Predict.py 时):

  12/1212 [..............................] - ETA: 3:32 - loss: 0.2656 - cosine_proximity: 0.0420
  24/1212 [..............................] - ETA: 1:55 - loss: 0.2712 - cosine_proximity: 0.2066
  36/1212 [..............................] - ETA: 1:24 - loss: 0.2703 - cosine_proximity: 0.2294
  48/1212 [>.............................] - ETA: 1:08 - loss: 0.2394 - cosine_proximity: 0.2690
  60/1212 [>.............................] - ETA: 58s - loss: 0.2286 - cosine_proximity: 0.2874 
  72/1212 [>.............................] - ETA: 52s - loss: 0.2247 - cosine_proximity: 0.2750
  84/1212 [=>............................] - ETA: 47s - loss: 0.2115 - cosine_proximity: 0.2924 

等等。

请注意,我没有在我的 Predict.py 文件中创建任何训练命令。我已经多次重新运行代码,并确保我运行的是正确的文件。不过,似乎没有任何效果。

感谢您的帮助!

【问题讨论】:

  • 你确定你运行的是正确的文件吗?
  • 是的,我已经仔细检查过了。我首先运行 LSTMTraining.py 文件,然后运行 ​​Predict.py 文件。即使我运行 Predict.py 文件,它也会显示训练消息。
  • 拥有 LSTMTraining.pyPredict.py完整 源代码会有所帮助(不是JSON);另外,您如何执行 Predict.py - 从不同 .py 文件中的 IPython 内核、直接从 Predict.py 或从命令终端?
  • 我已更新帖子以包含完整的源代码。我尝试从 Windows 命令提示符和实际文件(在 VS Code 编辑器中)运行 Predict.py,但都没有工作。
  • 提示:按名称提及用户以通知他们 - 偶然发现您的问题,因为它在更新时遇到了问题

标签: python keras tf.keras


【解决方案1】:

问题可能出在您的 VSCode IDE 上,它需要额外的配置才能同时与 Python 及其包一起使用 - 当您运行一个脚本时,您可能正在运行 所有 脚本,因此出现了可见的行为.我推荐的一个解决方案是切换到Spyder 并使用Anaconda 安装你的包。两者都安装好后,在 PC 上搜索“anaconda command prompt”或“anaconda powershell”,然后在终端中输入:

conda update conda
conda update --all
conda install numpy # optional (sort of)
conda install matplotlib # optional (sort of)
# SEE BELOW
conda install -c conda-forge keras
conda update --all # final 'cleanup' command - ensures package compatibility

如果您打算使用 GPU(强烈推荐),您需要先下载 CUDA -instructions here(获取 CUDA 10 而不是文章中的 9)。然后按照文章中的方式运行conda install tensorflow-gpu

然后,在 Spyder 中:Tools -> Preferences -> PYTHONPATH manager -> 添加您计划使用的模块/数据的所有文件夹,这样您就不必每次都%cd 或担心相对路径,可以直接导入。最后,确保 Anaconda & Spyder 使用正确的Python interpreter

重新启动 Spyder,运行脚本 - 假设没有错误,一切都应该很好。

【讨论】:

  • 似乎一切正常!感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-07-28
  • 2019-05-15
  • 2020-10-22
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2017-03-27
相关资源
最近更新 更多