Creating multiple output models for regression analysis [Beginner]

What I want to do with machine learning is creating AI that gives multiple answers. I managed to move it like that, so I posted it as a memorandum.

Trial environment

software version
Google Colaboratory 2020-10-01

Implementation code

model

def build_model():
  inputs = layers.Input(shape=(len(train_dataset.keys()),))
  x = layers.Dense(64, activation='relu')(inputs)

  a = layers.Dense(64, activation='relu')(x)
  output_a = layers.Dense(1, name='bmi')(a)

  b = layers.Dense(64, activation='relu')(x)
  output_b = layers.Dense(1, name='life_expectancy')(b)

  model = keras.Model(inputs=inputs, outputs=[output_a, output_b], name='Health')

  model.compile(
      optimizer=tf.keras.optimizers.RMSprop(0.001),
      loss_weights=[1., 0.8],
      loss=['mse', 'mse'],
      metrics=['mae', 'mse'])

  return model

Suddenly, the code I actually tried is above It seems that keras.Sequential, which is often used in sample code, has only one output. It seems that if you want to ask for multiple things at once, you have to define Model with functional API For example, predicting the number of "retweets" and "likes" from the content of the tweet.

It seems that you can increase the output as much as you want by storing the output result of the layer to be added in a variable and passing it as an array to the argument ʻoutputs of Keras.Model`.

Also, by specifying the loss function in the array for loss, it seems that the evaluation method can be changed for each output.

Teacher data

height weight bmi sex birthday life_expectancy
161 121 47 female 1982-02-01 40
154 55 23 female 2005-08-09 72
172 64 22 male 1976-09-21 37

I made fictitious data that associates height, weight with BMI, and life expectancy. Create a model that predicts BMI and life expectancy from height, weight, gender, and year of birth

Training

model.fit(train_data, [train_label_a, train_label_b], epochs=100)

When training, pass the data and teacher data to fit as an array If you pass it in dictionary format, it seems that you can not learn because the error feedback does not work well A common image analysis system handles large image data well with tf.data.Dataset, but this also seems to be a dictionary type, so learning with multiple output models failed. Moreover, when passing with tf.data.Dataset, it is passed with the data and label set, so it is even more chimpunkampun orz

Postscript: When passing data with tf.data.Dataset, I was able to give feedback by matching the dictionary key and the model output name.

ds = tf.data.Dataset.from_tensor_slices((normed_train_data, {"bmi": train_labels.bmi, "life_expectancy": train_labels.life_expectancy}))
ds = ds.batch(BATCH_SIZE)

model.fit(ds, epochs=100)

bonus

It is the whole code that I checked while writing and moving Published on Colaboratory

Regression problem-Predicting BMI and life expectancy

at the end

I tried to write about machine learning, but I'm in the middle of Gori without knowing it at all. Therefore, please forgive me though the ending of the article is just "~ seems".

The accuracy of machine learning itself is beyond imagination, and if there is no bias in the learning data, it seems that even a small amount of data will give considerable prediction accuracy. Exceptional things and patterns that are not blessed with learning data will give strange numerical values, so how much can we pack these things ... I wonder if it will be the difference with Bonjin. I felt that

reference

Getting started with Keras with functional API

Recommended Posts

Creating multiple output models for regression analysis [Beginner]
Multiple regression analysis with Keras
Machine learning algorithm (multiple regression analysis)
Data analysis for improving POG 3-Regression analysis-
Output beginner
[scikit-learn, matplotlib] Multiple regression analysis and 3D drawing
Poisson regression analysis
Regression analysis method
2. Multivariate analysis spelled out in Python 2-1. Multiple regression analysis (scikit-learn)