类型错误:'Equal' Op 的输入'y'的类型 float32 与参数 'x' 的 int32 类型不匹配



我对Keras和LSTM很陌生。我一直在尝试训练我的序列模型,以使用以下代码预测股票的未来价格,但上面的错误不断弹出。

我尝试更改两种x_data的dtype,y_data.astype(np.float16(。但是,每次我都会返回 TypeError 说明我有一个 float32 类型。

如果有帮助,以下是我的数据的形状:

xtrain.shape : (32, 24,

67(, ytrain.shape : (32, 24, 1(, xtest.shape : (38, 67(, 形状 : (38, 1(

有没有人知道可能出了什么问题?我已经困在这个问题上一段时间了。如果有人能给我一个提示,那就太好了。

y_data = y_data.to_numpy().astype(np.float32)
x_data = main_df.to_numpy().astype(np.float32)
num_x_signals = x_data.shape[1]
num_y_signals = y_data.shape[1]
# SPLIT TRAIN TEST DATA
ratio = 0.85
train_ratio = int(ratio * len(x_data))
x_train = x_data[0:train_ratio]
x_test = x_data[train_ratio:]
y_train = y_data[0:train_ratio]
y_test = y_data[train_ratio:]
# GENERATE RANDOM SEQUENCES
batch_size = 32
sequence_length = 24
EPOCHS = 50
def batch_generator(x_train, y_train, batch_size, sequence_length, num_x_signals, num_y_signals, num_train):
    while True:
        x_shape = (batch_size, sequence_length, num_x_signals)
        x_batch = np.zeros(shape = x_shape).astype(np.float32)
        y_shape = (batch_size, sequence_length, num_y_signals)
        y_batch = np.zeros(shape = y_shape).astype(np.float32)
        for i in range(batch_size):
            idx = np.random.randint(num_train - sequence_length)
            x_batch[i] = x_train[idx:idx+sequence_length]
            y_batch[i] = y_train[idx:idx+sequence_length]
        yield (x_batch, y_batch)
generator = batch_generator(x_train, y_train, batch_size, sequence_length, num_x_signals, num_y_signals, train_ratio)
xtrain, ytrain = next(generator)
xtest, ytest = (np.expand_dims(x_test, axis=0),
                np.expand_dims(y_test, axis=0))
# LSTM MODEL
model = Sequential()
model.add(LSTM(32, input_shape = (None, num_x_signals,), return_sequences = True))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(LSTM(128, return_sequences = True))
model.add(Dropout(0.15))
model.add(BatchNormalization())
model.add(LSTM(128))
model.add(Dropout(0.18))
model.add(BatchNormalization())
model.add(Dense(32, activation = 'relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation = 'softmax'))
opt = tf.keras.optimizers.Adam(lr = 0.001, decay = 1e-6)
model.compile(
    loss = 'sparse_categorical_crossentropy',
    optimizer = opt,
    metrics = ['accuracy']
)
name_of_file = f"{to_predict}-{sequence_length}-{future_predict}-{int(time.time())}"
tensorboard = TensorBoard(log_dir = "logs/{}".format(name_of_file))
filepath = "LSTM_Final-{epoch:02d}-{val_acc:.3f}"
checkpoint = ModelCheckpoint("models/{}.model".format(filepath, monitor = 'val_acc', verbose = 1, save_best_only = True, mode = 'max')) # saves only the best ones
history = model.fit(
    xtrain, ytrain,
    epochs = EPOCHS,
    validation_data = (xtest, ytest),
    callbacks = [tensorboard, checkpoint]
)
score = model.evaluate(xtest, ytest, verbose = 0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save("models/{}".format(name_of_file))

我发现这个问题与指定的损失函数有关。

我的代码:

import tensorflow as tf
from tensorflow import keras
model = tf.keras.Sequential([
    keras.layers.Dense(64, activation=tf.nn.relu, input_shape=[3]),
    keras.layers.Dense(64, activation=tf.nn.relu),
    keras.layers.Dense(1)
])
#I changed the loss function from 'sparse_categorical_crossentropy' to 'mean_squared error'
model.compile(optimizer='adam',loss='mean_squared_error',metrics=['accuracy'])
X = train_dataset.to_numpy()
y = train_labels.to_numpy()
model.fit(X,y, epochs=5)

X 形状为 (920,3(,dtype = float64

Y 形状为 (920,1(,dtype = float64

我的问题出在model.fit方法上。我从一个图像识别示例中获取了"sparse_categorical_crossentropy"函数,我在这里尝试的是用于房价预测的神经网络。

最新更新