Tensorflow dense layer

Viewed 164

im making a neural network called EDITH and when i compile it, it works, here is the code for the neural network data creator:

words = [lemmatizer.lemmatize(word) for word in words if word not in ignore_letters]
words = sorted(set(words))

classes = sorted(set(classes))

pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))

traning = []
output_empty = [0] * len(classes)

for document in documents:
    bag = []
    word_patterns = document[0]
    word_patterns = [lemmatizer.lemmatize(word.lower()) for word in word_patterns]
    for word in words:
        bag.append(1) if word in word_patterns else bag.append(0)

    output_row = list(output_empty)
    output_row[classes.index(document[1])] = 1
    traning.append([bag, output_row])

random.shuffle(traning)
traning = np.array(traning)

tx = list(traning[:, 0])
ty = list(traning[:, 1])

model = Sequential()
model.add(Dense(128, input_shape=(len(tx[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(ty[0]), activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])

hist = model.fit(np.array(tx), np.array(ty), epochs=10000, batch_size=5, verbose=1)
model.save('edith.h5', hist)
print("Done")

And the run program to use the data compiled by the code above, it gives me this error:

ValueError: Exception encountered when calling layer "sequential" (type Sequential).

Input 0 of layer "dense" is incompatible with the layer: expected axis -1of input shape to have value 11, but received input with shape (None, 1)

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 1), dtype=int32)
  • training=False
  • mask=None
1 Answers

Issue is with y_train shape, expected 11 labels.Looks like you have given 1.

Working sample code

import tensorflow as tf
import numpy as np

X_train = np.random.random((1512,18))
y_train = np.random.random((1512,11))

dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
train_data = dataset.shuffle(len(X_train)).batch(32)
train_data = train_data.prefetch(
        buffer_size=tf.data.experimental.AUTOTUNE)

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(128, input_shape=(18,), activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(11, activation='softmax'))

model.compile(optimizer='adam',
                loss=tf.keras.losses.CategoricalCrossentropy(),
                metrics=['accuracy'])
hist = model.fit(train_data, epochs=5, batch_size=5, verbose=1)

Output

Epoch 1/5
48/48 [==============================] - 1s 5ms/step - loss: 16.0157 - accuracy: 0.0893
Epoch 2/5
48/48 [==============================] - 0s 5ms/step - loss: 68.8496 - accuracy: 0.0906
Epoch 3/5
48/48 [==============================] - 0s 5ms/step - loss: 273.6717 - accuracy: 0.0893
Epoch 4/5
48/48 [==============================] - 0s 5ms/step - loss: 692.3754 - accuracy: 0.0681
Epoch 5/5
48/48 [==============================] - 0s 5ms/step - loss: 1339.0580 - accuracy: 0.0939
Related