About tensorflow graph: what am I wrong with this program? - tensorflow

import tensorflow as tf
def activation(e, f, g):
return e + f + g
with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')
res = activation(a, b, c)
init = tf.initialize_all_variables()
with tf.Session() as sess:
# Start running operations on the Graph.
merged = tf.merge_all_summaries()
sess.run(init)
hi = sess.run(res)
print hi
writer = tf.train.SummaryWriter("/tmp/basic", sess.graph_def)
output error:
Value Error: Fetch argument <tf.Tensor 'add_1:0' shape=(3,) dtype=int32> of
<tf.Tensor 'add_1:0' shape=(3,) dtype=int32> cannot be interpreted as a Tensor.
(Tensor Tensor("add_1:0", shape=(3,), dtype=int32) is not an element of this graph.)

The error is actually giving you a tip of why it's failing. When you started the session, you are actually using a different graph than the one referenced by res. The easiest solution is to simply move everything inside with tf.Graph().as_default():.
import tensorflow as tf
def activation(e, f, g):
return e + f + g
with tf.Graph().as_default():
a = tf.constant([5, 4, 5], name='a')
b = tf.constant([0, 1, 2], name='b')
c = tf.constant([5, 0, 5], name='c')
res = activation(a, b, c)
init = tf.initialize_all_variables()
with tf.Session() as sess:
# Start running operations on the Graph.
merged = tf.merge_all_summaries()
sess.run(init)
hi = sess.run(res)
print hi
writer = tf.train.SummaryWriter("/tmp/basic", sess.graph_def)
Alternatively you can also just remove the line with tf.Graph().as_default(): and then it will also worked as expected.

Another option is to initialize graph variable first:
graph = tf.Graph()
with graph.as_default():
and than pass it to your session:
with tf.Session(graph=graph) as session:

Related

How to update the variable in tf.variable_scope with variable's name?

I want to modification the value about variable 'weight1' in tf.variable_scope.
I try to modification the value by other function, but it not work follow me.
def inference(q, reuse=False):
with tf.variable_scope('layer1', reuse = reuse):
x = tf.get_variable('weight1', [1, 3], initializer = tf.truncated_normal_initializer(stddev = 0.1))
y = tf.get_variable('weight2', [3, 1], initializer = tf.constant_initializer([[1],[2],[3]]))
return tf.matmul(x, y)
def update_process(reuse=True):
with tf.variable_scope('layer1', reuse = reuse):
x = tf.get_variable('weight1',[1, 3])
update=tf.assign(x, x-1)
with tf.Session() as sess:
sess.run(init)
print(sess.run(x))
init = tf.global_variables_initializer()
z = inference(1)
with tf.Session() as sess:
sess.run(init)
for i in range(5):
update_process(reuse = True)
print(sess.run(z))
print('\n')
I want to this code output different list about sess.run(z), but the value is always same.
You need to run sess.run(update) in update_process in the same session that the inference part of the graph runs:
import tensorflow as tf
def inference(q, reuse=False):
with tf.variable_scope('layer1', reuse = reuse):
x = tf.get_variable('weight1', [1, 3], initializer = tf.truncated_normal_initializer(stddev = 0.1))
y = tf.get_variable('weight2', [3, 1], initializer = tf.constant_initializer([[1],[2],[3]]))
return tf.matmul(x, y)
def update_process(reuse=True):
with tf.variable_scope('layer1', reuse = reuse):
x = tf.get_variable('weight1',[1, 3])
update=tf.assign(x, x-1)
print(sess.run(update))
z = inference(1)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(5):
update_process(reuse = True)
print(sess.run(z))
print('\n')

How to use Dateset to feed array of data to inference with tensorflow?

I am new to Tensorflow Dataset API, and I could not fully understand the simplicity of its design, so I need some help.
Here is a simple example
import tensorflow as tf
x = tf.placeholder(tf.int32, shape=[])
y = tf.square(x)
with tf.Session() as sess:
print(sess.run(y, {x: 2}))
# result is 4, simple
If I have an integer array arr_x=[2, 3, 5, 8, 10], how can I use Dateset API to iterate the array?
I am trying
p = tf.placeholder(tf.int32, shape=[None])
d = tf.data.Dataset.from_tensor_slices(p)
d = d.map(lambda x: x)
iter = d.make_initializable_iterator()
next_element = iter.get_next()
with tf.Session() as sess:
sess.run(iter.initializer, feed_dict={p: [2, 3, 4]})
while True:
try:
print sess.run(y, next_element)
except tf.errors.OutOfRangeError:
break
But no luck, any idea?
What about:
arr_x = np.array([2, 3, 5, 8, 10])
arr_y = np.array([[0,1],[1,0],[1,0],[0,1],[1,0]])
dataset = tf.data.Dataset.from_tensor_slices((arr_x, arr_y))
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
sess = tf.Session()
while True:
try:
print(sess.run(next_element))
except tf.errors.OutOfRangeError:
break

Variable assignment in TensorFlow gives error

