Elamur Research⌕
☰
home / papers / 2023 / Hard Prompts Made Easy: Gradient-Based Discrete Optimization for Prompt Tuning and Discovery

Hard Prompts Made Easy: Gradient-Based Discrete Optimization for Prompt Tuning and Discovery

Paper · 2023 · Yuxin Wen, Neel Jain, John Kirchenbauer et al.

Summary

Prompts are the main way to steer large generative models. Readable ("hard") prompts are usually written by hand. Learned continuous ("soft") prompts are opaque, tied to a single model, and unusable through text-only APIs. The authors introduce PEZ, a gradient-based procedure that learns discrete token prompts. It borrows an idea from training quantized networks: keep a continuous copy of the prompt embeddings, snap them to the nearest vocabulary embeddings for each forward pass, compute the gradient at the snapped point, and apply it to the continuous copy. The final prompt is the projected result. For images, PEZ optimizes CLIP's image–text cosine similarity. This finds prompts describing a target image without backpropagating through a diffusion model, and those prompts can then drive Stable Diffusion. For text, PEZ learns classification prompts for language models such as GPT-2 Large. The learned prompts match or beat existing discrete optimizers and transfer across models. They also show that word-level content filters can be evaded, which matters for both prompt engineering and API safety.

Key points

  • Baselines: PEZ is compared with AutoPrompt (SGD variant) and FluentPrompt. Both project embeddings to the nearest tokens after each update, which can stall optimization and demands careful learning-rate tuning.
  • Text classification: On AGNEWS, PEZ outperforms the other methods by roughly 2%, and it is comparable on sentiment analysis. Prompts trained on GPT-2 Large with a fluency constraint transfer best, giving about a 14% gain over the bare template on OPT-6.7B.
  • Few-shot AGNEWS (GPT-2 Large, 100 seeds, three appended tokens, λ = 0.03):
    • k=2: the fluent PEZ prompt reaches 70.93 accuracy, versus 58.34 for the empty template and 74.92 for a soft prompt.
    • k=4: the fluent PEZ prompt reaches 74.15, versus 79.93 for a soft prompt.
    • Each run takes about 5 minutes. Some discovered prompts are readable, such as ones mentioning "BBC" or blogs.
  • Safety: An optimized prompt reproduced the banned "Afghan girl" image on Midjourney without using the blocked word. The authors argue that only feature-based detectors are likely to stop such workarounds.
  • Code: Available at github.com/YuxinWenRick/hard-prompts-made-easy.

Our summary of the paper, in our own words — not its text. The work belongs to its authors; read it at the links above.

 Fields, topics and concepts

Cite

Cite as Yuxin Wen, Neel Jain, John Kirchenbauer, Micah Goldblum, Jonas Geiping and Tom Goldstein. 2023. Hard Prompts Made Easy: Gradient-Based Discrete Optimization for Prompt Tuning and Discovery. arXiv:2302.03668.

The paper itself, not this page. BibTeX from the library's record of the work. DOI 10.48550/arxiv.2302.03668 · arXiv 2302.03668

@misc{wen2023hard,
  title = {{Hard Prompts Made Easy: Gradient-Based Discrete Optimization for Prompt Tuning and Discovery}},
  author = {Yuxin Wen, Neel Jain, John Kirchenbauer, Micah Goldblum, Jonas Geiping and Tom Goldstein},
  year = {2023},
  doi = {10.48550/arxiv.2302.03668},
  eprint = {2302.03668},
  archivePrefix = {arXiv},
  url = {https://doi.org/10.48550/arxiv.2302.03668}
}

References

37 references, 21 of them with a card of ours. ↗ leads to the work, «find» searches for one we have no id for yet.

  1. Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., Agarwal, S., Herbert-Voss, A., Krueger, G., Henighan, T., Child, R., Ramesh, A., Ziegler, D. M., Wu, J., Winter, C., Hesse, C., Chen, M., Sigler, E., Litwin, M., Gray, S., Ch… — Language Models are Few-Shot Learners
  2. Cherti, M., Beaumont, R., Wightman, R., Wortsman, M., Ilharco, G., Gordon, C., Schuhmann, C., Schmidt, L., and Jitsev, J. Reproducible scaling laws for contrastive language-image learning. arXiv preprint arXiv:2212.07143, 2022.
  3. Chung, H. W., Hou, L., Longpre, S., Zoph, B., Tay, Y., Fedus, W., Li, E., Wang, X., Dehghani, M., Brahma, S., et al. Scaling instruction-finetuned language models. arXiv preprint arXiv:2210.11416, 2022. find
  4. Courbariaux, M., Bengio, Y., and David, J.-P. Binaryconnect: Training deep neural networks with binary weights during propagations. Advances in neural information processing systems, 28, 2015.
  5. Courbariaux, M., Hubara, I., Soudry, D., El-Yaniv, R., and Bengio, Y. Binarized neural networks: Training deep neural networks with weights and activations constrained to+ 1 or-1. arXiv preprint arXiv:1602.02830, 2016.
  6. Deng, M., Wang, J., Hsieh, C.-P., Wang, Y., Guo, H., Shu, T., Song, M., Xing, E. P., and Hu, Z. Rlprompt: Optimizing discrete text prompts with reinforcement learning. ArXiv, abs/2205.12548, 2022.
  7. Ding, N., Hu, S., Zhao, W., Chen, Y., Liu, Z., Zheng, H., and Sun, M. OpenPrompt: An open-source framework for prompt-learning. In Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics: System Demonstrations, pp. 105–113, Dublin, Ireland, May 2022. Association for C…
  8. Ebrahimi, J., Rao, A., Lowd, D., and Dou, D. Hot-Flip: White-box adversarial examples for text classification. In Proceedings of the 56th Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers), pp. 31–36, Melbourne, Australia, July 2018. Association for Computationa…
  9. Gal, R., Alaluf, Y., Atzmon, Y., Patashnik, O., Bermano, A. H., Chechik, G., and Cohen-Or, D. An image is worth one word: Personalizing text-to-image generation using textual inversion. arXiv preprint arXiv:2208.01618, 2022. find
  10. Hu, X., Gan, Z., Wang, J., Yang, Z., Liu, Z., Lu, Y., and Wang, L. Scaling up vision-language pre-training for image captioning. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pp. 17980–17989, 2022. find
  11. Khashabi, D., Lyu, X., Min, S., Qin, L., Richardson, K., Welleck, S., Hajishirzi, H., Khot, T., Sabharwal, A., Singh, S., and Choi, Y. Prompt waywardness: The curious case of discretized interpretation of continuous prompts. In Proceedings of the 2022 Conference of the North American Chapter of the… find
  12. Kumar, S., Paria, B., and Tsvetkov, Y. Gradient-based constrained sampling from language models. arXiv preprint arXiv:2205.12558, 2022. find
  13. Lester, B., Al-Rfou, R., and Constant, N. The power of scale for parameter-efficient prompt tuning. In Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing, pp. 3045–3059, Online and Punta Cana, Dominican Republic, November 2021a. Association for Computational Lingu…
  14. Lester, B., Al-Rfou, R., and Constant, N. The Power of Scale for Parameter-Efficient Prompt Tuning. arXiv:2104.08691 [cs], September 2021b. URL http://arxiv.org/abs/2104.08691. ↗
  15. Li, H., De, S., Xu, Z., Studer, C., Samet, H., and Goldstein, T. Training quantized nets: A deeper understanding. Advances in Neural Information Processing Systems, 30, 2017. find
  16. Li, J., Li, D., Xiong, C., and Hoi, S. Blip: Bootstrapping language-image pre-training for unified vision-language understanding and generation. arXiv preprint arXiv:2201.12086, 2022.
  17. Li, X. L. and Liang, P. Prefix-tuning: Optimizing continuous prompts for generation. In Proceedings of the 59th Annual Meeting of the Association for Computational Linguistics and the 11th International Joint Conference on Natural Language Processing (Volume 1: Long Papers), pp. 4582–4597, 2021. find
  18. Lin, T.-Y., Maire, M., Belongie, S., Hays, J., Perona, P., Ramanan, D., Dollár, P., and Zitnick, C. L. Microsoft coco: Common objects in context. In European conference on computer vision, pp. 740–755. Springer, 2014.
  19. Liu, Z., Luo, P., Wang, X., and Tang, X. Deep learning face attributes in the wild. In Proceedings of the IEEE international conference on computer vision, pp. 3730– 3738, 2015. find
  20. Loshchilov, I. and Hutter, F. Decoupled weight decay regularization. arXiv preprint arXiv:1711.05101, 2017. find
  21. McAuley, J. and Leskovec, J. Hidden factors and hidden topics: Understanding rating dimensions with review text. In Proceedings of the 7th ACM Conference on Recommender Systems, RecSys ’13, pp. 165–172, New York, NY, USA, 2013. Association for Computing Machinery. ISBN 9781450324090. doi: 10.1145/25… find
  22. Prasad, A., Hase, P., Zhou, X., and Bansal, M. Grips: Gradient-free, edit-based instruction search for prompting large language models. arXiv preprint arXiv:2203.07281, 2022.
  23. Radford, A., Wu, J., Child, R., Luan, D., Amodei, D., and Sutskever, I. Language Models are Unsupervised Multi-task Learners. OpenAI, pp. 24, 2019. find
  24. Radford, A., Kim, J. W., Hallacy, C., Ramesh, A., Goh, G., Agarwal, S., Sastry, G., Askell, A., Mishkin, P., Clark, J., et al. Learning transferable visual models from natural language supervision. In International conference on machine learning, pp. 8748–8763. PMLR, 2021.
  25. Rando, J., Paleka, D., Lindner, D., Heim, L., and Tramèr, F. Red-teaming the stable diffusion safety filter. ArXiv, abs/2210.04610, 2022. — [arXiv:2210.04610](https://arxiv.org/abs/2210.04610) find
  26. Rastegari, M., Ordonez, V., Redmon, J., and Farhadi, A. Xnor-net: Imagenet classification using binary convolutional neural networks. In European conference on computer vision, pp. 525–542. Springer, 2016.
  27. Rombach, R., Blattmann, A., Lorenz, D., Esser, P., and Ommer, B. High-resolution image synthesis with latent diffusion models. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pp. 10684–10695, 2022.
  28. Sanh, V., Webson, A., Raffel, C., Bach, S., Sutawika, L., Alyafeai, Z., Chaffin, A., Stiegler, A., Raja, A., Dey, M., Bari, M. S., Xu, C., Thakker, U., Sharma, S. S., Szczechla, E., Kim, T., Chhablani, G., Nayak, N., Datta, D., Chang, J., Jiang, M. T.-J., Wang, H., Manica, M., Shen, S., Yong, Z. X.,… — Multitask Prompted Training Enables Zero-Shot Task Generalization
  29. Santana, G. Gustavosta/Stable-Diffusion-Prompts · Datasets at Hugging Face, December 2022. URL https://huggingface.co/datasets/Gustavosta/Stable-Diffusion-Prompts. ↗
  30. Schuhmann, C., Beaumont, R., Vencu, R., Gordon, C., Wightman, R., Cherti, M., Coombes, T., Katta, A., Mullis, C., Wortsman, M., et al. Laion-5b: An open large-scale dataset for training next generation image-text models. arXiv preprint arXiv:2210.08402, 2022.
  31. Shazeer, N. and Stern, M. Adafactor: Adaptive learning rates with sublinear memory cost. In International Conference on Machine Learning, pp. 4596–4604. PMLR, 2018. find
  32. Shi, W., Han, X., Gonen, H., Holtzman, A., Tsvetkov, Y., and Zettlemoyer, L. Toward human readable prompt tuning: Kubrick’s the shining is a good movie, and a good prompt too? arXiv preprint arXiv:2212.10539, 2022. find
  33. Shin, T., Razeghi, Y., Logan IV, R. L., Wallace, E., and Singh, S. AutoPrompt: Eliciting Knowledge from Language Models with Automatically Generated Prompts. In Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP), pp. 4222– 4235, Online, November 2020. Asso…
  34. Socher, R., Perelygin, A., Wu, J., Chuang, J., Manning, C. D., Ng, A., and Potts, C. Recursive deep models for semantic compositionality over a sentiment treebank. In Proceedings of the 2013 Conference on Empirical Methods in Natural Language Processing, pp. 1631– 1642, Seattle, Washington, USA, Oct…
  35. Zhang, P., Li, X., Hu, X., Yang, J., Zhang, L., Wang, L., Choi, Y., and Gao, J. Vinvl: Revisiting visual representations in vision-language models. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, pp. 5579–5588, 2021. find
  36. Zhang, S., Roller, S., Goyal, N., Artetxe, M., Chen, M., Chen, S., Dewan, C., Diab, M., Li, X., Lin, X. V., et al. Opt: Open pre-trained transformer language models. arXiv preprint arXiv:2205.01068, 2022.
  37. Zhang, X., Zhao, J., and LeCun, Y. Character-level convolutional networks for text classification. Advances in neural information processing systems, 28, 2015.

Updated

↑ back to top

This page in Markdown → https://md.research.elamur.ai/free/en/papers/wen-2023-hard-prompts-made-easy-gradient/