如何修复此错误:数据基数不明确:x 大小:17260 y 大小:863 确保所有数组包含相同数量的样本

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

我正在尝试编写一些代码,它会在 868 行中找到一个模式,每行有 20 个数字,并且会根据它们的模式打印 10 个最可能的序列,但我无法克服这个错误。这是代码:

`import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, LSTM

# Load the dataset
data = np.genfromtxt('data.csv', delimiter=',')

# Set the sliding window size
window_size = 5

# Initialize the input and output arrays
X_train = []
y_train = []

# Loop through the data and generate input-output pairs
for i in range(window_size, len(data)):
    X_train.append(data[i-window_size:i])
    y_train.append(data[i])

# Convert the input and output arrays to NumPy arrays
X_train = np.array(X_train)
y_train = np.array(y_train)

# Reshape X_train to add an extra dimension
X_train = X_train.reshape(-1, window_size, 1)

# Define the LSTM model
model = Sequential()
model.add(LSTM(32, return_sequences=True, input_shape=(window_size, 1)))
model.add(Dense(40))
model.add(Dense(1))

# Compile the model
model.compile(loss='mse', optimizer='adam')

# Print the model summary
model.summary()

# Train the model
model.fit(X_train, y_train, epochs=100, verbose=1)

# Generate the most probable sequences
num_sequences = 10
sequence_length = 20

# Initialize the input array with random data
X_pred = np.random.rand(num_sequences, window_size, 1)

# Generate the sequences
for i in range(sequence_length - window_size):
    y_pred = model.predict(X_pred)
    X_pred[:, :-1, :] = X_pred[:, 1:, :]
    X_pred[:, -1, :] = y_pred[:, -1, :]

# Rank the sequences based on their probability
y_pred = model.predict(X_pred)
probabilities = np.sum((y_pred - X_pred[:, -1, :])**2, axis=2)
ranked_indices = np.argsort(probabilities)[:, 0][:num_sequences]

# Print the most probable sequences
for i in ranked_indices:
     print("Sequence {}: {}".format(i+1, X_pred[i]))`

我尝试更改 x 和/或 y,但我仍然遇到类似的错误。这是错误:

模型:“顺序”

层(类型)输出形状参数#

lstm (LSTM)(无、5、32)4352

dense(密集)(无、5、40)1320

dense_1(密集)(无、5、1)41

================================================= ================ 总参数:5713 (22.32 KB) 可训练参数:5713 (22.32 KB) 不可训练的参数:0(0.00 字节)


Traceback(最后一次调用):文件 "c:\Users\H\Desktop\Inv\Python p3.py”,第 41 行,在 model.fit(X_train, y_train, epochs=100, verbose=1) 文件 "C:\Users\H\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils raceback_utils.py", 第 70 行,在 error_handler raise e.with_traceback(filtered_tb) from 无文件 "C:\Users\H\AppData\Local\Programs\Python\Python310\lib\site-packages\keras ngine\data_adapter.py", 第 1943 行,在 _check_data_cardinality 中引发 ValueError(msg) ValueError:数据基数不明确:x 尺寸:17260 y 尺寸:863 确保所有阵列包含相同数量的样本。聚苯乙烯 C:\Users*\Desktop\Inv\Python>`

python arrays valueerror ambiguous
© www.soinside.com 2019 - 2024. All rights reserved.