Skip to content
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

loading pre-trained model #1

Open
avieini opened this issue Nov 21, 2018 · 4 comments
Open

loading pre-trained model #1

avieini opened this issue Nov 21, 2018 · 4 comments

Comments

@avieini
Copy link

avieini commented Nov 21, 2018

Hi,
I'm trying to load a pretrained model and use it with my own dataset.
How can I do it?

thanks

@MirunaPislar
Copy link
Owner

Hi,
Which if the models are you trying to load? tf_attention, dnn_models or some other model?

@avieini
Copy link
Author

avieini commented Nov 21, 2018

Thanks for the quick answer!
I basically want to run the tf_attention model on my data and visualize the words that are more relevant than other to the discrimination (as in your example ).
my data is a csv file with one columns for the sentence and another binary column indicating if the sentence is sarcastic or not

@MirunaPislar
Copy link
Owner

MirunaPislar commented Nov 21, 2018

Hey. So I've just checked and it should work if you simply clone the repo (you will need all code and the tf_attention model) and just run python3 visualize_tf_attention.py from inside the src/ directory. This will automatically load the tf_attention model specified by MODEL_PATH (by default this is the one that I provided in models/tf_attention).

If you want to use your own data, then place it under /res/tokens. Make sure you rename your files as "tokens_clean_original_train.txt" and "tokens_clean_original_test.txt" and format them in the same way as my files in order to get the visualizations for your data and not mine. After running this, the visualizations will be automatically saved as "attention_vis.html" under the directory plots/html_visualizations/.

More generally, if you want to re-use the model without necessarily using my code, just download the full tf_attention model (link here) and then this would definitely work to restore (any) pre-trained model (you'll need to specify the MODEL_PATH):
`
graph = tf.Graph()

with graph.as_default():

    sess = tf.Session()

    with sess.as_default():

            # Load the saved meta graph and restore variables

            saver = tf.train.import_meta_graph("{}.meta".format(MODEL_PATH))

            sess.run(tf.global_variables_initializer())

            sess.run(tf.local_variables_initializer())

            saver.restore(sess, MODEL_PATH)

            print("Model file restored.")`

Then you can re-adapt my code to get the visualizations you want :) But the first approach would be way faster. Hope this helped!

@avieini
Copy link
Author

avieini commented Nov 22, 2018

Hi thank again,
I did as you wrote (the first option) but i'm getting the following error:

Traceback (most recent call last):
File "/home/dave/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/home/dave/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1319, in _run_fn
options, feed_dict, fetch_list, target_list, run_metadata)
File "/home/dave/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1407, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Assign requires shapes of both tensors to match. lhs shape= [5382,100] rhs shape= [42912,100]
[[{{node save/Assign_11}} = Assign[T=DT_FLOAT, _class=["loc:@Embedding_layer/Variable"], use_locking=true, validate_shape=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](Embedding_layer/Variable/Adam_1, save/RestoreV2:11)]]

apparently, something went wrong with the pretrained model and it's graph and my data.
If you have any idea how to overcome it I would be very glad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants