如何在预测文件中修复NameError?

时间:2018-12-29 10:07:33

标签: tensorflow keras keras-layer

我有两个文件-model.pypredict.py。我在模型文件中使用stack中的tensorflow,然后训练模型并将其保存到jason。当我尝试将模型加载到预测文件中时,它给我一个错误。

模型文件可以完美运行,这是model.py中的一段代码:

import pandas as pd
import tensorflow as tf
from ast import literal_eval
from keras.models import Model, model_from_json
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers import *#Flatten, Dense, Lambda, SimpleRNN
from keras.optimizers import SGD

def make_model():
  ## layers
  out = Lambda(lambda x: tf.stack([x[0], x[1]], axis=1), output_shape=(2, 20))([input1, input2])
  ## few more layers
  sgd = SGD(lr = 0.1)
  model.compile(loss = "binary_crossentropy", optimizer = sgd, metrics = ["accuracy"])    
  return model

model = make_model()
model.fit(x_train, y_train, epochs = 10, batch_size = 25, verbose = 2)

## saving the model
model_json = model.to_json()

with open("/home/yamini/model.json", "w") as json_file:
    json_file.write(model_json)

model.save_weights("/home/yamini/model.h5")
print("Saved model to disk")

predict.py看起来像这样:

import tensorflow as tf
from keras.models import Sequential
from keras.layers import *
from keras.models import model_from_json
from keras.backend import stack
from keras.optimizers import SGD

# load json and create model
json_file = open('/home/yamini/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("/home/yamini/model.h5")
print("Loaded model from disk")

当我尝试运行predict.py时,出现以下错误:

Using TensorFlow backend.
Traceback (most recent call last):
   File "predict.py", line 12, in <module>
   loaded_model = model_from_json(loaded_model_json)
   File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/engine/saving.py", line 492, in model_from_json
   return deserialize(config, custom_objects=custom_objects)
   File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
   File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
   File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/engine/network.py", line 1032, in from_config
process_node(layer, node_data)
  File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/engine/network.py", line 991, in process_node
layer(unpack_singleton(input_tensors), **kwargs)
  File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/engine/base_layer.py", line 457, in __call__
  output = self.call(inputs, **kwargs)
  File "/home/yamini/virtual/home/yamini/virtual/lib/python3.6/site-packages/keras/layers/core.py", line 687, in call
  return self.function(inputs, **arguments)
  File "model.py", line 58, in <lambda>
  out = Lambda(lambda x: tf.stack([x[0], x[1]], axis=1), output_shape=(2, 20))([input1, input2])
  NameError: name 'tf' is not defined

1 个答案:

答案 0 :(得分:0)

该错误是由于未知的tf对象引起的。 以下更改可能会解决该问题。 在predict.py中,更改以下行: loaded_model = model_from_json(loaded_model_json) 至 : loaded_model = model_from_json(loaded_model_json, {"tf":tf})