Python Tensorflow,向序列模型添加额外信息的最佳方法是什么?

Python Tensorflow,向序列模型添加额外信息的最佳方法是什么?,python,numpy,tensorflow,keras,Python,Numpy,Tensorflow,Keras,我是机器学习的新手,我想了解如何制作模型 我想做的事 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) ] # Th

我是机器学习的新手,我想了解如何制作模型

我想做的事

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) ])
这是正确的做法吗

这不会混淆模型吗

如果
extra\u info
中有1000多个字段,那么这也是正确的吗

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])))

这样做时,您将面临的问题是1。数据存储在numpy数组中,而不是存储在张量中,张量是模型所期望的输入。要转换它们,您只需使用tensorflow方法convert_To_tensor(查看以下文档:)。这看起来像这样:

X = tf.convert_to_tensor(X)
Y = tf.convert_to_tensor(Y)
2.这里没有模型可以预测的模式,因为你只是给它输入随机数,所以它似乎没有学到任何东西。3.当预测时,你给出的是整个训练数据集,这可能会让事情有点混乱。2和3其实不是什么问题,只是需要记住的事情。除此之外,你做得很好。
希望有帮助

如果您有其他信息要提供给模型,您应该查看。它允许您定义其他输入,然后合并不同的路径。这允许例如为每个功能创建一个输入路径


如果您需要坚持使用顺序API,这可能是目前处理它的最佳和唯一方法。

第1点是错误的。您不需要将输入转换为张量(参见)。第2点也不是真的,因为输入中显然有一个模式…是的,但它仍然会给出一个随机输出。