【问题标题】:How to use a tflearn trained model in an application?如何在应用程序中使用经过 tflearn 训练的模型?
【发布时间】:2016-11-14 15:37:05
【问题描述】:

我目前正在尝试在应用程序中使用经过训练的模型。

我一直在使用this code 使用 LSTM 模型生成美国城市名称。代码运行良好,我确实设法获得了城市名称。

现在,我正在尝试保存模型,以便将其加载到不同的应用程序中,而无需再次训练模型。

这是我的基本应用程序的代码:

from __future__ import absolute_import, division, print_function

import os
from six import moves
import ssl
import tflearn
from tflearn.data_utils import *


path = "US_cities.txt"
maxlen = 20
X, Y, char_idx = textfile_to_semi_redundant_sequences(
    path, seq_maxlen=maxlen, redun_step=3)


# --- Create LSTM model
g = tflearn.input_data(shape=[None, maxlen, len(char_idx)])
g = tflearn.lstm(g, 512, return_seq=True, name="lstm1")
g = tflearn.dropout(g, 0.5, name='dropout1')
g = tflearn.lstm(g, 512, name='lstm2')
g = tflearn.dropout(g, 0.5, name='dropout')
g = tflearn.fully_connected(g, len(char_idx), activation='softmax', name='fc')
g = tflearn.regression(g, optimizer='adam', loss='categorical_crossentropy',
                            learning_rate=0.001)


# --- Initializing model and loading
model = tflearn.models.generator.SequenceGenerator(g, char_idx)
model.load('myModel.tfl')
print("Model is now loaded !")


# 
#    Main Application   
# 

while(True):
    user_choice = input("Do you want to generate a U.S. city names ? [y/n]")
    if user_choice == 'y':
        seed = random_sequence_from_textfile(path, 20)
        print("-- Test with temperature of 1.5 --")
        model.generate(20, temperature=1.5, seq_seed=seed, display=True)
    else:
        exit()

这是我得到的输出:

Do you want to generate a U.S. city names ? [y/n]y
-- Test with temperature of 1.5 --
rk
Orange Park AcresTraceback (most recent call last):
  File "App.py", line 46, in <module>
    model.generate(20, temperature=1.5, seq_seed=seed, display=True)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/models/generator.py", line 216, in generate
    preds = self._predict(x)[0]
  File "/usr/local/lib/python3.5/dist-packages/tflearn/models/generator.py", line 180, in _predict
    return self.predictor.predict(feed_dict)
  File "/usr/local/lib/python3.5/dist-packages/tflearn/helpers/evaluator.py", line 69, in predict
    o_pred = self.session.run(output, feed_dict=feed_dict).tolist()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 894, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1, 25, 61) for Tensor 'InputData/X:0', which has shape '(?, 20, 61)'

不幸的是,在我的应用程序中使用 generate() 时,我看不出为什么形状发生了变化。谁能帮我解决这个问题?

提前谢谢你

威廉

【问题讨论】:

  • 这不能完全解决您的问题,但您可以尝试将seq_maxlen=20 添加到tflearn.models.generator.SequenceGenerator。我猜25 来自这个构造函数参数。
  • 你好sygi,谢谢你的回复,很抱歉回复晚了。我更改了 seq_maxlen,形状问题现在已修复!但正如你所说,它没有完全工作......生成的名称根本不是新的。我尝试将 checkpoint_path 添加到构造函数中,但仍然没有更改任何内容。

标签: python-3.x tensorflow deep-learning lstm tflearn


【解决方案1】:

解决了吗?

一个解决方案是简单地向 python 脚本添加“模式”,这要归功于参数解析器:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("mode", help="Train or/and test", nargs='+', choices=["train","test"])
args = parser.parse_args()

然后

if args.mode == "train":
     # define your model
     # train the model
     model.save('my_model.tflearn')

if args.mode == "test":
     model.load('my_model.tflearn')
     # do whatever you want with your model

我真的不明白为什么这会起作用,以及为什么当您尝试从不同的脚本加载模型时它不起作用。 但我想这暂时应该没问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多