How do I evaluate tf.sign(grad)?

25 views
Skip to first unread message

ephi...@yahoo.com

unread,
Nov 28, 2018, 5:31:56 PM11/28/18
to cleverhans dev

In the cleverhans\attacks.py code, the tensor "grad" is the gradient of the loss function w.r.t. "x", where x is a tensor of image data dtype=float32, shape (10000, 32, 32, 3).
How do I evaluate tf.sign(grad)? what scalar or numpy array data should I feed it?

  sess = tf.Session()
  
with sess.as_default():
         aa
=tf.sign(grad)  
         bb
=aa.eval(session=sess, feed_dict={x??: x_test?? })

Ian Goodfellow

unread,
Nov 28, 2018, 5:39:52 PM11/28/18
to ephi...@yahoo.com, cleverhans dev
The key in feed_dict should be the TensorFlow representation of the input to the network. The value should be the NumPy representation.

You might have something like

x = tf.placeholder(...)
loss = cross_entropy(labels=y, logits=model.get_logits(x))
grad, = tf.gradients(loss, x)
sg = tf.sign(grad)
with sess.as_default():
   sgv = sg.eval(feed_dict={x: MNIST.x_test})

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-de...@googlegroups.com.
To post to this group, send email to cleverh...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cleverhans-dev/0fac7a9a-0d7e-4cb4-ac18-b48b797e23ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ephi5757

unread,
Nov 28, 2018, 9:32:21 PM11/28/18
to Ian Goodfellow, cleverhans dev
Excellent. Thank you for your kind and expert help. Once I have established some helpful software scripts, I am looking forward to developing additional adversarial examples through understanding and execution of the attacks in the cleverhans library. 
Thanks for your patience. AT


Sent via the Samsung Galaxy S® 4 mini ™, an AT&T 4G LTE smartphone
Message has been deleted

ephi...@yahoo.com

unread,
Dec 6, 2018, 6:39:49 PM12/6/18
to cleverhans dev
in mnist_tutorial_picklable.py I tried the following based on your reply:

  x = tf.placeholder(tf.float32, shape=(None, img_rows, img_cols,
                                        nchannels
))
    loss 
= CrossEntropy(model, smoothing=label_smoothing)  
 
# loss=cross_entropy(labels=y, logits=model.get_logits(x)) did not appear to work. CrossEntropy seems to need the primary "model" input parameter.

    grad
, = tf.gradients(loss, x)
    sg 
= tf.sign(grad)

 

    sgv 
= sg.eval(session=sess, feed_dict={x: x_test})
    np
.save('pert.npy',sgv)

However, the following error occurs at    grad, = tf.gradients(loss, x):
TypeError: Expected binary or unicode string, got <cleverhans.loss.CrossEntropy object at 0x000001F297A67B38>
TypeError: Failed to convert object of type <class 'cleverhans.loss.CrossEntropy'> to Tensor. Contents: <cleverhans.loss.CrossEntropy object at 0x000001F297A67B38>. Consider casting elements to a supported type.

I'm not sure why the tf.gradients() function is failing. 
Any comments that you may offer would be helpful.
Best,AT

On Wednesday, November 28, 2018 at 9:32:21 PM UTC-5, ephi...@yahoo.com wrote:
Excellent. Thank you for your kind and expert help. Once I have established some helpful software scripts, I am looking forward to developing additional adversarial examples through understanding and execution of the attacks in the cleverhans library. 
Thanks for your patience. AT


Sent via the Samsung Galaxy S® 4 mini ™, an AT&T 4G LTE smartphone


-------- Original message --------
From: Ian Goodfellow <goodfel...@gmail.com>
Date:11/28/2018 5:39 PM (GMT-05:00)
Cc: cleverhans dev <cleverhans-dev@googlegroups.com>
Subject: Re: How do I evaluate tf.sign(grad)?

The key in feed_dict should be the TensorFlow representation of the input to the network. The value should be the NumPy representation.

You might have something like

x = tf.placeholder(...)
loss = cross_entropy(labels=y, logits=model.get_logits(x))
grad, = tf.gradients(loss, x)
sg = tf.sign(grad)
with sess.as_default():
   sgv = sg.eval(feed_dict={x: MNIST.x_test})

On Wed, Nov 28, 2018 at 5:31 PM 'ephi...@yahoo.com' via cleverhans dev <cleverhans-dev@googlegroups.com> wrote:

In the cleverhans\attacks.py code, the tensor "grad" is the gradient of the loss function w.r.t. "x", where x is a tensor of image data dtype=float32, shape (10000, 32, 32, 3).
How do I evaluate tf.sign(grad)? what scalar or numpy array data should I feed it?

  sess = tf.Session()
  
with sess.as_default():
         aa
=tf.sign(grad)  
         bb
=aa.eval(session=sess, feed_dict={x??: x_test?? })

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cleverhans-dev@googlegroups.com.

Ian Goodfellow

unread,
Dec 6, 2018, 6:41:23 PM12/6/18
to ephi...@yahoo.com, cleverhans dev
You need to call `loss.fprop` to get a TensorFlow Tensor. In the code you have there `loss` is an instance of the CrossEntropy class. It is an object that knows how to build the Tensor you need, but it's not a Tensor itself.

On Wed, Nov 28, 2018 at 5:31 PM 'ephi...@yahoo.com' via cleverhans dev <cleverh...@googlegroups.com> wrote:

In the cleverhans\attacks.py code, the tensor "grad" is the gradient of the loss function w.r.t. "x", where x is a tensor of image data dtype=float32, shape (10000, 32, 32, 3).
How do I evaluate tf.sign(grad)? what scalar or numpy array data should I feed it?

  sess = tf.Session()
  
with sess.as_default():
         aa
=tf.sign(grad)  
         bb
=aa.eval(session=sess, feed_dict={x??: x_test?? })

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-de...@googlegroups.com.
To post to this group, send email to cleverh...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-de...@googlegroups.com.
To post to this group, send email to cleverh...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cleverhans-dev/9811d850-8da9-4e2b-963e-5d4773572baf%40googlegroups.com.

ephi...@yahoo.com

unread,
Dec 7, 2018, 1:03:30 PM12/7/18
to cleverhans dev
Yes. Thank you. calling loss.fprop worked nicely.
Nevertheless, I am having trouble to debug this added code to complete my task to visualize the perturbation from an FSGM attack.
At the next to last line of code, i.e., sgv = sg.eval(feed_dict={x: x_test}), the program gives the following error:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10].
Then, when I change the code as sgv = sg.eval(feed_dict={y: y_test}), the error is...
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,28,28,1]
Note that in debugging mode I was able to determine that...
x.shape=[None, 28, 28, 1]; y.shape=[None, 10]; sg.shape=[None, 28, 28, 1], grad.shape=[None, 28, 28, 1], x_test.shape=(10000, 28, 28, 1).
Thank you in advance for your time and kind help.
Best, AT

FYI, my code is:

    x = tf.placeholder(tf.float32, shape=(None, img_rows, img_cols, nchannels))
    y = tf.placeholder(tf.float32, shape=(None, nb_classes))
   
loss = CrossEntropy(model, smoothing=label_smoothing)
    losses
= loss.fprop(x, y)
    grad
, = tf.gradients(losses, x)
    sg
= tf.sign(grad)
    x_sample
= x_test[: ,:, :, :]  
    adv_x_sample
= adv_x.eval(session=sess, feed_dict={x: x_test})  
    np
.save('legitx.npy',x_sample)
    np
.save('adv-x-p1.npy',adv_x_sample)
   
with sess.as_default():
        sgv
= sg.eval(feed_dict={x: x_test})
        np
.save('pert-p1.npy',sgv)



On Thursday, December 6, 2018 at 6:41:23 PM UTC-5, Ian Goodfellow wrote:
You need to call `loss.fprop` to get a TensorFlow Tensor. In the code you have there `loss` is an instance of the CrossEntropy class. It is an object that knows how to build the Tensor you need, but it's not a Tensor itself.

On Wed, Nov 28, 2018 at 5:31 PM 'ephi...@yahoo.com' via cleverhans dev <cleverhans-dev@googlegroups.com> wrote:

In the cleverhans\attacks.py code, the tensor "grad" is the gradient of the loss function w.r.t. "x", where x is a tensor of image data dtype=float32, shape (10000, 32, 32, 3).
How do I evaluate tf.sign(grad)? what scalar or numpy array data should I feed it?

  sess = tf.Session()
  
with sess.as_default():
         aa
=tf.sign(grad)  
         bb
=aa.eval(session=sess, feed_dict={x??: x_test?? })

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cleverhans-dev@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cleverhans-dev@googlegroups.com.

Ian Goodfellow

unread,
Dec 7, 2018, 4:58:20 PM12/7/18
to ephi...@yahoo.com, cleverhans dev
You need to pass *both* placeholders in one dictionary.

To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-de...@googlegroups.com.
To post to this group, send email to cleverh...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-de...@googlegroups.com.
To post to this group, send email to cleverh...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-de...@googlegroups.com.
To post to this group, send email to cleverh...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cleverhans-dev/0d8689e3-2218-4394-829b-2d7cdb74ab36%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Sent from Gmail Mobile

ephi...@yahoo.com

unread,
Dec 8, 2018, 7:15:55 PM12/8/18
to cleverhans dev
Thank you. I am looking forward to applying this update asap Monday. FYI, I received an email response offline to suggest the same.
Thanks again. AT

--- On Fri, 12/7/18, a... dalal <kkr....@gmail.com> wrote:
> You need to feed both the placeholders x and y together i.e. sg.eval(feed_dict={x: x_test, y:y_test}) because sg graph evaluation need both the placeholders.


On Friday, December 7, 2018 at 4:58:20 PM UTC-5, Ian Goodfellow wrote:
You need to pass *both* placeholders in one dictionary.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cleverhans-dev@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cleverhans-dev@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "cleverhans dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cleverhans-dev+unsubscribe@googlegroups.com.
To post to this group, send email to cleverhans-dev@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages