<jupyter_start><jupyter_text>1-D Convonets  Introduction - 1-D convonents can be used for text data.- They are faster than RNNs and at times more useful for audio / signal processing. - Such 1D convolution layers can recognize local patterns in a sequence. Because thesame input transformation is performed on every patch, a pattern learned at a certainposition in a sentence can later be recognized at a different position, making 1D convnetstranslation invariant (for temporal translations). - For instance, a 1D convnet processingsequences of characters using convolution windows of size 5 should be able tolearn words or word fragments of length 5 or less - The 2D poolingm operation has a 1D equivalent: extracting 1D patches (subsequences) from an inputand outputting the maximum value (max pooling) or average value (average pooling).- Just as with 2D convnets, this is used for reducing the length of 1D inputs (subsampling).  Working with IMDB<jupyter_code>from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.models import Sequential
from keras import layers
from keras import optimizers
max_features = 1000       # Consider only top 1000 words as features
max_len = 500             # Maximum length of review 500
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
print("Number of reviews in train = %d" %(len(x_train)))
print("Number of reviews in test = %d" %(len(x_test)))
x_padded_train = sequence.pad_sequences(x_train, maxlen = max_len)
x_padded_test = sequence.pad_sequences(x_test, maxlen= max_len)<jupyter_output><empty_output><jupyter_text>Tips for using 1D Convonets - 1D convnets are structured in the same way as their 2D counterparts, which you usedin chapter 5: they consist of a stack of Conv1D and MaxPooling1D layers, ending ineither a global pooling layer or a Flatten layer, that turn the 3D outputs into 2D outputs,allowing you to add one or more Dense layers to the model for classification orregression.- One difference, though, is the fact that you can afford to use larger convolutionwindows with 1D convnets. - With a 2D convolution layer, a 3 × 3 convolution windowcontains 3 × 3 = 9 feature vectors; but with a 1D convolution layer, a convolution windowof size 3 contains only 3 feature vectors. - You can thus easily afford 1D convolution windows of size 7 or 9.<jupyter_code>model = Sequential()
model.add(layers.Embedding(max_features, 128, input_length=max_len))
model.add(layers.Conv1D(32, kernel_size=7, activation='relu'))
model.add(layers.MaxPool1D(5))
model.add(layers.Conv1D(32, kernel_size=7, activation='relu'))
model.add(layers.GlobalMaxPool1D())
model.add(layers.Dense(1))

opt = optimizers.Adam(lr = 0.001, decay=1e-5)
model.compile(optimizer =  opt, loss = 'binary_crossentropy', metrics=['acc'])
model.summary()
history = model.fit(x_padded_train, y_train, validation_data=(x_padded_test, y_test) ,epochs = 2, batch_size=32, verbose = 1)<jupyter_output>Train on 25000 samples, validate on 25000 samples
Epoch 1/2
25000/25000 [==============================] - 130s 5ms/step - loss: 0.6560 - acc: 0.6531 - val_loss: 0.5343 - val_acc: 0.7648
Epoch 2/2
25000/25000 [==============================] - 133s 5ms/step - loss: 0.4605 - acc: 0.8052 - val_loss: 0.4623 - val_acc: 0.8074
{'fraction_non_alphanumeric': 0.09513677811550152, 'fraction_numerical': 0.04680851063829787, 'mean_word_length': 5.244781783681215, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '282969', 'license': 'MIT', 'n_tokens_mistral': 1110, 'n_tokens_neox': 962, 'n_words': 395, 'path': 'Learning_Keras/Reccurent_Networks/Convonet_1D_Sequence.ipynb', 'repo_name': 'oke-aditya/Machine_Learning', 'size': '3290'}