I want to call this main(_)
function from another Python script without spawning a new process (so that it's easier to debug). However, that function is written to work with command line arguments. What would be the cleanest way call that function directly from another function?
Asked
Active
Viewed 531 times
2

Kamil Sindi
- 21,782
- 19
- 96
- 120

Fabian
- 990
- 3
- 11
- 24
-
Does this work? `import word2vec_optimized; word2vec_optimized.main(["your", "args", "here"])` – Nayuki Feb 28 '16 at 22:45
-
1I don't think it would work. The argument `_` in main seems dummy and ignored by the function. – Fabian Feb 28 '16 at 23:03
-
You're right - my bad. Interestingly, `_` gets overwritten in the `for` loop too. – Nayuki Feb 28 '16 at 23:33
1 Answers
4
You can import FLAGS
and then define the required args (train_data, eval_data, save_path).
In [13]: from tensorflow.models.embedding.word2vec_optimized import FLAGS
In [14]: from tensorflow.models.embedding.word2vec_optimized import main
In [16]: main(_)
--train_data --eval_data and --save_path must be specified.
An exception has occurred, use %tb to see the full traceback.
In [17]: FLAGS.train_data = "this"
In [18]: FLAGS.eval_data = "that"
In [19]: FLAGS.save_path = "some_path"
In [20]: main(_)
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 8

antikantian
- 610
- 1
- 4
- 11
-
Thanks. This works. `FLAGS` is a static variable so I need to be extra careful because I want to train two different models consecutively. – Fabian Feb 28 '16 at 23:04
-
By the way, what is your recommendation for resetting the flags value (for a different function call)? – Fabian Feb 28 '16 at 23:09
-
@Fabian I think you can just assign new values later on and the next call to main will use the new values. But, is there any reason why you don't just import Word2Vec and then init two instances, passing your own options to the class directly? – antikantian Feb 28 '16 at 23:16
-
@Fabian Also, somewhat related, and yet totally unrelated, [this](http://arxiv.org/abs/1511.06388) article by Trask et al. is an interesting spin on the word2vec model. – antikantian Feb 28 '16 at 23:20
-
For your question: to create a `Word2Vec` instance, I need to pass in an `Option` object. The creation of this object depends solely on `FLAGS`. So I still need to work around modifying the static `FLAGS` values. Thanks for the reference to the paper. I'll read it. – Fabian Feb 28 '16 at 23:25
-
@Fabian, right, but along with the two `Word2Vec` instances, you can have two options instances. Or, just import `Options` and subclass it, then redefine the three variables in `__init__` that you need to change. Import `FLAGS`, then do something like: `FLAGS1 = FLAGS; FLAGS2 = FLAGS; FLAGS2.train_data="training_data"`. In the first `__init__` you can have: `self.train_data = FLAGS2.train_data`, etc. That way you only need to reimplement what is needed, and the rest you can leave alone and they will take the options from `FLAGS`. – antikantian Feb 28 '16 at 23:31
-
Here's a gist so it's more readable. Note: I haven't tested this, but I think it should work with some modifications. [Gist](https://gist.github.com/antikantian/37784f06bb57b725c6ab) – antikantian Feb 28 '16 at 23:36