-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tensorflowjs_converter NotImplementedError #3381
Comments
@fawazahmed0 please follow the instructions here to convert your model : https://github.com/tensorflow/tfjs/tree/master/tfjs-converter |
@rthadur , Thanks for replying, I ran the tensorflowjs_wizard, and this is the error I got:
And this is the log file it generated:
|
@rthadur And one more thing, I am currently using this: https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1 as my word embedding layer in the code and I assume this error might be in someway related to the word embedding layer |
I am pretty much sure that this error is related to the tfhub word embedding layer, because I created a word embedding layer from scratch using GloVe dataset and the tensorflow converter script seems to convert the model into js format with no issues. But I am not sure why when I use tfhub word embedding layers, it throws error. I even tried different embedding layer such as this: https://tfhub.dev/google/collections/universal-sentence-encoder/1 and even that doesn't seem to work |
@fawazahmed0 can you share a code that does not reference your local data files? |
@pyu10055 ,here is the code that I made specifically made for debugging this issue, #import json
from urllib.request import urlopen
import pandas as pd
import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
import glob
import os
from tensorflow import keras
transData = []
allTransData = []
allTransTarget = []
# https://cdn.jsdelivr.net/gh/fawazahmed0/LargeTxtFile@latest/qdata/en.sahih.txt
url = urlopen('https://cdn.jsdelivr.net/gh/fawazahmed0/LargeTxtFile@latest/qdata/en.sahih.txt')
# https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines
# https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
# Reading all the translations in translationDir
transData.append(url.read().splitlines())
# Adding all translations to allTrans
for i in transData:
count = 0
for j in i:
#https://stackoverflow.com/questions/37016946/remove-b-character-do-in-front-of-a-string-literal-in-python-3
allTransData.append(j.decode("utf-8"))
allTransTarget.append(count)
count+=1
# https://stackoverflow.com/questions/51125266/how-do-i-split-tensorflow-datasets
DATASET_SIZE = len(allTransData)
train_size = int(0.7 * DATASET_SIZE)
val_size = int(0.15 * DATASET_SIZE)
test_size = int(0.15 * DATASET_SIZE)
full_dataset = tf.data.Dataset.from_tensor_slices((allTransData, allTransTarget))
# shuffling the data, it increases accuracy
full_dataset = full_dataset.shuffle(len(allTransData))
train_dataset = full_dataset.take(train_size)
test_dataset = full_dataset.skip(train_size)
validation_dataset = test_dataset.skip(test_size)
test_dataset = test_dataset.take(test_size)
# we are not shuffling data, but we will try shuffling the whole data
# keeping batch size low will increase the accuracy, and keeping it high will decrease the accuracy
# batch size low will take hours to train and large batch size will take minutes to train
batch_size = 5000
train_dataset = train_dataset.batch(batch_size)
test_dataset = test_dataset.batch(batch_size)
validation_dataset = validation_dataset.batch(batch_size)
# trainable means the weights in this layer will get updated during training
#embedding = hub.load("C:/Users/Nawaz/Desktop/qadev/js/nlm128")
#https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1
embedding = hub.load("https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1")
# print(embedding(["cat is on the mat", "dog is in the fog"]))
hub_layer = hub.KerasLayer(embedding, input_shape=[],
dtype=tf.string, trainable=True)
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(100, activation='relu'))
model.add(tf.keras.layers.Dense(6236))
# New add softmax layer as output while training, it will effect in loss calculation, model might not train well
# model.add(tf.keras.layers.Dense(6236, activation='softmax'))
model.summary()
# https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/keras/regression.ipynb#scrollTo=LcopvQh3X-kX
# https://keras.io/api/callbacks/
# we will stop ,if the loss does not change after n epochs , patience stores the n variable
# don't remove this assuming tuner will take care of epochs, we have to stop by using earlystopping to avoid unecessary epochs
early_stop = [ tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5) ]
# if target or label is integer values i.e 1,2,3 use SparseCategoricalCrossentropy, else if they are one hot encoded [1,0,0][0,1,0][0,0,1] use categorical_crossentropy
# https://jovianlin.io/cat-crossentropy-vs-sparse-cat-crossentropy/
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(train_dataset, epochs=1, validation_data=validation_dataset, verbose=1, callbacks=early_stop)
model.evaluate(test_dataset, verbose=2)
model.save('models/debug') Just in case you still need the saved model, here it is: Thanks |
The error seems to be a known issue of TF |
The solution in the stackoverflow link seems to be using custom layer, and tensorflow.js doesn't support custom layers yet. Anyways, thanks for looking into this |
@fawazahmed0 If you converted the model to custom layer, you can convert the saved model to TFJS GraphModel for inference. (LayersModel does not support custom layer, but GraphModel may) |
@pyu10055 , oh wait, I assume you were looking at first error log, this the log you should look at: And also one more thing, the problem is not in my code, but rather the word embedding layer (TF Hub Module) and specifically the word embedding layers which doesn't require preprocessing the input, we can feed variable length string into it. To reproduce this issue, just try converting any TF Hub text embedding model (SavedModel 2.0 format) which can take variable length string as input, it would throw some signature error. You can try converting this into js using tensorflowjs_converter: |
Tensorflowjs Python Package Version
1.7.4.post1
Operating System
Windows 7
Describe the problem or feature request
Hi, I trained my model assuming I would be able to convert it into javascript, but I didn't knew that only few operations are supported in tensorflowjs.
If you could help me to remove the unsupported operation from my code , that would be great.
I would like to convert my model saved in SavedModel format
This is what I am getting(probability is the folder containing my SavedModel and tfjs is the output dir)
I even tried to convert using python:
and this is the error I got:
Code to reproduce the bug / link to feature request
Here is the code which was used to build the model:
Here is the list of python packages and their version installed in my pc:
The text was updated successfully, but these errors were encountered: