How to transform a neural network model using tflearn to tensorflow

175 views Asked by At

In this model I preprocessed some data using nlp, I think it was correct but when I tried to use tensorflow for the creation of the neural network it didn't work, it's actually fitting with no problem but when I do the predictions it doesn't work. what is the problem pls.

# create our training data
training = []
output = []
# create an empty array for our output
output_empty = [0] * len(classes)

# training set, bag of words for each sentence
for doc in documents:
    # initialize our bag of words
    bag = []
    # list of tokenized words for the pattern
    pattern_words = doc[0]
    # stem each word
    pattern_words = [stemmer.stem(word.lower()) for word in pattern_words]
    # create our bag of words array
    for w in words:
        bag.append(1) if w in pattern_words else bag.append(0)

    # output is a '0' for each tag and '1' for current tag
    output_row = list(output_empty)
    output_row[classes.index(doc[1])] = 1

    training.append([bag, output_row])

# shuffle our features and turn into np.array
random.shuffle(training)
training = np.array(training)

# create train and test lists
train_x = list(training[:,0])
train_y = list(training[:,1])
#model creation using tflearn
net = tflearn.input_data(shape=[None, len(train_x[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(train_y[0]), activation='softmax')
net = tflearn.regression(net)
#my own transformation to tensorflow 
model = keras.Sequential([
    keras.layers.Dense(units=100, input_shape=(None,len(train_x[0]))), 
    keras.layers.Dense(units=40, activation='relu'), 
    keras.layers.Dense(units=40, activation='relu'),
    keras.layers.Dense(units=3, activation='softmax')
    
])
#compile our project 
model.compile(optimizer='sgd', 
              loss=tf.losses.CategoricalCrossentropy(),
              metrics=['accuracy'])
#fit our model 
history = model.fit(
    train_x,
    train_y,
    epochs=500,
    steps_per_epoch=5,
    batch_size=8,
)

but while fitting the model i get this warning:

WARNING:tensorflow:Model was constructed with shape (None, None, 17)
for input Tensor("dense_20_input:0", shape=(None, None, 17),
dtype=float32), but it was called on an input with incompatible shape
(None, 17).
1

There are 1 answers

0
AudioBubble On

The Warning occurs because Keras adds an additional dimension to the data, which is the batch size. Changing the following line

keras.layers.Dense(units=100, input_shape=(None,len(train_x[0]))),

with the following line will help.

keras.layers.Dense(units=100, input_shape=(train_x[0].shape)),