Merge string tensors in TensorFlow

2024/10/3 19:16:51

I work with a lot of dtype="str" data. I've been trying to build a simple graph as in https://www.tensorflow.org/versions/master/api_docs/python/train.html#SummaryWriter.

For a simple operation, I wanted to concatenate strings together using a placeholder as in (How to feed a placeholder?)

Does anyone know how to merge string tensors together?

import tensorflow as tf
sess = tf.InteractiveSession()with tf.name_scope("StringSequence") as scope:left = tf.constant("aaa",name="LEFT")middle = tf.placeholder(dtype=tf.string, name="MIDDLE")right = tf.constant("ccc",name="RIGHT")complete = tf.add_n([left,middle,right],name="COMPLETE") #fails here
sess.run(complete,feed_dict={middle:"BBB"})
#writer = tf.train.SummaryWriter("/users/mu/test_out/", sess.graph_def)
Answer

Thanks to your question, we prioritized adding support for string concatenation in TensorFlow, and added it in this commit. String concatenation is implemented using the existing tf.add() operator, to match the behavior of NumPy's add operator (including broadcasting).

To implement your example, you can write:

complete = left + middle + right

…or, equivalently, but if you want to name the resulting tensor:

complete = tf.add(tf.add(left, middle), right, name="COMPLETE")

We have not yet added support for strings in tf.add_n() (or related ops like tf.reduce_sum()) but will consider this if there are use cases for it.

NOTE: To use this functionality immediately, you will need to build TensorFlow from source. The new op will be available in the next release of TensorFlow (0.7.0).

https://en.xdnf.cn/q/70696.html

Related Q&A

How to reduce memory usage of threaded python code?

I wrote about 50 classes that I use to connect and work with websites using mechanize and threading. They all work concurrently, but they dont depend on each other. So that means 1 class - 1 website - …

Connection is closed when a SQLAlchemy event triggers a Celery task

When one of my unit tests deletes a SQLAlchemy object, the object triggers an after_delete event which triggers a Celery task to delete a file from the drive.The task is CELERY_ALWAYS_EAGER = True when…

Python escape sequence \N{name} not working as per definition

I am trying to print unicode characters given their name as follows:# -*- coding: utf-8 -*- print "\N{SOLIDUS}" print "\N{BLACK SPADE SUIT}"However the output I get is not very enco…

Binary integer programming with PULP using vector syntax for variables?

New to the python library PULP and Im finding the documentation somewhat unhelpful, as it does not include examples using lists of variables. Ive tried to create an absolutely minimalist example below …

Nonblocking Scrapy pipeline to database

I have a web scraper in Scrapy that gets data items. I want to asynchronously insert them into a database as well. For example, I have a transaction that inserts some items into my db using SQLAlchemy …

python function to return javascript date.getTime()

Im attempting to create a simple python function which will return the same value as javascript new Date().getTime() method. As written here, javascript getTime() method returns number of milliseconds …

Pulling MS access tables and putting them in data frames in python

I have tried many different things to pull the data from Access and put it into a neat data frame. right now my code looks like this.from pandas import DataFrame import numpy as npimport pyodbc from sq…

Infinite loop while adding two integers using bitwise operations?

I am trying to solve a problem, using python code, which requires me to add two integers without the use of + or - operators. I have the following code which works perfectly for two positive numbers: d…

When is pygame.init() needed?

I am studying pygame and in the vast majority of tutorials it is said that one should run pygame.init() before doing anything. I was doing one particular tutorial and typing out the code as one does an…

mypy overrides in toml are ignored?

The following is a simplified version of the toml file example from the mypy documentation: [tool.mypy] python_version = "3.7" warn_return_any = true warn_unused_configs = true[[tool.mypy.ove…