Skip to content

Instantly share code, notes, and snippets.

@EnisBerk
Created August 17, 2019 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EnisBerk/d092c4b0dd689f38b4c5e1eab20ace5c to your computer and use it in GitHub Desktop.
Save EnisBerk/d092c4b0dd689f38b4c5e1eab20ace5c to your computer and use it in GitHub Desktop.
tensor2tesnor tests
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "tensor2tesnor.ipynb",
"version": "0.3.2",
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "HeeXYtyF-yz5",
"colab_type": "code",
"outputId": "82f1ad29-4b87-4b32-9cfd-09887ce56f75",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
}
},
"source": [
"!pip install tensorflow-serving-api\n",
"!pip install tensor2tensor"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Collecting tensorflow-serving-api\n",
"\u001b[?25l Downloading https://files.pythonhosted.org/packages/24/c1/2b4ca53d699d79937c43d618de76f47db794364aeb3069349d97777678af/tensorflow_serving_api-1.14.0-py2.py3-none-any.whl (40kB)\n",
"\u001b[K |████████████████████████████████| 40kB 3.0MB/s \n",
"\u001b[?25hRequirement already satisfied: grpcio>=1.0<2 in /usr/local/lib/python3.6/dist-packages (from tensorflow-serving-api) (1.15.0)\n",
"Requirement already satisfied: tensorflow~=1.14.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-serving-api) (1.14.0)\n",
"Requirement already satisfied: protobuf>=3.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow-serving-api) (3.7.1)\n",
"Requirement already satisfied: six>=1.5.2 in /usr/local/lib/python3.6/dist-packages (from grpcio>=1.0<2->tensorflow-serving-api) (1.12.0)\n",
"Requirement already satisfied: tensorboard<1.15.0,>=1.14.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.14.0)\n",
"Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.11.2)\n",
"Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (0.33.4)\n",
"Requirement already satisfied: tensorflow-estimator<1.15.0rc0,>=1.14.0rc0 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.14.0)\n",
"Requirement already satisfied: google-pasta>=0.1.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (0.1.7)\n",
"Requirement already satisfied: numpy<2.0,>=1.14.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.16.4)\n",
"Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.1.0)\n",
"Requirement already satisfied: gast>=0.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (0.2.2)\n",
"Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (0.7.1)\n",
"Requirement already satisfied: keras-preprocessing>=1.0.5 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.1.0)\n",
"Requirement already satisfied: keras-applications>=1.0.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (1.0.8)\n",
"Requirement already satisfied: astor>=0.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow~=1.14.0->tensorflow-serving-api) (0.8.0)\n",
"Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.0->tensorflow-serving-api) (41.0.1)\n",
"Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.15.0,>=1.14.0->tensorflow~=1.14.0->tensorflow-serving-api) (3.1.1)\n",
"Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<1.15.0,>=1.14.0->tensorflow~=1.14.0->tensorflow-serving-api) (0.15.5)\n",
"Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from keras-applications>=1.0.6->tensorflow~=1.14.0->tensorflow-serving-api) (2.8.0)\n",
"Installing collected packages: tensorflow-serving-api\n",
"Successfully installed tensorflow-serving-api-1.14.0\n",
"Requirement already satisfied: tensor2tensor in /usr/local/lib/python3.6/dist-packages (1.11.0)\n",
"Requirement already satisfied: flask in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.1.1)\n",
"Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.12.0)\n",
"Requirement already satisfied: google-api-python-client in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.7.10)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (4.28.1)\n",
"Requirement already satisfied: h5py in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (2.8.0)\n",
"Requirement already satisfied: mesh-tensorflow in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (0.0.5)\n",
"Requirement already satisfied: gym in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (0.10.11)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.16.4)\n",
"Requirement already satisfied: bz2file in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (0.98)\n",
"Requirement already satisfied: gevent in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.4.0)\n",
"Requirement already satisfied: gunicorn in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (19.9.0)\n",
"Requirement already satisfied: sympy in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.1.1)\n",
"Requirement already satisfied: scipy in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.3.1)\n",
"Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (2.21.0)\n",
"Requirement already satisfied: tensorflow-probability in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (0.7.0)\n",
"Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (0.16.0)\n",
"Requirement already satisfied: oauth2client in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (4.1.3)\n",
"Requirement already satisfied: tfds-nightly in /usr/local/lib/python3.6/dist-packages (from tensor2tensor) (1.1.0.dev201908090105)\n",
"Requirement already satisfied: click>=5.1 in /usr/local/lib/python3.6/dist-packages (from flask->tensor2tensor) (7.0)\n",
"Requirement already satisfied: Jinja2>=2.10.1 in /usr/local/lib/python3.6/dist-packages (from flask->tensor2tensor) (2.10.1)\n",
"Requirement already satisfied: Werkzeug>=0.15 in /usr/local/lib/python3.6/dist-packages (from flask->tensor2tensor) (0.15.5)\n",
"Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python3.6/dist-packages (from flask->tensor2tensor) (1.1.0)\n",
"Requirement already satisfied: google-auth-httplib2>=0.0.3 in /usr/local/lib/python3.6/dist-packages (from google-api-python-client->tensor2tensor) (0.0.3)\n",
"Requirement already satisfied: httplib2<1dev,>=0.9.2 in /usr/local/lib/python3.6/dist-packages (from google-api-python-client->tensor2tensor) (0.11.3)\n",
"Requirement already satisfied: google-auth>=1.4.1 in /usr/local/lib/python3.6/dist-packages (from google-api-python-client->tensor2tensor) (1.4.2)\n",
"Requirement already satisfied: uritemplate<4dev,>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from google-api-python-client->tensor2tensor) (3.0.0)\n",
"Requirement already satisfied: pyglet>=1.2.0 in /usr/local/lib/python3.6/dist-packages (from gym->tensor2tensor) (1.4.1)\n",
"Requirement already satisfied: greenlet>=0.4.14; platform_python_implementation == \"CPython\" in /usr/local/lib/python3.6/dist-packages (from gevent->tensor2tensor) (0.4.15)\n",
"Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.6/dist-packages (from sympy->tensor2tensor) (1.1.0)\n",
"Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->tensor2tensor) (2.8)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->tensor2tensor) (2019.6.16)\n",
"Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->tensor2tensor) (1.24.3)\n",
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->tensor2tensor) (3.0.4)\n",
"Requirement already satisfied: cloudpickle>=0.6.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow-probability->tensor2tensor) (0.6.1)\n",
"Requirement already satisfied: decorator in /usr/local/lib/python3.6/dist-packages (from tensorflow-probability->tensor2tensor) (4.4.0)\n",
"Requirement already satisfied: pyasn1-modules>=0.0.5 in /usr/local/lib/python3.6/dist-packages (from oauth2client->tensor2tensor) (0.2.6)\n",
"Requirement already satisfied: rsa>=3.1.4 in /usr/local/lib/python3.6/dist-packages (from oauth2client->tensor2tensor) (4.0)\n",
"Requirement already satisfied: pyasn1>=0.1.7 in /usr/local/lib/python3.6/dist-packages (from oauth2client->tensor2tensor) (0.4.6)\n",
"Requirement already satisfied: protobuf>=3.6.1 in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (3.7.1)\n",
"Requirement already satisfied: psutil in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (5.4.8)\n",
"Requirement already satisfied: attrs in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (19.1.0)\n",
"Requirement already satisfied: termcolor in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (1.1.0)\n",
"Requirement already satisfied: promise in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (2.2.1)\n",
"Requirement already satisfied: absl-py in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (0.7.1)\n",
"Requirement already satisfied: wrapt in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (1.11.2)\n",
"Requirement already satisfied: dill in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (0.3.0)\n",
"Requirement already satisfied: tensorflow-metadata in /usr/local/lib/python3.6/dist-packages (from tfds-nightly->tensor2tensor) (0.14.0)\n",
"Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.6/dist-packages (from Jinja2>=2.10.1->flask->tensor2tensor) (1.1.1)\n",
"Requirement already satisfied: cachetools>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth>=1.4.1->google-api-python-client->tensor2tensor) (3.1.1)\n",
"Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.6.1->tfds-nightly->tensor2tensor) (41.0.1)\n",
"Requirement already satisfied: googleapis-common-protos in /usr/local/lib/python3.6/dist-packages (from tensorflow-metadata->tfds-nightly->tensor2tensor) (1.6.0)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "IkWcrHEZ-z9B",
"colab_type": "code",
"colab": {}
},
"source": [
"# t2t-query-server \\\n",
"# --server=localhost:9000 \\\n",
"# --servable_name=my_model \\\n",
"# --problem=translate_ende_wmt8k \\\n",
"# --data_dir=~/t2t/data"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tb3GSo22AOIL",
"colab_type": "code",
"colab": {}
},
"source": [
"import requests\n",
"import json"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "LY47_KBnAUlu",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "MUp4Ou46IqyK",
"colab_type": "code",
"colab": {}
},
"source": [
"from __future__ import absolute_import\n",
"from __future__ import division\n",
"from __future__ import print_function\n",
"\n",
"import os\n",
"\n",
"from oauth2client.client import GoogleCredentials\n",
"from six.moves import input # pylint: disable=redefined-builtin\n",
"\n",
"from tensor2tensor import problems as problems_lib # pylint: disable=unused-import\n",
"from tensor2tensor.serving import serving_utils\n",
"from tensor2tensor.utils import registry\n",
"from tensor2tensor.utils import usr_dir\n",
"import tensorflow as tf\n",
"\n",
"\n",
"from tensor2tensor.data_generators import text_encoder\n",
"\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "brBk1y0ZMC2h",
"colab_type": "code",
"colab": {}
},
"source": [
"import numpy as np"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "_3t6IoebOVic",
"colab_type": "code",
"colab": {}
},
"source": [
"problem_name='summarize_cnn_dailymail32k'\n",
"problem = registry.problem(problem_name)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "JVk6e-SOKvnk",
"colab_type": "code",
"colab": {}
},
"source": [
"# encoder1=problem.feature_encoders(\"./\")[\"inputs\"]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "PcbiuihHLCnd",
"colab_type": "code",
"colab": {}
},
"source": [
"# encoder2=problem.feature_info[fname].encoder"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "XWBIQo58LOKY",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "f803bf10-3e07-443c-d505-7816ab62eaba"
},
"source": [
"input_text=\"testing our model\"\n",
"inputs_list=[input_text]\n"
],
"execution_count": 76,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"instances\" : [[[2807],\\n [ 149],\\n [1146]]] }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 76
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "FWUpK52mO0vq",
"colab_type": "code",
"outputId": "b722d69c-ba13-4175-c701-4a8c23105fff",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 275
}
},
"source": [
"!mkdir tmp\n",
"!mkdir data\n",
"!wget \"https://storage.googleapis.com/tensor2tensor-data/vocab.summarize_cnn_dailymail32k.32768.subwords\"\n"
],
"execution_count": 19,
"outputs": [
{
"output_type": "stream",
"text": [
"mkdir: cannot create directory ‘tmp’: File exists\n",
"mkdir: cannot create directory ‘data’: File exists\n",
"--2019-08-17 00:47:45-- https://storage.googleapis.com/tensor2tensor-data/vocab.summarize_cnn_dailymail32k.32768.subwords\n",
"Resolving storage.googleapis.com (storage.googleapis.com)... 74.125.141.128, 2607:f8b0:400c:c06::80\n",
"Connecting to storage.googleapis.com (storage.googleapis.com)|74.125.141.128|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 300745 (294K) [application/octet-stream]\n",
"Saving to: ‘vocab.summarize_cnn_dailymail32k.32768.subwords.1’\n",
"\n",
"vocab.summarize_cnn 100%[===================>] 293.70K --.-KB/s in 0.003s \n",
"\n",
"2019-08-17 00:47:45 (101 MB/s) - ‘vocab.summarize_cnn_dailymail32k.32768.subwords.1’ saved [300745/300745]\n",
"\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"HParams([('batch_size_multiplier', 1), ('input_space_id', 0), ('loss_multiplier', 1.0), ('modality', {'targets': <tensor2tensor.layers.modalities.SymbolModality object at 0x7f28a556a4a8>, 'inputs': <tensor2tensor.layers.modalities.SymbolModality object at 0x7f28a556a3c8>}), ('stop_at_eos', 1), ('target_space_id', 0), ('vocabulary', {'targets': <tensor2tensor.data_generators.text_encoder.SubwordTextEncoder object at 0x7f28a5715940>, 'inputs': <tensor2tensor.data_generators.text_encoder.SubwordTextEncoder object at 0x7f28a5715940>}), ('was_copy', False), ('was_reversed', False)])"
]
},
"metadata": {
"tags": []
},
"execution_count": 19
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Aj_RRduLC7Dw",
"colab_type": "code",
"colab": {}
},
"source": [
"hparams = tf.contrib.training.HParams(\n",
" data_dir=os.path.expanduser(\"./\"))\n",
"problem.get_hparams(hparams)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "94tiNeTLBmTX",
"colab_type": "code",
"colab": {}
},
"source": [
"# def _encode(inputs, encoder, add_eos=True):\n",
"# input_ids = encoder.encode(inputs)\n",
"# if add_eos:\n",
"# input_ids.append(text_encoder.EOS_ID)\n",
"# input_ids=tf.reshape(inputs, [1, -1, 1])\n",
"# return input_ids\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "28AohVI3O1sa",
"colab_type": "code",
"outputId": "ae5da80d-6a51-403b-cebb-67daab42b267",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
}
},
"source": [
"# fname = \"inputs\" if problem.has_inputs else \"targets\"\n",
"# print(fname)\n",
"# input_encoder = problem.feature_info[fname].encoder\n",
"# print(input_encoder)\n",
"# input_ids_list = [\n",
"# _encode(inputs, input_encoder, add_eos=problem.has_inputs)\n",
"# for inputs in inputs_list\n",
"# ]\n",
"# print(input_ids_list)\n",
"# examples = [_make_example(input_ids, problem, fname)\n",
"# for input_ids in input_ids_list]"
],
"execution_count": 35,
"outputs": [
{
"output_type": "stream",
"text": [
"inputs\n",
"<tensor2tensor.data_generators.text_encoder.SubwordTextEncoder object at 0x7f28a5715940>\n",
"[[2807, 149, 1146, 1]]\n",
"{'targets': VarLenFeature(dtype=tf.int64), 'inputs': VarLenFeature(dtype=tf.int64)} None\n",
"targets\n",
"VarLenFeature(dtype=tf.int64)\n",
"<dtype: 'int64'>\n",
"inputs\n",
"VarLenFeature(dtype=tf.int64)\n",
"<dtype: 'int64'>\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "oDtI9kLmBjCZ",
"colab_type": "code",
"colab": {}
},
"source": [
"# examples"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9RXA1mtWP9OT",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "J-06yCqFQ8eC",
"colab_type": "code",
"outputId": "d9f1a2f6-50df-4fba-9503-8d9d3d321769",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 224
}
},
"source": [
""
],
"execution_count": 0,
"outputs": [
{
"output_type": "stream",
"text": [
"--2019-08-16 17:07:39-- https://storage.googleapis.com/tensor2tensor-data/vocab.summarize_cnn_dailymail32k.32768.subwords\n",
"Resolving storage.googleapis.com (storage.googleapis.com)... 74.125.141.128, 2607:f8b0:400c:c06::80\n",
"Connecting to storage.googleapis.com (storage.googleapis.com)|74.125.141.128|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 300745 (294K) [application/octet-stream]\n",
"Saving to: ‘vocab.summarize_cnn_dailymail32k.32768.subwords’\n",
"\n",
"\r vocab.sum 0%[ ] 0 --.-KB/s \rvocab.summarize_cnn 100%[===================>] 293.70K --.-KB/s in 0.003s \n",
"\n",
"2019-08-16 17:07:39 (91.3 MB/s) - ‘vocab.summarize_cnn_dailymail32k.32768.subwords’ saved [300745/300745]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "NDxPWMYbRQ4y",
"colab_type": "code",
"colab": {}
},
"source": [
"# def _make_example(input_ids, problem, input_feature_name=\"inputs\"):\n",
"# \"\"\"Make a tf.train.Example for the problem.\n",
"# features[input_feature_name] = input_ids\n",
"# Also fills in any other required features with dummy values.\n",
"# Args:\n",
"# input_ids: list<int>.\n",
"# problem: Problem.\n",
"# input_feature_name: name of feature for input_ids.\n",
"# Returns:\n",
"# tf.train.Example\n",
"# \"\"\"\n",
"# features = {\n",
"# input_feature_name:\n",
"# tf.train.Feature(int64_list=tf.train.Int64List(value=input_ids))\n",
"# }\n",
"\n",
"# # Fill in dummy values for any other required features that presumably\n",
"# # will not actually be used for prediction.\n",
"# data_fields, _ = problem.example_reading_spec()\n",
"# print(data_fields,_)\n",
"# for fname, ftype in data_fields.items():\n",
"# print(fname)\n",
"# print(ftype)\n",
"# print(ftype.dtype)\n",
"# if fname == input_feature_name:\n",
"# continue\n",
"# if not isinstance(ftype, tf.FixedLenFeature):\n",
"# # Only FixedLenFeatures are required\n",
"# continue\n",
"# if ftype.default_value is not None:\n",
"# # If there's a default value, no need to fill it in\n",
"# continue\n",
"# num_elements = functools.reduce(lambda acc, el: acc * el, ftype.shape, 1)\n",
"# if ftype.dtype in [tf.int32, tf.int64]:\n",
"# value = tf.train.Feature(\n",
"# int64_list=tf.train.Int64List(value=[0] * num_elements))\n",
"# if ftype.dtype in [tf.float32, tf.float64]:\n",
"# value = tf.train.Feature(\n",
"# float_list=tf.train.FloatList(value=[0.] * num_elements))\n",
"# if ftype.dtype == tf.bytes:\n",
"# value = tf.train.Feature(\n",
"# bytes_list=tf.train.BytesList(value=[\"\"] * num_elements))\n",
"# tf.logging.info(\"Adding dummy value for feature %s as it is required by \"\n",
"# \"the Problem.\", fname)\n",
"# features[fname] = value\n",
"# return tf.train.Example(features=tf.train.Features(feature=features))\n",
"\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "qpSb_6SkRRlE",
"colab_type": "code",
"colab": {}
},
"source": [
"# r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', json={\"instances\": examples[0]})\n",
"# r.text"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "n9qxZk7QaiR0",
"colab_type": "code",
"colab": {}
},
"source": [
"# import base64\n",
"\n",
"# input_data = {\n",
"# \"instances\": [{\n",
"# \"input\": {\n",
"# \"b64\": base64.b64encode(ex.SerializeToString())\n",
"# }\n",
"# } for ex in examples]\n",
"# }"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "luRevhSNapW0",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "rXKBsH4icLtP",
"colab_type": "code",
"colab": {}
},
"source": [
"# r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', json=input_data)\n",
"# r.text"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "nwBwW1LXcPIM",
"colab_type": "code",
"colab": {}
},
"source": [
"# a=tf.contrib.util.make_tensor_proto(\n",
"# [ex.SerializeToString() for ex in examples], shape=[len(examples)])"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "3EQV5RfpdHb2",
"colab_type": "code",
"colab": {}
},
"source": [
"# r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', json={\"instances\": [\"\\n\\027\\n\\025\\n\\006inputs\\022\\013\\032\\t\\n\\007\\367\\025\\225\\001\\372\\010\\001\"]})\n",
"# r.text"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "HBYaE9UedVz3",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "utyt23i0dX1n",
"colab_type": "code",
"colab": {}
},
"source": [
"# a=[ex.SerializeToString() for ex in examples]"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "w8teVfOndeC4",
"colab_type": "code",
"outputId": "aeccf4cd-1ff4-4b8d-dcb1-f224c08e881c",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"input_ids_list"
],
"execution_count": 107,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[[2807, 149, 1146, 1]]"
]
},
"metadata": {
"tags": []
},
"execution_count": 107
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "C7ZNCR0AdgJg",
"colab_type": "code",
"outputId": "b774f80a-be36-46ce-d161-abc5f02bc0e3",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
}
},
"source": [
"json_request = '{{ \"instances\" : {} }}'.format(''.join([str(e) for e in input_ids_list]))\n",
"json_request = '{{ \"instances\" : {} }}'.format(np.array2string(np.array(input_ids_list[0]), separator=',', formatter={'float':lambda x: \"%.1f\" % x}))\n",
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict',data=json_request)\n",
"r.text"
],
"execution_count": 105,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Failed to process element: 0 of \\\\\\'instances\\\\\\' list. Error: Invalid argument: JSON Value: 2807 Type: Number is not of expected type: string\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 105
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ON_iKSvahBY5",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "-RkeUXSchPsz",
"colab_type": "code",
"outputId": "bb7b7f11-65e4-4eb6-9e01-c316024bc041",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" json={\"instances\":[{\"input\":[['2807', '149', '1146', '1']]}]})\n",
"r.text"
],
"execution_count": 110,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Incompatible shapes at component 0: expected [1,4] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 110
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "KBbWGRo9jHDo",
"colab_type": "code",
"outputId": "04e82f50-4923-4da4-c19d-f0004188f15a",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" json={\"instances\":[[\"2807\", \"149\", \"1146\"]]})\n",
"r.text"
],
"execution_count": 111,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Incompatible shapes at component 0: expected [3] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 111
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TU99au7GID8O",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "f4f7933e-9d8a-477a-9aff-1c815a8ae860"
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" json={\"instances\":[[[\"2807\"], [\"149\"], [\"1146\"]]]})\n",
"r.text"
],
"execution_count": 112,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Incompatible shapes at component 0: expected [3,1] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 112
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "u7-kRbLho3qP",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "52be7859-4da1-4f26-fc55-23e153a92193"
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" json={\"instances\":[[]]})\n",
"r.text"
],
"execution_count": 113,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Incompatible shapes at component 0: expected [0] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 113
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "dc5D8VjFIjot",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"outputId": "c7630e60-18d7-4713-bad4-344b6e52aef1"
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" json={\"instances\":[[]]})\n",
"r.text"
],
"execution_count": 114,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"2 root error(s) found.\\\\n (0) Invalid argument: Incompatible shapes at component 0: expected [0] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\\\\n (1) Invalid argument: Incompatible shapes at component 0: expected [0] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\\\\n\\\\t [[transformer/Shape/_423]]\\\\n0 successful operations.\\\\n0 derived errors ignored.\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 114
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "pw31ijOPJq9d",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"outputId": "7f592bc6-9277-45c6-ebf7-0bd3e5713970"
},
"source": [
"# tf.reshape(encoders[\"inputs\"].encode(input_str) + [1], [1, -1, 1, 1])\n",
"\n",
"inputs=encoder2.encode(input_text)\n",
"inputs=np.array(inputs)\n",
"inputs=np.reshape(inputs, [1, -1, 1])\n",
"print(inputs)\n",
"json_request = '{{ \"instances\" : {} }}'.format(np.array2string(inputs, separator=',', formatter={'int':lambda x: \"\\\"{}\\\"\".format(x)}))\n",
"json_request"
],
"execution_count": 115,
"outputs": [
{
"output_type": "stream",
"text": [
"[[[2807]\n",
" [ 149]\n",
" [1146]]]\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"instances\" : [[[\"2807\"],\\n [\"149\"],\\n [\"1146\"]]] }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 115
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "zSWTcvBHNFbO",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "668f0e40-a6ba-4971-e71f-95ac8c619757"
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" data=json_request)\n",
"r.text"
],
"execution_count": 117,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Incompatible shapes at component 0: expected [3,1] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 117
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Zp48xE6ENIUh",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 119
},
"outputId": "b13d1ea0-0ce9-4cc5-f239-b0c07e15db88"
},
"source": [
"# tf.reshape(encoders[\"inputs\"].encode(input_str) + [1], [1, -1, 1, 1])\n",
"\n",
"inputs=encoder2.encode(input_text)\n",
"# tf.reshape(inputs, [1, -1, 1])\n",
"inputs=np.array(inputs)\n",
"inputs=np.reshape(inputs, [1, -1, 1, 1])\n",
"print(inputs)\n",
"json_request = '{{ \"instances\" : {} }}'.format(np.array2string(inputs, separator=',', formatter={'int':lambda x: \"\\\"{}\\\"\".format(x)}))\n",
"json_request"
],
"execution_count": 118,
"outputs": [
{
"output_type": "stream",
"text": [
"[[[[2807]]\n",
"\n",
" [[ 149]]\n",
"\n",
" [[1146]]]]\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"instances\" : [[[[\"2807\"]],\\n\\n [[\"149\"]],\\n\\n [[\"1146\"]]]] }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 118
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TERPIzIDOMDj",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "03e43c26-868f-4a90-86dd-3d6b773302bf"
},
"source": [
"r = requests.post('https://services.paperspace.io/model-serving/desfnnrqt1v633v:predict', \n",
" data=json_request)\n",
"r.text"
],
"execution_count": 119,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'{ \"error\": \"Incompatible shapes at component 0: expected [3,1,1] but got [].\\\\n\\\\t [[{{node TensorSliceDataset}}]]\" }'"
]
},
"metadata": {
"tags": []
},
"execution_count": 119
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "Gv0hWUvWR2L6",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
@jaredscheib
Copy link

"name": "tensor2tesnor.ipynb" -- typo on filename here matter?

@EnisBerk
Copy link
Author

No, it does not. Misha said probably there is a problem with deployed model so I am not sure whats the problem. I will start from deploying model myself if I get time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment