gpt4 book ai didi

python - 值错误 : Data cardinality is ambiguous

转载 作者:行者123 更新时间:2023-12-04 04:29:40 33 4
gpt4 key购买 nike

我正在尝试根据从 DataFrame 获取的数据训练 LSTM 网络。

这是代码:

x_lstm=x.to_numpy().reshape(1,x.shape[0],x.shape[1])

model = keras.models.Sequential([
keras.layers.LSTM(x.shape[1], return_sequences=True, input_shape=(x_lstm.shape[1],x_lstm.shape[2])),
keras.layers.LSTM(NORMAL_LAYER_SIZE, return_sequences=True),
keras.layers.LSTM(NORMAL_LAYER_SIZE),
keras.layers.Dense(y.shape[1])
])

optimizer=keras.optimizers.Adadelta()

model.compile(loss="mse", optimizer=optimizer)
for i in range(150):
history = model.fit(x_lstm, y)
save_model(model,'tmp.rnn')

这失败了
ValueError: Data cardinality is ambiguous:
x sizes: 1
y sizes: 99
Please provide data which shares the same first dimension.

当我将模型更改为
model = keras.models.Sequential([
keras.layers.LSTM(x.shape[1], return_sequences=True, input_shape=x_lstm.shape),
keras.layers.LSTM(NORMAL_LAYER_SIZE, return_sequences=True),
keras.layers.LSTM(NORMAL_LAYER_SIZE),
keras.layers.Dense(y.shape[1])
])

它失败并出现以下错误:
Input 0 of layer lstm_9 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [None, 1, 99, 1200]

我如何让这个工作?

x 的形状为 (99, 1200) (99 个项目,每个项目有 1200 个特征,这只是一个更大的数据集的样本),y 的形状是 (99, 1)

最佳答案

Error建议,First DimensionXy是不同的。 First Dimension表示 Batch Size它应该是一样的。

请确保Y还有shape , (1, something) .

我可以使用下面显示的代码重现您的错误:

from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
import tensorflow as tf
import numpy as np


# define sequences
sequences = [
[1, 2, 3, 4],
[1, 2, 3],
[1]
]

# pad sequence
padded = pad_sequences(sequences)
X = np.expand_dims(padded, axis = 0)
print(X.shape) # (1, 3, 4)

y = np.array([1,0,1])
#y = y.reshape(1,-1)
print(y.shape) # (3,)

model = Sequential()
model.add(LSTM(4, return_sequences=False, input_shape=(None, X.shape[2])))
model.add(Dense(1, activation='sigmoid'))

model.compile (
loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(0.001))

model.fit(x = X, y = y)

如果我们观察 Print声明,
Shape of X is  (1, 3, 4)
Shape of y is (3,)

可以通过取消注释该行来修复此错误, y = y.reshape(1,-1) ,这使得 First Dimension ( Batch_Size ) 等于 ( 1 ) Xy .

现在,工作代码和输出如下所示:
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
import tensorflow as tf
import numpy as np


# define sequences
sequences = [
[1, 2, 3, 4],
[1, 2, 3],
[1]
]

# pad sequence
padded = pad_sequences(sequences)
X = np.expand_dims(padded, axis = 0)
print('Shape of X is ', X.shape) # (1, 3, 4)

y = np.array([1,0,1])
y = y.reshape(1,-1)
print('Shape of y is', y.shape) # (1, 3)

model = Sequential()
model.add(LSTM(4, return_sequences=False, input_shape=(None, X.shape[2])))
model.add(Dense(1, activation='sigmoid'))

model.compile (
loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(0.001))

model.fit(x = X, y = y)

上面代码的输出是:
Shape of X is  (1, 3, 4)
Shape of y is (1, 3)
1/1 [==============================] - 0s 1ms/step - loss: 0.2588
<tensorflow.python.keras.callbacks.History at 0x7f5b0d78f4a8>

希望这可以帮助。快乐学习!

关于python - 值错误 : Data cardinality is ambiguous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62253289/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com