예제 #1
0
  def test_embedding_lookup_shape(self):

    def _evaluate(tensors, feed_dict):
      sess = ops.get_default_session()
      if sess is None:
        with self.test_session() as sess:
          return sess.run(tensors, feed_dict=feed_dict)
      else:
        return sess.run(tensors, feed_dict=feed_dict)

    with self.session(use_gpu=test_util.is_gpu_available(),
                      config=default_config):
      default_val = -1

      keys = constant_op.constant([0, 1, 2], dtypes.int64)
      values = constant_op.constant([[0, 0, 0], [1, 1, 1], [2, 2, 2]],
                                    dtypes.int32)
      table = de.get_variable("t140",
                              dtypes.int64,
                              dtypes.int32,
                              dim=3,
                              initializer=default_val)
      self.evaluate(table.upsert(keys, values))
      self.assertAllEqual(3, self.evaluate(table.size()))

      # shape of ids is fully defined
      ids = constant_op.constant([[0, 1], [2, 4]], dtypes.int64)
      embeddings = de.embedding_lookup(table, ids)
      self.assertAllEqual([2, 2, 3], embeddings.get_shape())
      re = self.evaluate(embeddings)
      self.assertAllEqual([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [-1, -1, -1]]],
                          re)

      # shape of ids is partially defined
      ids = gen_array_ops.placeholder(shape=(2, None), dtype=dtypes.int64)
      embeddings = de.embedding_lookup(table, ids)
      self.assertFalse(embeddings.get_shape().is_fully_defined())
      re = _evaluate(
          embeddings,
          feed_dict={ids: np.asarray([[0, 1], [2, 4]], dtype=np.int64)})
      self.assertAllEqual([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [-1, -1, -1]]],
                          re)

      # shape of ids is unknown
      ids = gen_array_ops.placeholder(dtype=dtypes.int64)
      embeddings = de.embedding_lookup(table, ids)
      self.assertEqual(embeddings.get_shape(), tensor_shape.unknown_shape())
      re = _evaluate(
          embeddings,
          feed_dict={ids: np.asarray([[0, 1], [2, 4]], dtype=np.int64)})
      self.assertAllEqual([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [-1, -1, -1]]],
                          re)
예제 #2
0
def _prepare_signature_inputs(layers: dict, dtype_layer, model_keys):
    signature = {}
    for key, value in model_keys.items():
        if value in layers.keys():
            x = gen_array_ops.placeholder(dtype=dtype_layer,
                                          shape=layers[value],
                                          name=value)
            x_tensor_info = build_tensor_info(x)
            signature[key] = x_tensor_info
    return signature
def _prepare_signature(layers: dict, model_keys):
    signature = {}
    for key, value in model_keys.items():
        if value in layers.keys():
            x = gen_array_ops.placeholder(
                dtype=type_mapping[layers[value].precision],
                shape=layers[value].shape,
                name=value)
            x_tensor_info = build_tensor_info(x)
            signature[key] = x_tensor_info
    return signature
예제 #4
0
def _prepare_signature_outputs(names: list, dtype_layer, shape, model_keys):
    """
    Inference Engine Api does not report outputs shapes,
    for signature format consistency with TF Serving theres returned a
    dummy shape==(1,1,1).
    To be fixed in the future.
    """
    signature = {}
    for key, value in model_keys.items():
        if value in names:
            x = gen_array_ops.placeholder(dtype=dtype_layer,
                                          shape=shape,
                                          name=value)
            x_tensor_info = build_tensor_info(x)
            signature[key] = x_tensor_info
    return signature
예제 #5
0
def placeholder(dtype, shape=None, name=None):
    if context.executing_eagerly():
        raise RuntimeError("tf.placeholder() is not compatible with "
                           "eager execution.")

    return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
예제 #6
0
 def add_op_to_graph(num_ops):
     with func_graph.FuncGraph("add").as_default():
         a = gen_array_ops.placeholder(dtypes.float32)
         b = gen_array_ops.placeholder(dtypes.float32)
         for _ in range(num_ops):
             gen_math_ops.add(a, b)