【问题标题】:Save keras model for production mode without Tensorflow将 keras 模型保存为生产模式
【发布时间】:2022-11-16 02:26:41
【问题描述】:

我想保存经过训练的 keras 模型,以便它可以在应用程序的 django rest 后端中使用。 我做了很多研究,但似乎没有安装 tensorflow 就无法使用这些模型。

那么,这个存储空间有什么用呢?我不想在服务器上安装像tensorflow这样的重型库。 我测试了使用 pickle 和 joblib 以及 keras 自己的 model.save() 进行保存。

有没有办法在不安装 tensorflow 而只使用 keras 本身的情况下加载这个模型?

这是我的代码的一部分

from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout


        xtrain, ytrain = np.array(xtrain), np.array(ytrain)
        ytrain = np.reshape(ytrain, (ytrain.shape[0], 1, 1))
        model = Sequential()
        model.add(LSTM(150, return_sequences=True, input_shape=(xtrain.shape[1], 1)))
        model.add(LSTM(150, return_sequences=False))
        model.add(Dense(25))
        model.add(Dense(1))
        model.compile(optimizer='adam', loss='mean_squared_error')
        model.fit(xtrain, ytrain, batch_size=1, epochs=7)
        model.save('model.h5')

通常可以完美运行,但如果我在其他地方使用该模型,则会出现此错误:

ModuleNotFoundError:没有名为“tensorflow”的模块

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您不需要在生产中使用 Tensorflow,您可以通过替换编程语言中的随机函数来使用系数。

    示例:输入数组时间系数矩阵,未装箱的系统输入到带装箱容器中反馈系统的输出。

    temp = tf.random.normal([10], 1, 0.2, tf.float32)
    temp = np.asarray(temp) * np.asarray([ coefficient_0, coefficient_1, coefficient_2, coefficient_3, coefficient_4, coefficient_5, coefficient_6, coefficient_7, coefficient_8, coefficient_9 ]) #action = actions['up']
    temp = tf.nn.softmax(temp)
    
    action = int(np.argmin(temp))
    

    输出:从动作列表中的大部分响应动作中选择的动作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2020-07-13
      • 2019-01-26
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多