TensorFlow Estimator Prediction Error: Tensor Must Be From the Same Graph Issue

I have a TensorFlow code that involves using TFTransformOutput to transform raw features read from an existing recordIO of Tf.Examples and then feeding those transformed features into a warm started TensorFlow estimator.EstimatorV2 for predictions.

The entire process is managed within a single TensorFlow graph and session context to avoid graph mismatch errors potentially due to using both tf_v1 and tf_v2 in the code.

However, I keep encountering an error related to tensors being from different graphs. Below is the code and the error message I’m facing. I would really appreciate your help.

Trimmed code:

def main(argv: Sequence[str]) -> None:
...
  tf.compat.v1.disable_v2_behavior()

  def transform_features_fn(
      tf_transform_output,
      hparams,
      examples,
      used_features,
  ):
    """Creates an input function for serving based on ExampleMetadata."""
    feature_spec = tf_transform_output.raw_feature_spec()

    parsed_features = tf.io.parse_example(examples, feature_spec)
    transformed_features = tf_transform_output.transform_raw_features(
        parsed_features, drop_unused_features=True
    )
    return transformed_features

  # Define paths and parameters
  tf_transform_output_path = "/some/path"
  checkpoint_path = "some/path/2"
  testdata_filepattern = "some/path/3"

  # Load the transform output
  tf_transform_output = trainer_util.TFTransformOutput(tf_transform_output_path)

  # Define hyperparameters
  hparams = model_hparams.create_hparams()
  # Function to get records
  def get_records(records_list):
    for f in gfile.Glob(testdata_filepattern):
      with recordio.RecordReader(f) as rr:
        for record in rr:
          ex = example_pb2.Example()
          ex.ParseFromString(record)
          records_list.append(ex)
          return  # Return after one example for simplicity

  # Get records
  records_list = []
  get_records(records_list=records_list)

  # Define the graph context
  graph = tf.Graph()
  with graph.as_default():
    with tf.compat.v1.Session(graph=graph) as sess:
      # Serialize the example
      serialized_examples_tensor = tf.constant(
          [records_list[0].SerializeToString()]
      )  # Changed to batch of one serialized example
      # Transform features within the same graph
      transformed_features = transform_features_fn(
          tf_transform_output,
          hparams,
          serialized_examples_tensor,
          used_features,
      )

      # Define input function
      def input_fn():
        dataset = tf.data.Dataset.from_tensor_slices(transformed_features)
        dataset = dataset.batch(1)
        return dataset

      # Create the estimator within the same graph
      my_model = model.create_estimator(
          tf_transform_output,
          hparams,
          warm_start_from=checkpoint_path,
          model_dir=checkpoint_path,
      )
      predictions = my_model.predict(input_fn=input_fn)
      for prediction in predictions:
        print(prediction["predictions"])

Error Message:

E0603 04:04:15.467269 3556985 app.py:668] Top-level exception: Tensor(“batch_size:0”, shape=(), dtype=int64, device=/device:CPU:0) must be from the same graph as Tensor(“TensorSliceDataset:0”, shape=(), dtype=variant) (graphs are <tensorflow.python.framework.ops.Graph object at 0x51cc1ab7b9c0> and <tensorflow.python.framework.ops.Graph object at 0x51cc24427bc0>).
…….
raise ValueError(
ValueError: Tensor(“batch_size:0”, shape=(), dtype=int64, device=/device:CPU:0) must be from the same graph as Tensor(“TensorSliceDataset:0”, shape=(), dtype=variant) (graphs are <tensorflow.python.framework.ops.Graph object at 0x51cc1ab7b9c0> and <tensorflow.python.framework.ops.Graph object at 0x51cc24427bc0>).

I tried to put all the operations in a single TensorFlow graph and session context to avoid graph mismatch errors potentially but it didn’t work.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật