验证数据的输入形状无效

问题描述 投票:0回答:1

我正在使用 Tensorflow 在 Python 中开发一个简单的 ML 模型。代码如下:

import tensorflow as tf
import pandas as pd

# Load CSV Data
def load_data(filename):
    data = pd.read_csv(filename)
    X = data[['X0','X1','X2','X3']]
    Y = data[['Y0','Y1']]
    return tf.data.Dataset.from_tensor_slices((X.values, Y.values))

training_data = load_data("binarydatatraining.csv")
print(training_data)

# Build a simple neural network model
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(4, activation='relu'),
  tf.keras.layers.Dense(2)
])
# Compile the model
model.compile(optimizer='adam',
              loss='mean_squared_error')

# Load validation data
validation_data = load_data("binarydatavalidation.csv")
print(validation_data)

# Train the model
model.summary()
model.fit(training_data.batch(9), epochs=5)
model.summary()
model.fit(training_data.batch(9), epochs=1, validation_data = validation_data, validation_steps = 2)

一切都运行得很完美,直到我开始包含验证数据,该数据具有与训练数据相同数量的参数。然后我收到错误

ValueError: Exception encountered when calling Sequential.call().

[1mInvalid input shape for input Tensor("sequential_1/Cast:0", shape=(4,), dtype=float32). Expected shape (None, 4), but input has incompatible shape (4,)[0m

Arguments received by Sequential.call():
  • inputs=tf.Tensor(shape=(4,), dtype=int64)
  • training=False
  • mask=None

打印验证数据集和训练数据集显示它们具有相同的维度,并且运行

print(training_data)
print(validation_data)
都给出

<_TensorSliceDataset element_spec=(TensorSpec(shape=(4,), dtype=tf.int64, name=None), TensorSpec(shape=(2,), dtype=tf.int64, name=None))>

如何正确设置验证数据以与

model.fit
内联运行?

python tensorflow machine-learning
1个回答
0
投票

只需将输入层添加到具有相同形状的模型中(4,)。然后它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.