How can I specify all metrics I need when compiling a model? - tensorflow

I have the following line of code on my model
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])
Which works perfectly and shows the accuracy after each epoch.
Now, my problem arises when I want other metrics to be shown too, like precision, recall or f1.
I don't know how can I do this?
For example, trying to add precision, I've tried this:
model.compile(loss='mean_squared_error', optimizer='adam', metrics=[metrics.Accuracy(), metrics.Precision()])
and this:
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy', 'precision'])
but none work, only 'accuracy' and 'AUC'.

Related

What layers, nodes and epoch amounts would you set for 84 columns and 14million rows of data?

I'm creating an incident intrusion detection based model and i've had trouble getting rid of false positives on my model or other classes being thrown in to the prediction, the classes might be related slightly but it's just not acceptable to me and i feel like i'm doing something incorrect. I'm using online datasets with pre-placed labels identifying each class and what a instance of data or row corresponds to. So it's a multiclass model.
What would you change with the below code? I was thinking of changing the input to the amount of features i have (84) and the hidden layer to be the average of the input and output. Thanks a lot! Also when is the perfect time to stop training? I was thinking of using early_stopping, but my 6gb rtx 3060 gpu runs out of memory after the first epoch, but only does this when i include early_stopping validation.
-14 million rows of 84 columns
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
model = Sequential()
model.add(tf.keras.Input(shape=len(X.columns,))) # input layer
model.add(Dense(32, activation='relu')) # hidden
model.add(Dense(16, activation='relu')) # hidden
model.add(Dense(15, activation='softmax')) # output
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics='accuracy')
model.fit(x_train, y_train, epochs=231, batch_size=512)

doc2Vec with keras - NN achieves zero accuracy

Hi I am applying an NN on tweets using doc2vec. Can anyone tell me, why my accuracy stays 0.0 in every epoch? Might the input vectors be the problem?
Sorry for asking beginners questions here. I´m not that advanced yet :-/
np.random.seed(seed)
model_d2v_01 = Sequential()
model_d2v_01.add(Dense(64, activation='relu', input_dim=200))
model_d2v_01.add(Dense(1, activation='sigmoid'))
model_d2v_01.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model_d2v_01.fit(train_vecs_ugdbow_tgdmm, y_train,
validation_data=(validation_vecs_ugdbow_tgdmm, y_validation),
epochs=10, batch_size=32, verbose=2)
I´m trying to combine a DBOW and a DMM Model (Vocab).
I was following more or less this tutorial: https://github.com/tthustla/twitter_sentiment_analysis_part10/blob/master/Capstone_part10.ipynb

How to use a Keras LSTM with batch size of 1 ? (avoid zero padding)

I want to train a Keras LSTM using a batch size of one.
In this case I wouldn't need zero padding so far I understood. The necessity for zero padding comes from equalizing the size of the batches, right?
It turns out this is not as easy as I thought.
My network looks like this:
model = Sequential()
model.add(Embedding(output_dim=embeddings.shape[1],
input_dim=embeddings.shape[0],
weights=[embeddings],
trainable=False))
model.add(Dropout(0.2))
model.add(Bidirectional(LSTM(100,
return_sequences=True,
activation="tanh",kernel_initializer="glorot_uniform")))
model.add(TimeDistributed(Dense(maxLabel)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=20, batch_size=1, shuffle=True)
I naively provide my training data and training labels as simple numpy array with this shape properties:
X: (2161,)
Y: (2161,)
I get now a ValueError: Error when checking target: expected activation_1 to have 3 dimensions, but got array with shape (2161, 1)
I am not sure how to satisfy this 3-D property without zero-padding which is what I want to avoid when working with batch sizes of one anyway.
Does anyone see what I am doing wrong?

Why did the Keras Sequential model give a different result compared to Model model?

I've tried a simple lstm model in keras to do a simple sentiment analysis using imdb dataset using both Sequential model and Model model, and turns out the latter gives a worse result. Here's my code :
model = Sequential()
model.add(Embedding(top_words, embedding_vector_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
It gives a result around 0.6 of accuracy in the first epoch, while the other code that use Model :
_input = Input(shape=[max_review_length], dtype='int32')
embedded = Embedding(
input_dim=top_words,
output_dim=embedding_size,
input_length=max_review_length,
trainable=False,
mask_zero=False
)(_input)
lstm = LSTM(100, return_sequences=True)(embedded)
probabilities = Dense(2, activation='softmax')(lstm)
model = Model(_input, probabilities)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
and it gives 0.5 accuracy as a result of the first epoch and never change afterwards.
Any reason for that, or am i doing something wrong? Thanks in advance
I see two main differences between your two models :
You have set the embeddings of the second model as "trainable=False". So you have probably a lot fewer parameters to optimize the second model compared to the first one.
The LSTM is returning the whole sequence in the second model, so the outputs shape will be different, so I don't see how you can compare the two models, they are not doing the same thing.

why do you need to compile a keras / tensorflow model that was loaded from a saved copy?

I understand how you save model and weights files and load them back up. I don't understand why the compilation with optimizers
loaded_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
score = loaded_model.evaluate(X_test, Y_test, verbose=0)
Since you are no longer during gradient descent, why do you need the loss function and optimizer?