gpt4 book ai didi

machine-learning - 如何正确输入形状或输入尺寸?

转载 作者:行者123 更新时间:2023-11-30 09:09:25 25 4
gpt4 key购买 nike

我是 Keras 新手,一直在努力正确调整数据。我已经尝试了几个星期了,这是我最接近的一次。我很确定我只是强制事情正常工作并且必须调整数据的形状。有几个问题:

  1. 模型、损失、优化或激活函数是否确定 input_shape 或 input_dim 需要的形状/维度?

如果不是,我该如何将数据塑造成正确的形式。

我尝试将数据整形为 (1, 1, 59),但随后我会收到提示,目标数据的形状为 (1, 1, 19)。目前,我知道如何实现这一点的唯一方法是将数据切成两半,使其成为均匀的形状,但我只想使用 20% 的数据来创建一个新集。

我的代码:我想做的是让模型学习 1 - 100 之间的序列。然后给定一个数字,它应该预测下一个数字应该是什么。

# Tool setup
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout

# Setup our dataset and testset.
dataset = [] # Training set.
validset = []
testset = []

dataset = list(range(60))
validset = list(range(60, 80))
testset = list(range(80, 100))

# Preprocess Data:
X_train = dataset[:-1] # Drop the last element.
Y_train = dataset[1:] # The second element is the target for prediction.

# Reshape training data for Keras LSTM model
# The training data needs to be (batchIndex, timeStepIndex, dimensionIndex)
# Single batch, time steps, dimensions
#print(np.array(X_train).shape)
X_train = np.array(X_train).reshape(-1, 59, 1)
Y_train = np.array(Y_train).reshape(-1, 59, 1)

# Normalize the Data:
#X_train = np.divide(X_train, 200)
#Y_train = np.divide(Y_train, 200)

X_test = validset[:-1] # Drop the last element.
Y_test = validset[1:] # The second element is the target for prediction.
#print(np.array(X_test).shape)
X_test = np.array(X_test).reshape(-1, 19, 1)
Y_test = np.array(Y_test).reshape(-1, 19, 1)

# Build Model
model = Sequential()
#model.add(LSTM(100, input_dim=1, return_sequences=True,
activation='softmax'))
model.add(LSTM(100, input_dim=1, return_sequences=True))
model.add(Dense(1))
model.compile(loss='mse', optimizer='rmsprop', metrics=["accuracy"])
#model.add(Dropout(0.80))

# Train the Model
history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test),
nb_epoch=10, batch_size=1, verbose=1)

# The validation set is checked during training to monitor progress, and
possibly for early stopping,
# but is never used for gradient descent.

# validation_data is used as held-out validation data. Will override
validation_split.
# validation_data=(X_test, Y_test)

# validation_split is the Fraction of the data to use as held-out validation
data.
# validation_split=0.083

from IPython.display import SVG
from keras.utils.visualize_util import model_to_dot

SVG(model_to_dot(model).create(prog='dot', format='svg'))

# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper left')
plt.show()

# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validate'], loc='upper left')
plt.show()

# Test the Model
#print(np.array(testset).shape)
testset = np.array(testset).reshape(-1, 5, 1)
predict = model.predict(testset)
# Undo the normalization step.
#predict = np.multiply(data, 200)
predict = predict.reshape(-1)
print(predict[0])

最佳答案

Does the model, loss, optimization, or activation function determine what shape/dimension the input_shape or input_dim needs to be?

我倾向于答案是"is"。某些功能需要不同的尺寸大小。

现在让我们保持简单,只关注问题的本质。

dataset = list(range(100))
validset = dataset[-20:]
testset = dataset[-20:]

Keras 似乎希望 LSTM 的数据如下所示:批处理索引、时间步长索引、维度索引

print(np.array(X_train).shape)
X_train = np.array(X_train).reshape(99, 1, 1)
Y_train = np.array(Y_train).reshape(99, 1, 1)
print(np.array(X_train).shape)

结果:(99,)(99, 1, 1)

模型简化为:

model = Sequential()  
model.add(LSTM(100, input_dim=1, return_sequences=True))
model.add(Dense(1))
model.compile(loss='mse', optimizer='rmsprop', metrics=["accuracy"])

从图表和错误的预测来看,仍有很多工作要做。至少这会让事情开始。

关于machine-learning - 如何正确输入形状或输入尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43854443/

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