0

I have a matrix (a Tensorflow variable) whose shape is determined dynamically. So I need to re-assign matrix while creating the graph. In python, we have options of resetting the graph or reassigning the variable by setting validate_shape to False.

Code Snippet:

Embedding matrix in tensorflow graph:

embedding_matrix = tf.get_variable("EMB_MATRIX",
                                        shape=[vocab_size, 300],
                                        dtype=tf.float32,
                                        initializer=tf.random_uniform_initializer(-0.1, 0.1, dtype=tf.float32),
                                        trainable=False)

Re-assigning the embedding matrix with new shape:

word_emb_matrix = np.insert(word_emb_matrix, vocab_size, np.arange(300), axis=0) # Inserting one row into embedding matrix
session.run(tf.assign(mtrain.embedding_matrix, word_emb_matrix, name="EMB_MATRIX", validate_shape=False)) #Assign embedding matrix with new shape

Now, when we use saved model/graph in Java, how can same functionality be achieved?

user3480922
  • 564
  • 1
  • 10
  • 22
  • Could you add some more detail to your question here? For example, when you say you have a "matrix", are you talking about a [Java `Tensor` object](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Tensor) or a node in the graph, i.e., a [Java `Output` object](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/Output), or something else? – ash Aug 18 '17 at 07:11
  • It's a node in the graph. So basically, I am trying to use a python-saved graph in Java. But, there's one node in the graph (a Tensorflow variable as a matrix) which needs to be re-assigned based on current dataset. Rest all graph remains same. Does this clear the question? – user3480922 Aug 18 '17 at 07:24
  • I'm not quite following. Is it possible for you to edit the question to include code snippets (e.g., Python code that does what you want and/or the Java code you have up to the point that you're stuck) – ash Aug 18 '17 at 07:30
  • Okay! I have updated the question with the code snippet about what is required to be achieved. – user3480922 Aug 18 '17 at 07:51

0 Answers0