'我想通过keras.model解决分类问题,但是在运行model.fit之后,我遇到了尺寸错误。我已经运行以下代码:'
class Program
{
static void Main(string[] args)
{
var input = @"[1 (832, 315, title1)]
[2 (232, 115, title2)]
[3 (432, 215, title3)]
[4 (132, 315, title4)]
[5 (932, 415, title5)]
";
TextToDictionary(input.AsSpan());
}
private static Dictionary<int, Tuple<double, double, string>> TextToDictionary(ReadOnlySpan<char> span)
{
var output = new Dictionary<int, Tuple<double, double, string>>();
while (span.Length > 0)
{
span = EatUntilToken(span, "[");
span = ConsumeToAndEatToken(span, "(", out var int1);
span = ConsumeToAndEatToken(span, ",", out var double1);
span = ConsumeToAndEatToken(span, ",", out var double2);
span = ConsumeToAndEatToken(span, ")", out var string1);
span = EatUntilToken(span, Environment.NewLine);
output.Add(int.Parse(int1), Tuple.Create(double.Parse(double1), double.Parse(double2), string1.ToString()));
}
return output;
}
private static ReadOnlySpan<char> ConsumeToAndEatToken(ReadOnlySpan<char> instruction, string token, out ReadOnlySpan<char> value)
{
var indexOf = instruction.IndexOf(token);
if (indexOf < 0)
throw new Exception($"String {instruction.ToString()} does not contain the expected token {token}");
value = instruction[..(indexOf)];
return instruction[(indexOf + token.Length)..];
}
private static ReadOnlySpan<char> EatUntilToken(ReadOnlySpan<char> instruction, string token)
{
var indexOf = instruction.IndexOf(token);
if (indexOf < 0)
throw new Exception($"String {instruction.ToString()} does not contain the expected token {token}");
return instruction[(indexOf + token.Length)..];
}
}
'输出:'
print(X_train.shape)
print(y_train.shape)
'运行以下代码后,'
(2588, 39436)
(2588, 6)
model = keras.Sequential(
[
keras.Input(shape=(39436,1)),
layers.Conv1D(32, kernel_size=3, strides=5, activation="relu"),
layers.MaxPooling1D(pool_size=10),
layers.Conv1D(64, kernel_size=3, strides=5, activation="relu"),
layers.MaxPooling1D(pool_size=10),
layers.Flatten(),
layers.Dropout(0.5),
layers.Dense(num_classes, activation="softmax"),
]
)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
“我给这个错误:”
ValueError:用户代码中:
model.fit(X_train, y_train, batch_size=128, epochs=15, validation_split=0.3)
“如果您能指导我,那将会是什么问题?”
答案 0 :(得分:0)
根据错误消息,您的输入数组的形状为[None, 39436]
。但是,在Input
层中,您传递的形状为[39436, 1]
,该形状与[None, 39436, 1]
相匹配,其中None
代表样本维。这就是抛出的错误。
您需要通过以下方式匹配形状:
1。重塑输入数据的形状,使其形状为[samples, 39436, 1]
,而模型结构保持不变。
这可以通过以下操作来完成(假设train_X是您的输入功能):
train_X = np.expand_dims(train_X, axis=2)
np.expand_dims
在数组形状的索引2处向数组添加新维度。因此,这里将[samples, 39436]
重塑为[samples, 39436, 1]
。
OR
2。在input_shape
层中更改 Input
参数,以接受[39436,]
的形状,以匹配您的数据。