If I have a placeholder:
placeholder = tf.placeholder(dtype=np.float32, shape=[1, 2])
And then I create an op which assigns the placeholder to a new variable y:
y = tf.Variable([[0, 0]], dtype=tf.float32)
y_op = tf.assign(y, placeholder)
Then specify a value which I will feed into this placeholder:
x = tf.Variable([[5, 5]], dtype=np.float32)
And finally run the operation:
sess = tf.Session()
sess.run(tf.global_variables_initializer())
sess.run(y_op, feed_dict={placeholder: x})
I get the following error:
ValueError: setting an array element with a sequence.
Why is this? From what I can see, the shapes of placeholder, y, and x, are all [1, 2].
You are trying to feed a graph variable using the feed dict. Feed dict and placeholders are for feeding external values into the graph. This code works:
placeholder = tf.placeholder(dtype=np.float32, shape=[1, 2])
y = tf.Variable([[0, 0]], dtype=tf.float32)
y_op = tf.assign(y, placeholder)
value = np.array([[5,5]])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
r = sess.run(y_op, feed_dict={placeholder: value})
But if the value you want to use in the graph is already a tf.Variable, there is no reason to use feed dict at all. This also works:
x = tf.Variable([[5, 5]], dtype=tf.float32)
y = tf.Variable([[0, 0]], dtype=tf.float32)
y_op = tf.assign(y, x)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
r = sess.run(y_op)

How to process the whole batch tensor in efficient way?

with tf.Session() as sess:
with tf.variable_scope('experiment'):
a = tf.get_variable('a', [1000,24,128], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1) )
b = tf.get_variable('b', [1000,15,128], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1) )
c = tf.get_variable('c', [1000,24,15], dtype=tf.float32, initializer=tf.random_normal_initializer(stddev=0.1))
for i in range(1000):
ai = tf.slice(a,[i,0,0],[1,-1,-1]) # (1,24,128)
bi = tf.slice(b,[i,0,0],[1,-1,-1]) # (1,15,128)
aii = tf.reshape(ai, [ int(ai.get_shape()[1]), int(ai.get_shape()[2])] ) # (24, 128)
bii = tf.reshape(bi, [ int(bi.get_shape()[1]), int(bi.get_shape()[2])] ) # (15, 128)
ci = contract_func(aii,bii) # (24,15)
c[i] = ci
I want to process the two batch: a and b and product c. Batch size is 1000.
Every element ci is the contract result of ai and bi. The contract operation is defined by myself.
But I am not sure whether it is an efficient way to do this? Is there any simple method ? Thanks.
I think what you are trying to achieve can be done with just:
import tensorflow as tf
with tf.variable_scope('experiment'):
a = tf.get_variable('a', [1000, 24, 128], dtype=tf.float32,
initializer=tf.random_normal_initializer(stddev=0.1))
b = tf.get_variable('b', [1000, 15, 128], dtype=tf.float32,
initializer=tf.random_normal_initializer(stddev=0.1))
c = tf.matmul(a, tf.transpose(b, (0, 2, 1)))
# Since Python 3.5 you can also do
# c = a # tf.transpose(b, (0, 2, 1))
print(c.shape)
# TensorShape([Dimension(1000), Dimension(24), Dimension(15)])
# Test compute the value
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
c_value = sess.run(c)
print(c_value.shape)
# (1000, 24, 15)

Difference between SparseTensor and SparseTensorValue

What is the difference between SparseTensor and SparseTensorValue? Is there anything I should keep in mind if I want to build the sparse tensor based on fed indices and values? I could only find a few toy examples.
It depends on where you define your Sparse Tensor.
If you would like to define the tensor outside the graph, e.g. define the sparse tensor for later data feed, use SparseTensorValue. In contrast, if the sparse tensor is defined in graph, use SparseTensor
Sample code for tf.SparseTensorValue:
x_sp = tf.sparse_placeholder(dtype=tf.float32)
W = tf.Variable(tf.random_normal([6, 6]))
y = tf.sparse_tensor_dense_matmul(sp_a=x_sp, b=W)
init = tf.global_variables_initializer()
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
sess.run(init)
stv = tf.SparseTensorValue(indices=[[0, 0], [1, 2]], values=[1.1, 1.2],
dense_shape=[2,6])
result = sess.run(y,feed_dict={x_sp:stv})
print(result)
Sample code for tf.SparseTensor:
indices_i = tf.placeholder(dtype=tf.int64, shape=[2, 2])
values_i = tf.placeholder(dtype=tf.float32, shape=[2])
dense_shape_i = tf.placeholder(dtype=tf.int64, shape=[2])
st = tf.SparseTensor(indices=indices_i, values=values_i, dense_shape=dense_shape_i)
W = tf.Variable(tf.random_normal([6, 6]))
y = tf.sparse_tensor_dense_matmul(sp_a=st, b=W)
init = tf.global_variables_initializer()
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
sess.run(init)
result = sess.run(y,feed_dict={indices_i:[[0, 0], [1, 2]], values_i:[1.1, 1.2], dense_shape_i:[2,6]})
print(result)
Hope this help~