首页 > 解决方案 > 序贯模型预测

问题描述

我从文件中获取数据:

y = np.genfromtxt('dataY.txt', dtype=np.float32) #input data for target dataset
x = np.genfromtxt('dataX.txt', dtype=np.float32)#input data for input dataset

我相应地拆分数据

xtrain, xtest, ytrain, ytest = train_test_split (x,y,test_size=.2)
train_data = tf.data.Dataset.from_tensor_slices((xtrain, ytrain))
valid_data = tf.data.Dataset.from_tensor_slices((xtest, ytest))

dataY.txt文件由 65 行组成。在给定输入 X 的情况下,每行包含 100 个我希望 NN 在训练后猜测的数字。

dataX.txt文件由 65 行组成,每个 Y 行。每行包含 100 个数字。因此,给定一百个数字,我想猜测 100 个新数字。

我定义和训练 NN:

N = 100
M = 100

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(N,1)))
model.add(tf.keras.layers.Dense(N, activation='relu'))
model.add(tf.keras.layers.Dense(N, activation='relu'))
model.add(tf.keras.layers.Dense(N, activation='relu'))
model.add(tf.keras.layers.Dense(M))

model.compile( loss='mse', metrics=['mse'])
model.fit(train_data, epochs=4, validation_data=valid_data)

问题1:训练前

WARNING:tensorflow:Model was constructed with shape (None, 100, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 100, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (100, 1, 1).
WARNING:tensorflow:Model was constructed with shape (None, 100, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 100, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (100, 1, 1).
2021-07-12 17:46:16.136364: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)

训练完成后:


WARNING:tensorflow:Model was constructed with shape (None, 100, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 100, 1), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (None, 1, 1).
(100, 1, 100)

这是致命的吗?我该如何解决?

问题2:

X_final_test = np.genfromtxt('testX.txt', dtype=np.float32)# test input

从具有单行和 100 个数字的文件中获取数据。它的形状是(100,)BUT

pred = model.predict (X_final_test)

有一个(100,1,100)形状,所以我不知道用我已知的结果来对抗它。问题是什么?我如何解决它?

标签: pythonnumpytensorflowmachine-learningkeras

解决方案


更改您的模型定义以反映您有 100 个输入:

N_in = 100
N_hid = 100 #good practice to separate these
N_out = 100

model = tf.keras.models.Sequential()
model.add(tf.keras.Input(shape=(None, N_in)))
model.add(tf.keras.layers.Dense(N_hid, activation='relu'))
model.add(tf.keras.layers.Dense(N_hid, activation='relu'))
model.add(tf.keras.layers.Dense(N_hid, activation='relu'))
model.add(tf.keras.layers.Dense(N_out))

model.compile( loss='mse', metrics=['mse'])
model.fit(train_data, epochs=4, validation_data=valid_data)

你现在正在做的是你指定它有 1 个输入并且它正在扩展到那个。为确保您的数据形状正确,请确保其大小为 [B, 100],其中 B 是您处理的批量大小(调试时可以为 1)。


推荐阅读