cppflow icon indicating copy to clipboard operation
cppflow copied to clipboard

Difficulty with problem having multiple inputs and outputs

Open mohamedmkamra opened this issue 3 years ago • 9 comments

I am trying to load a model that has multiple inputs and outputs. I followed you example but I keep getting segmentation faults errors. The model is created using the following python code:

def build_model(X_train, d , n ):

    n_inputs = X_train.shape[1]
    n_outputs = Y_train.shape[1]
    model = Sequential()
    model.add(Dense(128, input_dim=n_inputs, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    model.add(Dropout(d))
    # model.add(Dense(32, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    # model.add(Dropout(0))
    model.add(Dense(n_outputs))
    optimizer  = tf.keras.optimizers.Adam( learning_rate=0.001  , beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, name='Adam') # These are the default values
    # Smaller learning rate will cause better accuracy, but with higher computaional power (larger training time)
    
    model.compile(loss ='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])

    return model

and I saved the model using the code:

# Save Best Model
BestModel.save(f"{Model_Dir}/Model", save_format='tf')

It should be straight forward using the example that you provided but I keep getting errors. Perhaps I should treat this as a single input and output but the data type would be a tuple or list? Am I missing something?

Thanks

mohamedmkamra avatar Aug 22 '22 10:08 mohamedmkamra

Hi @mohamedmkamra

How are then you running the model with cppflow? What error you getting?

As far as I can see is a single-input, single-output model, so I don't think it's a multi-input problem.

serizba avatar Aug 22 '22 11:08 serizba

Actually the number of inputs is technically not 1. X_train is a numpy array with number of rows = ntrainData and number of columns = n_inputs Y_train is a numpy array with number of rows = ntrainData and number of columns = n_outputs This is why I am not sure about this.

mohamedmkamra avatar Aug 22 '22 12:08 mohamedmkamra

ntrainData is the number of dimensions or features of your data?

serizba avatar Aug 24 '22 14:08 serizba

No, ntrainData is the size of training data ( i.e. number of training samples)

mo7ammedmostafa avatar Aug 25 '22 05:08 mo7ammedmostafa

PyTorch models are prepared to take inputs of shape NxD (as yours). With N being number of samples, batch dimension, and D being the dimension of each data point.

Without seeing what are you trying is hard to make a guess.

serizba avatar Aug 25 '22 07:08 serizba

I am not using PyTorch, I am using tensor flow. I have used tensorflow::keras to train a neural network based on the model above.

def build_model(X_train, d , n ):

    n_inputs = X_train.shape[1]
    n_outputs = Y_train.shape[1]
    model = Sequential()
    model.add(Dense(128, input_dim=n_inputs, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    model.add(Dropout(d))
    # model.add(Dense(32, activation='elu', kernel_regularizer=regularizers.l2(l2=n)))
    # model.add(Dropout(0))
    model.add(Dense(n_outputs))
    optimizer  = tf.keras.optimizers.Adam( learning_rate=0.001  , beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False, name='Adam') # These are the default values
    # Smaller learning rate will cause better accuracy, but with higher computational power (larger training time)
    
    model.compile(loss ='mse',
                optimizer=optimizer,
                metrics=['mae', 'mse'])

    return model

The NN model is simple: it has 5 inputs, and the size of training data is roughly 100K samples with every sample consisting of 5 values as inputs and 2 values as outputs. NN The number of hidden layers and number of neurons per layer shown here is just for illustration.

mohamedmkamra avatar Aug 25 '22 08:08 mohamedmkamra

Yeah, I meant TensorFlow sorry.

But I don't see how you trying to use cppflow, you only posted Python code. If it's a TensorFlow related issue, it shouldn't be posted here.

serizba avatar Aug 25 '22 09:08 serizba

The tf model itself is good. It works fine for evaluation when the model files are read in Python.

But when I try to read the model files using cppflow, I get segmentation fault errors: Basically, I tried using the load_model example

#include

#include "cppflow/ops.h" #include "cppflow/model.h"

int main() {

auto input = cppflow::fill({10, 5}, 1.0f);
cppflow::model model(std::string(MODEL_PATH));
auto output = model(input);

std::cout << output << std::endl;

auto values = output.get_data<float>();

for (auto v : values) {
    std::cout << v << std::endl;
}
return 0;

}

mo7ammedmostafa avatar Aug 27 '22 06:08 mo7ammedmostafa

Try following this answer on StackOverflow

yochananscharf avatar Sep 11 '22 08:09 yochananscharf