I would like to feed a placeholder defined in a function. The following is an simplified example.
#!/usr/bin/python
import tensorflow as tf
def CreateInference():
x2 = tf.placeholder(tf.float32, (None))
w2 = tf.get_variable('w2', initializer=1.0)
b2 = tf.get_variable('b2', initializer=2.0)
y2 = w2 * x2 + b2
y2 = CreateInference()
writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# print (sess.run(y2, feed_dict={x2:2.0}))
writer.close()
The graph is correctly created as shown in the following Tensorboard graph.
The problem is that feed_dict={x2:2.0} doesn't work, since x2 is a local variable used within the function CreateInference. Could anyone please tell me how to access and feed values for the variable x2 in the above example?
Why not do the obvious and return references of the objects
#!/usr/bin/python
import tensorflow as tf
def CreateInference():
x2 = tf.placeholder(tf.float32, (None))
w2 = tf.get_variable('w2', initializer=1.0)
b2 = tf.get_variable('b2', initializer=2.0)
y2 = w2 * x2 + b2
return x2, y2
x2, y2 = CreateInference()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print (sess.run(y2, feed_dict={x2:2.0}))
Related
I have been trying for a while to implement sampled softmax because I have half a million output classes.
I have tried to follow the official documentation exactly, but I always get an error. This is my code:
def forward_propagation_sampled(X, parameters):
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
W3 = parameters['W3']
b3 = parameters['b3']
Z1 = tf.add(tf.matmul(W1, X), b1)
A1 = tf.nn.relu(Z1)
Z2 = tf.add(tf.matmul(W2,A1), b2)
A2 = tf.nn.relu(Z2)
Z3 = tf.add(tf.matmul(W3,A2), b3)
return Z3, W3, b3
This is the cost computation function:
def compute_cost(Z3, W3, b3, Y, mode):
Z3.set_shape([1144,1])
if mode == "train":
loss = tf.nn.sampled_softmax_loss(
weights=tf.transpose(W3),
biases=tf.Variable(b3),
labels = tf.reshape(tf.argmax(Y, 1), [-1,1]), #Since Y is one hot encoded
inputs=tf.Variable(initial_value=Z3,dtype=tf.float32, expected_shape=[1144,1]),
num_sampled = 2000,
num_classes = 1144,
partition_strategy="div"
)
elif mode == "eval":
logits = tf.matmul(inputs, tf.transpose(weights))
logits = tf.nn.bias_add(logits, biases)
labels_one_hot = tf.one_hot(labels, n_classes)
loss = tf.nn.softmax_cross_entropy_with_logits(labels=labels_one_hot,logits=logits)
cost = tf.reduce_mean(loss)
return cost
For the purpose of just testing this out, I am using 1144 output classes, which would otherwise scale to 500,000. There are 3144 training examples.
I get this error:
Shape must be rank 1 but is rank 2 for 'sampled_softmax_loss/Slice_1' (op: 'Slice') with input shapes: [3144,1], [1], [1].
I am unable to debug this or make any sense out of it. Any help would be really appreciated.
TensorFlow is throwing a TypeError when I execute the simplest possible graph.
sess = tf.Session()
x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)
z = x1
sess.run(tf.gradients(z, [x1, x2]), feed_dict={x1: 1, x2: 1})
This yields
TypeError: Fetch argument None has invalid type <class 'NoneType'>
Interestingly, if I change one line of code to:
z = x1 + x2
Then everything works perfectly!
Why is this happening?
According to your description, I properly modify the code.
import tensorflow as tf
sess = tf.Session()
x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)
z = x1
print sess.run(tf.gradients(z, [x1]), feed_dict={x1: 1})
z = x1 + x2
print sess.run(tf.gradients(z, [x1, x2]), feed_dict={x1: 1, x2: 1})
Now there is no error. I believe you can understand your problem.
When you are going to apply a function, you may need to look at the document. Here is the document on tf.gradients.
I am trying to estimate the forward pass and the backword gradient of the function below:
def func(img-batch, X1,X2):
L=1
A1 = X1*L**2
A2 = X2*L**2
AA1 = A1*A1
AA2 = A2*A2
A11A2 = A1*A2
v = tf.nn.conv2d(img-batch, A1A2, strides=[1, 1, 1, 1], padding='SAME')
v = v+ AA1+AA2
return v
When I add this function to the network, the gradient will be performed on each instruction of the function by default.
How can I use this function and calculate it in the forward pass, in the meantime ignoring the gradient of each instruction in the function and provide other gradient estimation and add it to the main gradient of the model?
You can use py_func to ignore the gradients in this function, and use gradient_override_map to provide customized gradients. Here is an example:
import tensorflow as tf
def myfunc(X1, X2):
L = 1
A1 = X1 * L**2
A2 = X2 * L**2
AA1 = A1 * A1
AA2 = A2 * A2
A11A2 = A1 * A2
...
v = AA1 + AA2 + A11A2
return v
#tf.RegisterGradient("GradMyfunc")
def grad_myfunc(op, grad):
X1 = op.inputs[0]
X2 = op.inputs[1]
return [grad * X2, grad * X1]
X1 = tf.Variable(tf.constant(1.1, dtype=tf.float64))
X2 = tf.Variable(tf.constant(2.2, dtype=tf.float64))
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": "GradMyfunc"}):
y = tf.py_func(myfunc, [X1, X2], [tf.float64])
with tf.Session() as sess:
grad = tf.gradients(y, [X1, X2])
sess.run(tf.global_variables_initializer())
print(sess.run(y))
print(sess.run(grad))
Look at the code:
import tensorflow as tf
x = tf.Variable(1)
x = tf.stop_gradient(x)
y = 2 * x
g = tf.gradients(y, x)
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
print(sess.run(g))
I want to freeze x and the gradient of y wrt x is zero, but the output is 2, so what's wrong?
Update
import tensorflow as tf
x0 = tf.Variable(1)
x1 = tf.stop_gradient(x0)
y = 2 * x1
g = tf.gradients(y, x0)
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
print(sess.run(g))
I use x1 which don't overwrite x0, but when I run this code, it will raise a error. If tf.stop_gradient act as tf.identity, I think x0 will have a path to y in computation graph and the gradient is 0 rather than raising a error? Can someone tell me what tf.stop_gradient does indeed?
tf.stop_gradient() stops the gradient computation at the specified point in the computation graph. Applying it to the variable is "too late", but you can apply it to y.
import tensorflow as tf
x = tf.Variable(1)
y = tf.stop_gradient(2 * x)
g = tf.gradients(y, x)
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
print(sess.run(g))
Note that this will not output 0 but instead throws an error, since the gradient of y w.r.t. to x is undefined in this case and you explicitly ask for it. In a real-world scenario this would probably not be the case.
import tensorflow as tf
import numpy as np
#Constant Declaration
LEARNING_RATE = 0.05
LEARNING_TIME = 10000
FILE_NAME = 'xor'
# Input Data Declaration
xy = np.loadtxt(FILE_NAME+'_data_set.txt',unpack=True,dtype='float32',delimiter=',')
x_data = np.transpose(xy[0:-1])
y_data = np.transpose(xy[-1])
print x_data
# Declaration Part
X = tf.placeholder(dtype = tf.float32,name="X-input")
Y = tf.placeholder(dtype = tf.float32,name="Y-input")
W1 = tf.Variable(tf.random_uniform([2,2],-1.0,1.0), name="Weight_1")
W2 = tf.Variable(tf.random_uniform([2,1],-1.0,1.0), name="Weight_2")
b1 = tf.Variable(tf.zeros([2]), name = 'Bias1')
b2 = tf.Variable(tf.zeros([1]), name = 'Bias2')
# Formula Part
with tf.name_scope("Layer1") as scope:
L1 = tf.sigmoid(tf.matmul(X,W1) + b1)
with tf.name_scope("Layer2") as scope:
hypothesis = tf.sigmoid(tf.matmul(L1,W2) + b2)
with tf.name_scope("Cost") as scope:
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
cost_summ = tf.scalar_summary("cost",cost)
# Minimizing Part
a = tf.Variable(LEARNING_RATE)
with tf.name_scope("train") as scope:
optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)
# Initializing Part
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
merged = tf.merge_all_summaries()
writer = tf.train.SummaryWriter('./logs/'+FILE_NAME,sess.graph_def)
# Running Part
for step in range(LEARNING_TIME):
sess.run(train,feed_dict = {X:x_data,Y:y_data})
if step % 20 == 0:
print step, sess.run(cost,feed_dict = {X:x_data,Y:y_data}), sess.run(W2)
summary = sess.run(merged, feed_dict = {X:x_data,Y:y_data})
writer.add_summary(summary, step)
#Test Part
correction = tf.equal(tf.floor(hypothesis + 0.5),Y)
accuracy = tf.reduce_mean(tf.cast(correction,'float'))
print sess.run([hypothesis,tf.floor(hypothesis + 0.5),correction,accuracy], feed_dict = {X:x_data,Y:y_data})
Above is my tensorflow code for solving xor logic. But problem is that accuracy is just 50 percent. And cost Converge to 0.69321.
Actually I have seen lots of code that solves xor implemented in tensorflow, I can't find what is wrong.
belows are images indicate how my code works.