ValueError: Data cardinality is ambiguous
ValueError: Data cardinality is ambiguous
我正在尝试使用从 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
所暗示的那样,X
和y
的First Dimension
是不同的。 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
) 对于 X
和 y
.
现在,工作代码如下所示,连同输出:
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>
希望这对您有所帮助。快乐学习!
我正在尝试使用从 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
所暗示的那样,X
和y
的First Dimension
是不同的。 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
) 对于 X
和 y
.
现在,工作代码如下所示,连同输出:
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>
希望这对您有所帮助。快乐学习!