How to use tf.contrib.copy_graph.copy_op_to_graph()?Changing the current graph of tf.placeholder objects in Tensorflow: Is it possible?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How do I sort a dictionary by value?How do I list all files of a directory?How do I import modules in pycharm?label_keys type error on DNNCLassifier Tensorflowtf.estimator.inputs.pandas_input_fn throws _NumericColumn' object has no attribute 'insert_transformed_featureTensorflow implementing crf loss
What caused the tendency for conservatives to not support climate change regulations?
Team member doesn't give me the minimum time to complete a talk
Are UK pensions taxed twice?
What are the slash markings on Gatwick's 08R/26L?
Points within polygons in different projections
Infinitely many hats
Can non-English-speaking characters use wordplay specific to English?
Can a non-EU citizen travel within the Schengen area without identity documents?
Why is there a need to modify system call tables in linux?
etoolbox: AtBeginEnvironment is not At Begin Environment
Why do Russians call their women expensive ("дорогая")?
A "distinguishing" family of subsets
Adding strings in lists together
Do creatures all have the same statistics upon being reanimated via the Animate Dead spell?
Did airlines fly their aircraft slower in response to oil prices in the 1970s?
The qvolume of an integer
How to detach yourself from a character you're going to kill?
Get LaTeX form from step by step solution
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
Understanding STM32 datasheet regarding decoupling capacitors
How should I push back against my job assigning "homework"?
Is floating in space similar to falling under gravity?
Why would Lupin kill Pettigrew?
What is the difference between nullifying your vote and not going to vote at all?
How to use tf.contrib.copy_graph.copy_op_to_graph()?
Changing the current graph of tf.placeholder objects in Tensorflow: Is it possible?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How do I sort a dictionary by value?How do I list all files of a directory?How do I import modules in pycharm?label_keys type error on DNNCLassifier Tensorflowtf.estimator.inputs.pandas_input_fn throws _NumericColumn' object has no attribute 'insert_transformed_featureTensorflow implementing crf loss
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm using tf.contrib.copy_graph.copy_op_to_graph()
to copy an operation from g1
to g2
.
Edited code:
BATCH_SIZE = 1, TIME_STEP = 2
def noise_rnn(self, BATCH_SIZE, TIME_STEP):
with tf.variable_scope("noise_rnn", reuse=tf.AUTO_REUSE, initializer=tf.orthogonal_initializer()):
gaussianNoiseRnnInputList=[]
for batch in range(BATCH_SIZE):
gaussianNoiseInputList=[]
for i in range(TIME_STEP):
gaussianNoiseInput = tf.truncated_normal(shape=[1, 1, 10], mean=0, stddev=tf.sqrt(0.6))
gaussianNoiseInputList.append(gaussianNoiseInput)
gaussianNoiseInput = tf.concat(gaussianNoiseInputList, axis=1, name='gaussianNoiseInput_concat')
gaussianNoiseRnnInputList.append(gaussianNoiseInput)
gaussianNoiseRnnInput = tf.concat(gaussianNoiseRnnInputList, axis=0, name='gaussianNoiseRnnInput_concat')
cell = tf.nn.rnn_cell.GRUCell(10)
hiddens, states = tf.nn.dynamic_rnn(cell=cell, inputs=gaussianNoiseRnnInput, dtype=tf.float32)
return hiddens
with noiseGraph.as_default():
gaussianRnnOutput = speech2vid.noise_rnn(BATCH_SIZE, TIME_STEP)
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
gaussianRnnOutput_copy = tf.contrib.copy_graph.copy_op_to_graph(gaussianRnnOutput, g2, [])
New error when copy_variable_to_graph(BATCH_SIZE, g2)
:
TypeError: 1(BATCH_SIZE) is not a Variable.
If I comment the following two lines:
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
I get another error:
......
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1879, in inputs
return Operation._InputList(self)
RuntimeError: maximum recursion depth exceeded
Actually, I do not know how to use this function. Some one can explain the third param []
in the function for me? And how to solve this error?
Thank you!
I saw an example here. But I do not know the meaning of []
?
EDIT: Edit code and errors.
python python-2.7 tensorflow
add a comment |
I'm using tf.contrib.copy_graph.copy_op_to_graph()
to copy an operation from g1
to g2
.
Edited code:
BATCH_SIZE = 1, TIME_STEP = 2
def noise_rnn(self, BATCH_SIZE, TIME_STEP):
with tf.variable_scope("noise_rnn", reuse=tf.AUTO_REUSE, initializer=tf.orthogonal_initializer()):
gaussianNoiseRnnInputList=[]
for batch in range(BATCH_SIZE):
gaussianNoiseInputList=[]
for i in range(TIME_STEP):
gaussianNoiseInput = tf.truncated_normal(shape=[1, 1, 10], mean=0, stddev=tf.sqrt(0.6))
gaussianNoiseInputList.append(gaussianNoiseInput)
gaussianNoiseInput = tf.concat(gaussianNoiseInputList, axis=1, name='gaussianNoiseInput_concat')
gaussianNoiseRnnInputList.append(gaussianNoiseInput)
gaussianNoiseRnnInput = tf.concat(gaussianNoiseRnnInputList, axis=0, name='gaussianNoiseRnnInput_concat')
cell = tf.nn.rnn_cell.GRUCell(10)
hiddens, states = tf.nn.dynamic_rnn(cell=cell, inputs=gaussianNoiseRnnInput, dtype=tf.float32)
return hiddens
with noiseGraph.as_default():
gaussianRnnOutput = speech2vid.noise_rnn(BATCH_SIZE, TIME_STEP)
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
gaussianRnnOutput_copy = tf.contrib.copy_graph.copy_op_to_graph(gaussianRnnOutput, g2, [])
New error when copy_variable_to_graph(BATCH_SIZE, g2)
:
TypeError: 1(BATCH_SIZE) is not a Variable.
If I comment the following two lines:
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
I get another error:
......
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1879, in inputs
return Operation._InputList(self)
RuntimeError: maximum recursion depth exceeded
Actually, I do not know how to use this function. Some one can explain the third param []
in the function for me? And how to solve this error?
Thank you!
I saw an example here. But I do not know the meaning of []
?
EDIT: Edit code and errors.
python python-2.7 tensorflow
add a comment |
I'm using tf.contrib.copy_graph.copy_op_to_graph()
to copy an operation from g1
to g2
.
Edited code:
BATCH_SIZE = 1, TIME_STEP = 2
def noise_rnn(self, BATCH_SIZE, TIME_STEP):
with tf.variable_scope("noise_rnn", reuse=tf.AUTO_REUSE, initializer=tf.orthogonal_initializer()):
gaussianNoiseRnnInputList=[]
for batch in range(BATCH_SIZE):
gaussianNoiseInputList=[]
for i in range(TIME_STEP):
gaussianNoiseInput = tf.truncated_normal(shape=[1, 1, 10], mean=0, stddev=tf.sqrt(0.6))
gaussianNoiseInputList.append(gaussianNoiseInput)
gaussianNoiseInput = tf.concat(gaussianNoiseInputList, axis=1, name='gaussianNoiseInput_concat')
gaussianNoiseRnnInputList.append(gaussianNoiseInput)
gaussianNoiseRnnInput = tf.concat(gaussianNoiseRnnInputList, axis=0, name='gaussianNoiseRnnInput_concat')
cell = tf.nn.rnn_cell.GRUCell(10)
hiddens, states = tf.nn.dynamic_rnn(cell=cell, inputs=gaussianNoiseRnnInput, dtype=tf.float32)
return hiddens
with noiseGraph.as_default():
gaussianRnnOutput = speech2vid.noise_rnn(BATCH_SIZE, TIME_STEP)
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
gaussianRnnOutput_copy = tf.contrib.copy_graph.copy_op_to_graph(gaussianRnnOutput, g2, [])
New error when copy_variable_to_graph(BATCH_SIZE, g2)
:
TypeError: 1(BATCH_SIZE) is not a Variable.
If I comment the following two lines:
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
I get another error:
......
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1879, in inputs
return Operation._InputList(self)
RuntimeError: maximum recursion depth exceeded
Actually, I do not know how to use this function. Some one can explain the third param []
in the function for me? And how to solve this error?
Thank you!
I saw an example here. But I do not know the meaning of []
?
EDIT: Edit code and errors.
python python-2.7 tensorflow
I'm using tf.contrib.copy_graph.copy_op_to_graph()
to copy an operation from g1
to g2
.
Edited code:
BATCH_SIZE = 1, TIME_STEP = 2
def noise_rnn(self, BATCH_SIZE, TIME_STEP):
with tf.variable_scope("noise_rnn", reuse=tf.AUTO_REUSE, initializer=tf.orthogonal_initializer()):
gaussianNoiseRnnInputList=[]
for batch in range(BATCH_SIZE):
gaussianNoiseInputList=[]
for i in range(TIME_STEP):
gaussianNoiseInput = tf.truncated_normal(shape=[1, 1, 10], mean=0, stddev=tf.sqrt(0.6))
gaussianNoiseInputList.append(gaussianNoiseInput)
gaussianNoiseInput = tf.concat(gaussianNoiseInputList, axis=1, name='gaussianNoiseInput_concat')
gaussianNoiseRnnInputList.append(gaussianNoiseInput)
gaussianNoiseRnnInput = tf.concat(gaussianNoiseRnnInputList, axis=0, name='gaussianNoiseRnnInput_concat')
cell = tf.nn.rnn_cell.GRUCell(10)
hiddens, states = tf.nn.dynamic_rnn(cell=cell, inputs=gaussianNoiseRnnInput, dtype=tf.float32)
return hiddens
with noiseGraph.as_default():
gaussianRnnOutput = speech2vid.noise_rnn(BATCH_SIZE, TIME_STEP)
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
gaussianRnnOutput_copy = tf.contrib.copy_graph.copy_op_to_graph(gaussianRnnOutput, g2, [])
New error when copy_variable_to_graph(BATCH_SIZE, g2)
:
TypeError: 1(BATCH_SIZE) is not a Variable.
If I comment the following two lines:
BATCH_SIZE_copy = tf.contrib.copy_graph.copy_variable_to_graph(BATCH_SIZE, g2)
TIME_STEP_copy = tf.contrib.copy_graph.copy_variable_to_graph(TIME_STEP, g2)
I get another error:
......
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 172, in copy_op_to_graph
new_op = copy_op_to_graph(op, to_graph, variables, scope)
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/contrib/copy_graph/python/util/copy_elements.py", line 200, in copy_op_to_graph
for x in op.inputs]
File "/media/data2/liuhan/envs/tf/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1879, in inputs
return Operation._InputList(self)
RuntimeError: maximum recursion depth exceeded
Actually, I do not know how to use this function. Some one can explain the third param []
in the function for me? And how to solve this error?
Thank you!
I saw an example here. But I do not know the meaning of []
?
EDIT: Edit code and errors.
python python-2.7 tensorflow
python python-2.7 tensorflow
edited Mar 25 at 1:54
Han.liu
asked Mar 24 at 9:55
Han.liuHan.liu
266
266
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
By looking at the source code, it seems that the variables
argument is used to retrieve already copied variables:
#Extract names of variables
copied_variables = dict((x.name, x) for x in variables)
#If a variable by the new name already exists, return the
#correspondng tensor that will act as an input
if new_name in copied_variables:
return to_graph.get_tensor_by_name(copied_variables[new_name].name)
However the following lines retrieve copied variables (or ops/tensors) as well:
try:
already_present = to_graph.as_graph_element(
new_name, allow_tensor=True, allow_operation=True)
return already_present
except:
pass
So my guess is that you can pass an empty list as third argument.
The simplified code you presented is not enough to understand what's your problem is, but in general, copy_op_to_graph()
takes operation or tensor (if it is a tensor, then it is an output of some unerlying operation) as input and copies it to a new graph. If it has variables as inputs you should copy those variables using copy_variable_to_graph()
before you attempt to copy an op. Take a look at this example of copying two variables and addition operation to a new graph:
import tensorflow as tf
var1 = tf.Variable(2*tf.ones([2, 2]), name='var1')
var2 = tf.Variable(tf.ones([2, 2]), name='var2')
add_tensor = tf.add(var1, var2)
to_graph = tf.Graph() # graph where everything above will be copied to
var1_copied = tf.contrib.copy_graph.copy_variable_to_graph(var1, to_graph)
var2_copied = tf.contrib.copy_graph.copy_variable_to_graph(var2, to_graph)
add_tensor_copied = tf.contrib.copy_graph.copy_op_to_graph(add_tensor, to_graph, [])
with tf.Session(graph=tf.get_default_graph()) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor.eval())
# [[3. 3.]
# [3. 3.]]
with tf.Session(graph=to_graph) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor_copied.eval())
# [[3. 3.]
# [3. 3.]]
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied tog2
before copying variables? Thank you very much. You really helped!
– Han.liu
Mar 25 at 2:01
BATCH_SIZE
andTIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to creategaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible withtensorflow==1.9
.
– Vlad
Mar 25 at 10:52
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322558%2fhow-to-use-tf-contrib-copy-graph-copy-op-to-graph%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
By looking at the source code, it seems that the variables
argument is used to retrieve already copied variables:
#Extract names of variables
copied_variables = dict((x.name, x) for x in variables)
#If a variable by the new name already exists, return the
#correspondng tensor that will act as an input
if new_name in copied_variables:
return to_graph.get_tensor_by_name(copied_variables[new_name].name)
However the following lines retrieve copied variables (or ops/tensors) as well:
try:
already_present = to_graph.as_graph_element(
new_name, allow_tensor=True, allow_operation=True)
return already_present
except:
pass
So my guess is that you can pass an empty list as third argument.
The simplified code you presented is not enough to understand what's your problem is, but in general, copy_op_to_graph()
takes operation or tensor (if it is a tensor, then it is an output of some unerlying operation) as input and copies it to a new graph. If it has variables as inputs you should copy those variables using copy_variable_to_graph()
before you attempt to copy an op. Take a look at this example of copying two variables and addition operation to a new graph:
import tensorflow as tf
var1 = tf.Variable(2*tf.ones([2, 2]), name='var1')
var2 = tf.Variable(tf.ones([2, 2]), name='var2')
add_tensor = tf.add(var1, var2)
to_graph = tf.Graph() # graph where everything above will be copied to
var1_copied = tf.contrib.copy_graph.copy_variable_to_graph(var1, to_graph)
var2_copied = tf.contrib.copy_graph.copy_variable_to_graph(var2, to_graph)
add_tensor_copied = tf.contrib.copy_graph.copy_op_to_graph(add_tensor, to_graph, [])
with tf.Session(graph=tf.get_default_graph()) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor.eval())
# [[3. 3.]
# [3. 3.]]
with tf.Session(graph=to_graph) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor_copied.eval())
# [[3. 3.]
# [3. 3.]]
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied tog2
before copying variables? Thank you very much. You really helped!
– Han.liu
Mar 25 at 2:01
BATCH_SIZE
andTIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to creategaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible withtensorflow==1.9
.
– Vlad
Mar 25 at 10:52
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
add a comment |
By looking at the source code, it seems that the variables
argument is used to retrieve already copied variables:
#Extract names of variables
copied_variables = dict((x.name, x) for x in variables)
#If a variable by the new name already exists, return the
#correspondng tensor that will act as an input
if new_name in copied_variables:
return to_graph.get_tensor_by_name(copied_variables[new_name].name)
However the following lines retrieve copied variables (or ops/tensors) as well:
try:
already_present = to_graph.as_graph_element(
new_name, allow_tensor=True, allow_operation=True)
return already_present
except:
pass
So my guess is that you can pass an empty list as third argument.
The simplified code you presented is not enough to understand what's your problem is, but in general, copy_op_to_graph()
takes operation or tensor (if it is a tensor, then it is an output of some unerlying operation) as input and copies it to a new graph. If it has variables as inputs you should copy those variables using copy_variable_to_graph()
before you attempt to copy an op. Take a look at this example of copying two variables and addition operation to a new graph:
import tensorflow as tf
var1 = tf.Variable(2*tf.ones([2, 2]), name='var1')
var2 = tf.Variable(tf.ones([2, 2]), name='var2')
add_tensor = tf.add(var1, var2)
to_graph = tf.Graph() # graph where everything above will be copied to
var1_copied = tf.contrib.copy_graph.copy_variable_to_graph(var1, to_graph)
var2_copied = tf.contrib.copy_graph.copy_variable_to_graph(var2, to_graph)
add_tensor_copied = tf.contrib.copy_graph.copy_op_to_graph(add_tensor, to_graph, [])
with tf.Session(graph=tf.get_default_graph()) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor.eval())
# [[3. 3.]
# [3. 3.]]
with tf.Session(graph=to_graph) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor_copied.eval())
# [[3. 3.]
# [3. 3.]]
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied tog2
before copying variables? Thank you very much. You really helped!
– Han.liu
Mar 25 at 2:01
BATCH_SIZE
andTIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to creategaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible withtensorflow==1.9
.
– Vlad
Mar 25 at 10:52
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
add a comment |
By looking at the source code, it seems that the variables
argument is used to retrieve already copied variables:
#Extract names of variables
copied_variables = dict((x.name, x) for x in variables)
#If a variable by the new name already exists, return the
#correspondng tensor that will act as an input
if new_name in copied_variables:
return to_graph.get_tensor_by_name(copied_variables[new_name].name)
However the following lines retrieve copied variables (or ops/tensors) as well:
try:
already_present = to_graph.as_graph_element(
new_name, allow_tensor=True, allow_operation=True)
return already_present
except:
pass
So my guess is that you can pass an empty list as third argument.
The simplified code you presented is not enough to understand what's your problem is, but in general, copy_op_to_graph()
takes operation or tensor (if it is a tensor, then it is an output of some unerlying operation) as input and copies it to a new graph. If it has variables as inputs you should copy those variables using copy_variable_to_graph()
before you attempt to copy an op. Take a look at this example of copying two variables and addition operation to a new graph:
import tensorflow as tf
var1 = tf.Variable(2*tf.ones([2, 2]), name='var1')
var2 = tf.Variable(tf.ones([2, 2]), name='var2')
add_tensor = tf.add(var1, var2)
to_graph = tf.Graph() # graph where everything above will be copied to
var1_copied = tf.contrib.copy_graph.copy_variable_to_graph(var1, to_graph)
var2_copied = tf.contrib.copy_graph.copy_variable_to_graph(var2, to_graph)
add_tensor_copied = tf.contrib.copy_graph.copy_op_to_graph(add_tensor, to_graph, [])
with tf.Session(graph=tf.get_default_graph()) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor.eval())
# [[3. 3.]
# [3. 3.]]
with tf.Session(graph=to_graph) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor_copied.eval())
# [[3. 3.]
# [3. 3.]]
By looking at the source code, it seems that the variables
argument is used to retrieve already copied variables:
#Extract names of variables
copied_variables = dict((x.name, x) for x in variables)
#If a variable by the new name already exists, return the
#correspondng tensor that will act as an input
if new_name in copied_variables:
return to_graph.get_tensor_by_name(copied_variables[new_name].name)
However the following lines retrieve copied variables (or ops/tensors) as well:
try:
already_present = to_graph.as_graph_element(
new_name, allow_tensor=True, allow_operation=True)
return already_present
except:
pass
So my guess is that you can pass an empty list as third argument.
The simplified code you presented is not enough to understand what's your problem is, but in general, copy_op_to_graph()
takes operation or tensor (if it is a tensor, then it is an output of some unerlying operation) as input and copies it to a new graph. If it has variables as inputs you should copy those variables using copy_variable_to_graph()
before you attempt to copy an op. Take a look at this example of copying two variables and addition operation to a new graph:
import tensorflow as tf
var1 = tf.Variable(2*tf.ones([2, 2]), name='var1')
var2 = tf.Variable(tf.ones([2, 2]), name='var2')
add_tensor = tf.add(var1, var2)
to_graph = tf.Graph() # graph where everything above will be copied to
var1_copied = tf.contrib.copy_graph.copy_variable_to_graph(var1, to_graph)
var2_copied = tf.contrib.copy_graph.copy_variable_to_graph(var2, to_graph)
add_tensor_copied = tf.contrib.copy_graph.copy_op_to_graph(add_tensor, to_graph, [])
with tf.Session(graph=tf.get_default_graph()) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor.eval())
# [[3. 3.]
# [3. 3.]]
with tf.Session(graph=to_graph) as sess:
sess.run(tf.global_variables_initializer())
print(add_tensor_copied.eval())
# [[3. 3.]
# [3. 3.]]
edited Mar 24 at 12:01
answered Mar 24 at 11:54
VladVlad
3,46111330
3,46111330
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied tog2
before copying variables? Thank you very much. You really helped!
– Han.liu
Mar 25 at 2:01
BATCH_SIZE
andTIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to creategaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible withtensorflow==1.9
.
– Vlad
Mar 25 at 10:52
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
add a comment |
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied tog2
before copying variables? Thank you very much. You really helped!
– Han.liu
Mar 25 at 2:01
BATCH_SIZE
andTIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to creategaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible withtensorflow==1.9
.
– Vlad
Mar 25 at 10:52
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
According to what you said "If it has variables as inputs you should copy those variables using copy_variable_to_graph() before you attempt to copy an op." I realized I do have some inputs, but my inputs are not tensorflow variables, and I get another error. I'm sorry I didn't show a clear code. I have edited my question.
– Han.liu
Mar 25 at 1:34
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied to
g2
before copying variables? Thank you very much. You really helped!– Han.liu
Mar 25 at 2:01
I have updated my code and error. Two 'int' are passed to the function. Is there any variables needed to be copied to
g2
before copying variables? Thank you very much. You really helped!– Han.liu
Mar 25 at 2:01
BATCH_SIZE
and TIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to create gaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible with tensorflow==1.9
.– Vlad
Mar 25 at 10:52
BATCH_SIZE
and TIME_STEP
are not part of tensorflow graph and they aren't variables. You can't copy them. You first need to create gaussianRnnOutput
and then you will be able to copy it. But again, to copy the whole graph it is not that trivial. You need to copy variables and ops individually. Take a look at this code that takes care of recursive copy. It may not be compatible with every tensorflow version, but It for certain compatible with tensorflow==1.9
.– Vlad
Mar 25 at 10:52
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
+ you should mention in your question that it has been edited and provide the original version of your question.
– Vlad
Mar 25 at 13:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322558%2fhow-to-use-tf-contrib-copy-graph-copy-op-to-graph%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown