| 245 | } |
| 246 | |
| 247 | func GetVanillaPythonCode() string { |
| 248 | return `import json |
| 249 | import os |
| 250 | import logging |
| 251 | import sys |
| 252 | |
| 253 | import tensorflow as tf |
| 254 | import numpy as np |
| 255 | |
| 256 | os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # ERROR |
| 257 | os.environ["CUDA_VISIBLE_DEVICES"] = "-1" |
| 258 | logging.getLogger('tensorflow').setLevel(logging.ERROR) |
| 259 | logging.disable(logging.WARNING) |
| 260 | |
| 261 | with open(sys.argv[1], "r") as f: |
| 262 | config = json.load(f) |
| 263 | |
| 264 | print("Loading Vanilla model") |
| 265 | |
| 266 | model = tf.keras.models.load_model(config["model_dir"]) |
| 267 | |
| 268 | learn_input_signature = [ |
| 269 | tf.TensorSpec(shape=(None, 1), dtype=tf.int32), |
| 270 | tf.TensorSpec(shape=None, dtype=tf.float32), |
| 271 | ] |
| 272 | predict_input_signature = [] |
| 273 | |
| 274 | zero_inputs = [] |
| 275 | |
| 276 | for model_layer in model.inputs: |
| 277 | input_shape = [1] |
| 278 | for dim in model_layer.shape[1:]: |
| 279 | input_shape.append(dim) |
| 280 | zero_inputs.append( |
| 281 | tf.zeros(shape=input_shape, dtype=model_layer.dtype) |
| 282 | ) |
| 283 | learn_input_signature.append(tf.TensorSpec( |
| 284 | shape=model_layer.shape, |
| 285 | dtype=model_layer.dtype, |
| 286 | )) |
| 287 | predict_input_signature.append(tf.TensorSpec( |
| 288 | shape=model_layer.shape, |
| 289 | dtype=model_layer.dtype, |
| 290 | )) |
| 291 | |
| 292 | evaluate_input_signature = learn_input_signature |
| 293 | |
| 294 | |
| 295 | class GolangModel(tf.Module): |
| 296 | def __init__(self): |
| 297 | super().__init__() |
| 298 | |
| 299 | self._model = model |
| 300 | |
| 301 | self._global_step = tf.Variable(0, dtype=tf.int32, trainable=False) |
| 302 | opt = tf.keras.optimizers.get(config["optimizer"]["class_name"]) |
| 303 | self._optimizer = opt.from_config(config["optimizer"]["config"]) |
| 304 | loss_func = None |