ikenas
ikenas

Reputation: 431

Tensorflow, what is the best way to add extra inforamtion to a sequential model?

I'm new to machine learning and I'm tring to understand how to make models.

What I'm tring to do.

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

# this are my X and Y values
X = np.array([ [x, x+1 ] for x in range(80) ])
Y = [ x + 2 for x in range(80) ]

# The data given in this case is random but has a pridictable pattern.
# if i would use a list of [81,82,1,2] 
# to predict once i fit the model it will give 83.

# I have this extra information that could be helpful for the prediction.
extra_info = [ 1, 2 ]

# What is the best way to add this extra information?

# This is the only way i konw how to add it:
X = np.array([ [x, x+1, extra_info[0], extra_info[1] ] for x in range(80) ])

Is this the correct way of doing this?

Is not going to confuse the model?

And if the extra_info had more than 1000 fields whould this also be correct?

model = keras.Sequential([
  layers.Dense( 64, activation='relu', input_shape =[ 4 ] ),
  layers.Dense( 64, activation='relu' ),
  layers.Dense( 1 )
])

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

history = model.fit(
  X, Y, epochs=20, verbose=0,
)

print(model.predict(np.array([80,81,1,2])))

Upvotes: 0

Views: 103

Answers (2)

Syrius
Syrius

Reputation: 941

If you have additional information that you want to feed to the model, you should look at the functional API of Keras. It let's you defined additional inputs and than merge different paths. This allows to e.g. create one input path per feature.

If you need to stick to the sequential API it's probably the best and only way how you handle it at the moment.

Upvotes: 1

Canbach
Canbach

Reputation: 106

The problem you will face when doing it like this is 1.The data is stored in a numpy array, not in a tensor, which is the input the model is expecting. To convert them you just have to use the tensorflow method convert_to_tensor (check this documentation out for it: https://www.tensorflow.org/api_docs/python/tf/convert_to_tensor). This would look something like this:

X = tf.convert_to_tensor(X)
Y = tf.convert_to_tensor(Y)

2.There is not really a pattern that the model can predict here, since you're just feeding it random numbers, so it will seem that it isnt learning anything. 3.When predicting you are giving the whole training dataset, which may make things a bit confusing. 2&3 arent really any issues, just things to keep in mind. Other than that you're doing great. Hope that helped!

Upvotes: 0

Related Questions