python - 序贯模型预测
问题描述
我从文件中获取数据:
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)
形状,所以我不知道用我已知的结果来对抗它。问题是什么?我如何解决它?
解决方案
更改您的模型定义以反映您有 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)。
推荐阅读
- c# - Bot Framework V3 电子邮件 Json 对象
- python - python响应 - 并非所有请求都已执行
- php - 禁用自动购物车/checkout_update 添加手动选项以在下订单前进行更新
- python - 如何找到最新的照片并打开它 - Photo Booth
- unity3d - 如何在 github 上备份一个统一项目
- python - datetime.now() 和 db 中的 datetimefield 之间的比较和区别
- jquery - 如何增加使用 jQuery 创建的最后一个跨度的值?
- ruby - 将 Rspec 升级到 Rspec 2.99,希望在前/后挂钩中使用 let 连接变量
- google-apps-script - 将行从一个电子表格复制到另一个电子表格时如何阻止添加额外的行
- javascript - 如何动态向图表添加数据