Skip to content

Instantly share code, notes, and snippets.

@Santara
Last active April 8, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Santara/1740be56e9f7198fe908e4327dd2a7b9 to your computer and use it in GitHub Desktop.
Save Santara/1740be56e9f7198fe908e4327dd2a7b9 to your computer and use it in GitHub Desktop.
Multi-network tensorflow trial
import tensorflow as tf
import numpy as np
inputs1 = tf.placeholder(shape=[1,16],dtype=tf.float32)
W = tf.Variable(tf.random_uniform([16,4],0,0.01))
Qout = tf.matmul(inputs1,W)
grad_Qout_inputs1 = tf.gradients(Qout, inputs1)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
inp = np.random.random([1,16])
grad_Qout_inputs1_val = sess.run([grad_Qout_inputs1], feed_dict={inputs1:inp})
print grad_Qout_inputs1_val
@Santara
Copy link
Author

Santara commented Apr 8, 2017

The last but 1 line throws the following error:

TypeError: Fetch argument [<tf.Tensor 'gradients_3/MatMul_grad/MatMul_1:0' shape=(16, 4) dtype=float32>] of [<tf.Tensor 'gradients_3/MatMul_grad/MatMul_1:0' shape=(16, 4) dtype=float32>] has invalid type <type 'list'>, must be a string or Tensor. (Can not convert a list into a Tensor or Operation.)

@Santara
Copy link
Author

Santara commented Apr 8, 2017

This one works perfectly fine:

# define symbolic variables
x = tf.placeholder("float") 
y = tf.placeholder("float")

# define a function R=R(x,y)
R = 0.127-(x*0.194/(y+0.194))

# The derivative of R with respect to y
Rdy = tf.gradients(R, y); 

# Launch a session for the default graph to comput dR/dy at (x,y)=(0.362, 0.556)
sess = tf.Session()
result = sess.run(Rdy, {x:0.362,y:0.556})
print result
#[0.12484978]

@manila95
Copy link

manila95 commented Apr 8, 2017

Try this

import tensorflow as tf
import numpy as np

inputs1 = tf.placeholder(shape=[1,16],dtype=tf.float32)
W = tf.Variable(tf.random_uniform([16,4],0,0.01))
Qout = tf.matmul(inputs1,W)
grad_Qout_inputs1 = tf.gradients(Qout, inputs1)

init = tf.global_variables_initializer()

sess = tf.Session()
sess.run(init)

inp = np.random.random([1,16])

grad_Qout_inputs1_val = sess.run([grad_Qout_inputs1], feed_dict={inputs1:inp})
print grad_Qout_inputs1_val

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment