Error in last layer of neural network - python-3.x

#10-Fold split
seed = 7
kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed)
np.random.seed(seed)
cvscores = []
act = 'relu'
for train, test in kfold.split(X, Y):
model = Sequential()
model.add(Dense(43, input_shape=(8,)))
model.add(Activation(act))
model.add(Dense(500))
model.add(Activation(act))
#model.add(Dropout(0.4))
model.add(Dense(1000))
model.add(Activation(act))
#model.add(Dropout(0.4))
model.add(Dense(1500))
model.add(Activation(act))
#model.add(Dropout(0.4))
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
hist = model.fit(X[train], Y[train],
epochs=500,
shuffle=True,
batch_size=100,
validation_data=(X[test], Y[test]), verbose=2)
#model.summary()
When I call model.fit it reports the following error :
ValueError: Error when checking target: expected activation_5 to have shape (None, 2) but got array with shape (3869, 1)
I am using keras with TensorFlow backend. Please ask for any further clarification if needed.

The problem was solved when we used this statement
y = to_categorical(Y[:])

Related

How to fix error validation split in MLP using Keras?

I'm newbie in Neural Network. I'm going to do a text classification research using MLP model with keras. Input layer consisting of 900 nodes, 2 hidden layers, and 2 outputs.
The code I use is as follows:
#Split data training & testing (90:10)
Train_X, Test_X, Train_Y, Test_Y = model_selection.train_test_split(dataset['review'],dataset['sentimen'],test_size=0.2, random_state=8)
Encoder = LabelEncoder()
Train_Y = Encoder.fit_transform(Train_Y)
Test_Y = Encoder.fit_transform(Test_Y)
Tfidf_vect = TfidfVectorizer(max_features=None)
Tfidf_vect.fit(dataset['review'])
Train_X_Tfidf = Tfidf_vect.transform(Train_X)
Test_X_Tfidf = Tfidf_vect.transform(Test_X)
#ANN Architecture
model = Sequential()
model.add(Dense(units = 100, activation = 'sigmoid', input_shape=(32, 900)))
model.add(Dense(units = 100, activation = 'sigmoid'))
model.add(Dense(units = 2, activation = 'sigmoid'))
opt = Adam (learning_rate=0.001)
model.compile(loss = 'binary_crossentropy', optimizer = opt,
metrics = ['accuracy'])
print(model.summary())
#Hyperparameter
epochs= 100
batch_size= 32
es = EarlyStopping(monitor="val_loss",mode='min',patience=10)
model_prediction = model.fit(Train_X_Tfidf, Train_Y, epochs=epochs,
batch_size=batch_size, verbose=1,
validation_split=0.1, callbacks =[es])
But getting Error:
/usr/local/lib/python3.8/dist-packages/keras/engine/data_adapter.py in train_validation_split(arrays, validation_split)
1478 unsplitable = [type(t) for t in flat_arrays if not _can_split(t)]
1479 if unsplitable:
-> 1480 raise ValueError(
1481 "`validation_split` is only supported for Tensors or NumPy "
1482 "arrays, found following types in the input: {}".format(unsplitable))
ValueError: `validation_split` is only supported for Tensors or NumPy arrays, found following types in the input: [<class 'scipy.sparse.csr.csr_matrix'>]
How to Fix it? Thank you so much.

dealing with sparse data on TensorFlow 2.2.0

I'm trying to make a Face Recognition app using insightface, I wrote this code on Tensorflow 2.1.0 and Keras 2.3.1 and it worked well but due to some issues I have to migrate to TensorFlow 2.2.0 and Keras 2.4.3, I understand that my problem is my embeddings. they are sparse but in a meaningful way. How can I avoid changing the meaningfulness for my embeddings and avoid the spares data? From the error, I understand (Consider casting elements to a supported type.) that TensorFlow can't convert my np.array to tensor because it is sparse.
what I tried
these arent the commands but I wrote them so you would have a notion of what I tried. np.array(data["embeddings"]).todense(),csr_matrix(data["embeddings"]), tf.convert_to_tensor(data["embeddings"]) and also tried to follow along this but couldn't get to model.fit_generator work.
>>> print(type(embeddings))
<class 'numpy.ndarray'>
>>> print(embeddings.shape)
(49, 512)
>>> print(embeddings)
[[ 0.02751185 0.0143353 0.0324492 ... -0.00347222 0.0154978
-0.01304669]
[ 0.09154768 -0.04196533 0.01197386 ... -0.08363352 0.03335601
0.01748604]
[ 0.00182035 -0.00307933 0.00386595 ... -0.04442558 0.04434329
0.06080627]
...
[-0.01564891 -0.01510727 0.0345119 ... -0.01690779 -0.00816008
0.08056415]
[-0.00543963 -0.03811216 -0.01148985 ... -0.05366111 0.07108331
-0.00186215]
[ 0.00627459 -0.04221528 0.00426272 ... 0.02838095 0.02116473
0.00491964]]
This is my code:
class SoftMax():
def __init__(self, input_shape, num_classes):
self.input_shape = input_shape
self.num_classes = num_classes
def build(self):
#create model
model = Sequential()
#add model layers
model.add(Dense(1024, activation='relu', input_shape=self.input_shape))
model.add(Dropout(0.5))
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(self.num_classes, activation='softmax'))
# loss and optimizer
optimizer=Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(loss=categorical_crossentropy,
optimizer=optimizer,
metrics=['accuracy'])
return model
def make_model(args, classifier=SoftMax):
# Load the face embeddings
data = pickle.loads(open(args.embeddings, "rb").read())
num_classes = len(np.unique(data["names"]))
ct = ColumnTransformer([('myŁ”Name', OneHotEncoder(), [0])])
labels = np.array(data["names"]).reshape(-1, 1)
labels = ct.fit_transform(labels)
embeddings = np.array(data["embeddings"])
# Initialize Softmax training model arguments
BATCH_SIZE = 32
EPOCHS = 32
input_shape = embeddings.shape[1]
# Build classifier
init_classifier = classifier(input_shape=(input_shape,), num_classes=num_classes)
model = init_classifier.build()
# Create KFold
cv = KFold(n_splits = 5, random_state = None, shuffle=True)
history = {'acc': [], 'val_acc': [], 'loss': [], 'val_loss': []}
# Train
for train_idx, valid_idx in cv.split(embeddings):
X_train, X_val, y_train, y_val = embeddings[train_idx], embeddings[valid_idx], labels[train_idx], labels[valid_idx]
his = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, verbose=1, validation_data=(X_val, y_val))
# write the face recognition model to output
model.save(args.mymodel)
f = open(args.le, "wb")
f.write(pickle.dumps(LabelEncoder()))
f.close()
Error
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("DeserializeSparse:0", shape=(None, 2), dtype=int64), values=Tensor("DeserializeSparse:1", shape=(None,), dtype=float32), dense_shape=Tensor("stack:0", shape=(2,), dtype=int64)). Consider casting elements to a supported type.

CNN with keras: Input 0 is incompatible with layer flatten_2: expected min_ndim=3, found ndim=2

I don't understand this error here is my model
L_branch = Sequential()
L_branch.add(Embedding(vocab_size, output_dim=15, input_length=3000, trainable=True))
L_branch.add(Conv1D(50, activation='relu', kernel_size=70, input_shape=(3000, )))
L_branch.add(MaxPooling1D(15))
L_branch.add(Flatten())
# second model
R_branch = Sequential()
R_branch.add(Dense(14, input_shape=(14,), activation='relu'))
R_branch.add(Flatten())
merged = Concatenate()([L_branch.output, R_branch.output])
out = Dense(70, activation='softmax')(merged)
final_model = Model([L_branch.input, R_branch.input], out)
final_model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
final_model.summary()
final_model.fit(
[input1, input2],
Y_train,
batch_size=200,
epochs=1,
verbose=1,
validation_split=0.1
)
where input 1 has the shape (5039, 3000)
and input 2 (5039, 14)
so why is the flatten dense asking for a third dimension? if dense do not change the number of dimensions like embedding layer or convolution?
remove Flatten layer... no need to use it. here the full structure
L_branch = Sequential()
L_branch.add(Embedding(vocab_size, output_dim=15, input_length=3000, trainable=True))
L_branch.add(Conv1D(50, activation='relu', kernel_size=70, input_shape=(3000, )))
L_branch.add(MaxPooling1D(15))
L_branch.add(Flatten())
# second model
R_branch = Sequential()
R_branch.add(Dense(14, input_shape=(14,), activation='relu'))
merged = Concatenate()([L_branch.output, R_branch.output])
out = Dense(70, activation='softmax')(merged)
final_model = Model([L_branch.input, R_branch.input], out)
final_model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
final_model.summary()

Hyperparameter optimization in images with Talos and flow_from_directory

I tried to optimize the hyperparameters of my keras CNN made for image classification. I consider using grid search from sklearn and talos optimizer (https://github.com/autonomio/talos). I overcame the fundamental difficulty with making x and y out from flow_from_directory (code below), but... it still does not work! Any idea? Maybe someone faced the same problem.
def talos_model(train_flow, validation_flow, nb_train_samples, nb_validation_samples, params):
model = Sequential()
model.add(Conv2D(6,(5,5),activation="relu",padding="same",
input_shape=(img_width, img_height, 3)))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(params['dropout']))
model.add(Conv2D(16,(5,5),activation="relu"))
model.add(MaxPooling2D((2,2)))
model.add(Dropout(params['dropout']))
model.add(Flatten())
model.add(Dense(120, activation='relu', kernel_initializer=params['kernel_initializer']))
model.add(Dropout(params['dropout']))
model.add(Dense(84, activation='relu', kernel_initializer=params['kernel_initializer']))
model.add(Dropout(params['dropout']))
model.add(Dense(10, activation='softmax'))
model.compile(loss=params['loss'],
optimizer=params['optimizer'],
metrics=['categorical_accuracy'])
checkpointer = ModelCheckpoint(filepath='talos_cnn.h5py',
monitor='val_categorical_accuracy', save_best_only=True)
history = model.fit_generator(generator=train_flow,
samples_per_epoch=nb_train_samples,
validation_data=validation_flow,
nb_val_samples=nb_validation_samples,
callbacks=[checkpointer],
verbose=1,
epochs=params['epochs'])
return history, model
train_generator = ImageDataGenerator(rescale=1/255)
validation_generator = ImageDataGenerator(rescale=1/255)
# Retrieve images and their classes for train and validation sets
train_flow = train_generator.flow_from_directory(directory=train_data_dir,
batch_size=batch_size,
target_size(img_height,img_width))
validation_flow = validation_generator.flow_from_directory(directory=validation_data_dir,
batch_size=batch_size,
target_size=(img_height,img_width),
shuffle = False)
#here I make x and y for talos
(X_train, Y_train) = train_flow.next()
#starting an experiment with talos
t = ta.Scan(x=X_train,
y=Y_train,
model=talos_model,
params=params,
dataset_name='landmarks',
experiment_no='1')
Error occurs in the last line:
The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Wrong dimensions when trying to use model.predict() from Keras

I think the code will speak for itself, but i trained a model, that i now wanna use to predict on some new input data. The new input data seems to be the wrong dimensions though. Below you can see the code and error messages for both the model and the predicting (attempted)
tokenizer = Tokenizer(num_words=10000)
df = pd.read_csv('/home/paperspace/Sentiment Analysis Dataset.csv', index_col = 0,
error_bad_lines = False)
y = list(df['Sentiment'])
tokenizer.fit_on_texts(list(df['SentimentText']))
X = tokenizer.texts_to_sequences(list(df['SentimentText']))
X = pad_sequences(X)
print("Done, fitting on texts.")
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, shuffle = True)
model = Sequential()
#Creates the wordembeddings.
embedding_vector_dim = 32
model.add(Embedding(10000, embedding_vector_dim, input_length=X.shape[1]))
model.add(Dropout(0.2))
model.add(LSTM(128))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.summary()
model.fit(numpy.array(X_train), numpy.array(y_train),
batch_size=128,
epochs=1,
validation_data=(numpy.array(X_test), numpy.array(y_test)))
score, acc = model.evaluate(numpy.array(X_test),numpy.array(y_test),
batch_size=128)
model.save('./sentiment_seq.h5')
print('Test score:', score)
print('Test accuracy:', acc)
Now for the trying to predict and error message.
text = "this is actually a very bad movie."
tokenizer = Tokenizer()
tokenizer.fit_on_texts(list(text))
X = tokenizer.texts_to_sequences(list(text))
X = pad_sequences(X)
X_flat = np.array([X.flatten()])
model = load_model('sentiment_test.h5')
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print(model.predict(X, batch_size = 1, verbose = 1))
ValueError: Error when checking : expected embedding_1_input to have shape (None, 116) but got array with shape (1, 38)
So basically why am i getting this error, when preprocessing is the same when training and predicting, and how can i know what the expected input should be before seeing the error message?
If you're not working with a fixed input length, you should not define an input_length in the embedding layer.

Resources