concept character in category deep learning

This is an excerpt from Manning's book Deep Learning with R.
Data type—This is the type of the data contained in the tensor; for instance, a tensor’s type could be integer or double. On rare occasions, you may see a character tensor. But because tensors live in preallocated contiguous memory segments, and strings, being variable-length, would preclude the use of this implementation, they’re rarely used.
This chapter explores deep-learning models that can process text (understood as sequences of words or sequences of characters), timeseries, and sequence data in general. The two fundamental deep-learning algorithms for sequence processing are recurrent neural networks and 1D convnets, the one-dimensional version of the 2D convnets that we covered in the previous chapters. We’ll discuss both of these approaches in this chapter.
Segment text into characters, and transform each character into a vector. Extract n-grams of words or characters, and transform each n-gram into a vector. N-grams are overlapping groups of multiple consecutive words or characters. Collectively, the different units into which you can break down text (words, characters, or n-grams) are called tokens, and breaking text into such tokens is called tokenization. All text-vectorization processes consist of applying some tokenization scheme and then associating numeric vectors with the generated tokens. These vectors, packed into sequence tensors, are fed into deep neural networks. There are multiple ways to associate a vector with a token. In this section, We’ll present two major ones: one-hot encoding of tokens, and token embedding (typically used exclusively for words, and called word embedding). The remainder of this section explains these techniques and shows how to use them to go from raw text to a tensor that you can send to a Keras network.
Of course, one-hot encoding can be done at the character level, as well. To unambiguously drive home what one-hot encoding is and how to implement it, listings 6.1 and 6.2 show two toy examples: one for words, the other for characters.
Listing 8.3. Vectorizing sequences of characters
maxlen <- 60 1 step <- 3 2 text_indexes <- seq(1, nchar(text) - maxlen, by = step) sentences <- str_sub(text, text_indexes, text_indexes + maxlen - 1) 3 next_chars <- str_sub(text, text_indexes + maxlen, text_indexes + maxlen) 4 cat("Number of sequences: ", length(sentences), "\n") chars <- unique(sort(strsplit(text, "")[[1]])) 5 cat("Unique characters:", length(chars), "\n") char_indices <- 1:length(chars) 6 names(char_indices) <- chars cat("Vectorization...\n") 7 x <- array(0L, dim = c(length(sentences), maxlen, length(chars))) 7 y <- array(0L, dim = c(length(sentences), length(chars))) 7 for (i in 1:length(sentences)) { 7 sentence <- strsplit(sentences[[i]], "")[[1]] 7 for (t in 1:length(sentence)) { 7 char <- sentence[[t]] 7 x[i, t, char_indices[[char]]] <- 1 7 } 7 next_char <- next_chars[[i]] 7 y[i, char_indices[[next_char]]] <- 1 7 } 7

This is an excerpt from Manning's book Deep Learning with Python.
A dataset of tweets, where we encode each tweet as a sequence of 280 characters out of an alphabet of 128 unique characters. In this setting, each character can be encoded as a binary vector of size 128 (an all-zeros vector except for a 1 entry at the index corresponding to the character). Then each tweet can be encoded as a 2D tensor of shape (280, 128), and a dataset of 1 million tweets can be stored in a tensor of shape (1000000, 280, 128).
This chapter explores deep-learning models that can process text (understood as sequences of words or sequences of characters), timeseries, and sequence data in general. The two fundamental deep-learning algorithms for sequence processing are recurrent neural networks and 1D convnets, the one-dimensional version of the 2D convnets that we covered in the previous chapters. We’ll discuss both of these approaches in this chapter.
Of course, one-hot encoding can be done at the character level, as well. To unambiguously drive home what one-hot encoding is and how to implement it, listings 6.1 and 6.2 show two toy examples: one for words, the other for characters.
Listing 8.3. Vectorizing sequences of characters
maxlen = 60 #1 step = 3 #2 sentences = [] #3 next_chars = [] #4 for i in range(0, len(text) - maxlen, step): sentences.append(text[i: i + maxlen]) next_chars.append(text[i + maxlen]) print('Number of sequences:', len(sentences)) chars = sorted(list(set(text))) #5 print('Unique characters:', len(chars)) char_indices = dict((char, chars.index(char)) for char in chars) #6 print('Vectorization...') x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) #7 y = np.zeros((len(sentences), len(chars)), dtype=np.bool) #7 for i, sentence in enumerate(sentences): #7 for t, char in enumerate(sentence): #7 x[i, t, char_indices[char]] = 1 #7 y[i, char_indices[next_chars[i]]] = 1 #7 #1 - You’ll extract sequences of 60 characters. #2 - You’ll sample a new sequence every three characters. #3 - Holds the extracted sequences #4 - Holds the targets (the follow-up characters) #5 - List of unique characters in the corpus #6 - Dictionary that maps unique characters to their index in the list “chars” #7 - One-hot encodes the characters into binary arrays