为什么我的 Keras 模型在加载后进行训练,即使我实际上没有提供任何新的训练数据?
Why does my Keras model train after I load it, even though I have not actually supplied any new training data?
我正在尝试使用 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 文件中创建任何训练命令。我已经多次重新运行代码,并确保我是 运行 正确的文件。仍然,似乎没有任何效果。
感谢您的帮助!
问题可能出在您的 VSCode IDE 上,它需要额外的配置才能与 Python 及其包一起工作——当您 运行 一个脚本时,您可能是 运行ning all 脚本,因此是看到的行为。我推荐的解决方案是切换到 Spyder and installing your packages with 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
-> 添加你打算使用的modules/data的所有文件夹,这样你就不用每次都%cd
或者担心相对路径,可以直接导入。最后,确保 Anaconda & Spyder 使用正确的 Python interpreter。
重新启动 Spyder,运行 脚本 - 假设没有错误,一切都应该很好。
我正在尝试使用 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 文件中创建任何训练命令。我已经多次重新运行代码,并确保我是 运行 正确的文件。仍然,似乎没有任何效果。
感谢您的帮助!
问题可能出在您的 VSCode IDE 上,它需要额外的配置才能与 Python 及其包一起工作——当您 运行 一个脚本时,您可能是 运行ning all 脚本,因此是看到的行为。我推荐的解决方案是切换到 Spyder and installing your packages with 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
-> 添加你打算使用的modules/data的所有文件夹,这样你就不用每次都%cd
或者担心相对路径,可以直接导入。最后,确保 Anaconda & Spyder 使用正确的 Python interpreter。
重新启动 Spyder,运行 脚本 - 假设没有错误,一切都应该很好。