即使我实际上没有提供任何新的训练数据,为什么在装载后我的Keras模型仍然训练? [英] Why does my Keras model train after I load it, even though I have not actually supplied any new training data?

查看:76
本文介绍了即使我实际上没有提供任何新的训练数据,为什么在装载后我的Keras模型仍然训练?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to train and make predictions with an LSTM model using tf.keras. I have written code in two different files, LSTMTraining.py which trains the Keras Model (and save it to a file), and Predict.py, which is supposed to load in the Keras model and use it to make predictions. For some reason, when I load the model in Predict.py, it starts training, even though I have not used the model.fit() command in that file. Why is this happening?

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

I have saved the model into multiple different file formats. For example, I've tried saving the model's architecture into a JSON file (using model_to_json()), and saving the weights seperately, then loading both of these files in seperately and then combining them. I've also tried saving them together into one file (using model.save()), and loading that in.

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

Creating and Training Model in LSTMTraining.py (Note: the log_similarity_loss was just a custom loss function I created for the model):

# 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无关的文本处理函数)

Loading in the model in Predict.py (Note: All the functions imported from "WordModel.py" are just functions for text processing I've written that are unrelated to 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时):

I was expecting no output, just the model to be loaded. However, I got the verbose training output of the model like so (when running 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文件中进行任何训练命令.我已多次重新运行代码,并确保我正在运行正确的文件.不过,似乎没有任何效果.

Note that I have not made any training command in my Predict.py file. I have rerun the code multiple times, and made sure that I was running the correct file. Still, nothing seems to be working.

感谢您的帮助!

推荐答案

您的VSCode IDE可能存在此问题,它需要进行额外的配置才能与Python及其软件包兼容-当您运行一个脚本时,您可能正在运行 all 脚本,因此是可见的行为.我建议的解决方案是切换到 Spyder 并使用

The problem's likely with your VSCode IDE, which takes additional configuring to work both with Python and its packages -- when you run one script, you may be running all the scripts, thus the seen behavior. A solution I'd recommend is switching to Spyder and installing your packages with Anaconda. Once you've installed both, search "anaconda command prompt" or "anaconda powershell" on your PC, and in the terminal, type:

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-

If you plan on using a GPU (highly recommended), you'll need to first download CUDA - instructions here (get CUDA 10 instead of 9 in the article). Then run conda install tensorflow-gpu as in the article.

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

Then, in Spyder: Tools -> Preferences -> PYTHONPATH manager -> add all folders of the modules/data you plan to use, so you don't have to %cd each time or worry about relative pathing and can import directly. Lastly, make sure Anaconda & Spyder use the right Python interpreter.

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

Restart Spyder, run scripts - assuming no bugs, all should be well.

这篇关于即使我实际上没有提供任何新的训练数据,为什么在装载后我的Keras模型仍然训练?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