0

I am working to adapt a python2.7 code that uses keras and tensorflow to implement a CNN but looks like the keras API has changed a little bit since when the original code was idealized. I keep getting an error about "Negative dimension after subtraction" and I can not find out what is causing it.

Unfortunately I am not able to provide an executable piece of code because I was not capable of make the original code works, but the repository containing all the source files can be found here.

The piece of code:

from keras.callbacks import EarlyStopping
from keras.layers.containers import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Reshape, Flatten, Dropout, Dense
from keras.layers.embeddings import Embedding
from keras.models import Graph
from keras.preprocessing import sequence

filter_lengths = [3, 4, 5]
self.model = Graph()

'''Embedding Layer'''
self.model.add_input(name='input', input_shape=(max_len,), dtype=int)

self.model.add_node(Embedding(
        max_features, emb_dim, input_length=max_len), name='sentence_embeddings', input='input')

'''Convolution Layer & Max Pooling Layer'''
for i in filter_lengths:
    model_internal = Sequential()
    
    model_internal.add(
        Reshape(dims=(1, self.max_len, emb_dim), input_shape=(self.max_len, emb_dim))
    )
    
    model_internal.add(Convolution2D(
        nb_filters, i, emb_dim, activation="relu"))
    
    model_internal.add(
        MaxPooling2D(pool_size=(self.max_len - i + 1, 1))
    )
    
    model_internal.add(Flatten())

    self.model.add_node(model_internal, name='unit_' + str(i), input='sentence_embeddings')

What I have tried:

m = tf.keras.Sequential()

m.add(tf.keras.Input(shape=(max_len, ), name="input"))

m.add(tf.keras.layers.Embedding(max_features, emb_dim, input_length=max_len))

filter_lengths = [ 3, 4, 5 ]

for i in filter_lengths:
    model_internal = tf.keras.Sequential(name=f'unit_{i}')

    model_internal.add(
        tf.keras.layers.Reshape(( 1, max_len, emb_dim ), input_shape=( max_len, emb_dim ))
    )

    model_internal.add(
        tf.keras.layers.Convolution2D(100, i, emb_dim, activation="relu")
    )

    model_internal.add(
        tf.keras.layers.MaxPooling2D(pool_size=( max_len - i + 1, 1 ))
    )

    model_internal.add(
        tf.keras.layers.Flatten()
    )

    m.add(model_internal)

I do not expect a complete solution, what I am really trying to understand is what is the cause to the following error:

Negative dimension size caused by subtracting 3 from 1 for '{{node conv2d_5/Conv2D}} = Conv2D[T=DT_FLOAT, data_format="NHWC", dilations=[1, 1, 1, 1], explicit_paddings=[], padding="VALID", strides=[1, 200, 200, 1], use_cudnn_on_gpu=true](Placeholder, conv2d_5/Conv2D/ReadVariableOp)' with input shapes: [?,1,300,200], [3,3,200,100].

0 Answers0