gpt4 book ai didi

python - 如何循环遍历Keras fit函数?

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

为了避免写 500 次:

model.fit(
x[i],
y[i],
batch_size=batch,
epochs=epochs,
validation_split=0.05)

我想循环遍历我正在拟合的数据集的数量。但是,当我这样做时,第一个拟合的指标如下:

loss: nan - acc: 7.4635e-04 - val_loss: nan - val_acc: 0.0000e+00

此后每次运行,其指标都是:

loss: nan - acc: 0.0000e+00 - val_loss: nan - val_acc: 0.0000e+00

现在我不认为 val_lossval_acc 为 0 和 nan 是好的,但至少有 acc 但在第一次拟合,所有指标基本上都是空的。

编辑:

这是我用于格式化数据的代码。 x[i] 是所有数据,其中 x[0] 是第一个整个数据集。

    x_train = [None] * len(os.listdir('/'))
y_train = [None] * len(os.listdir('/'))
x_test = [None] * len(os.listdir('/'))
y_test = [None] * len(os.listdir('/'))


def format_data(data, seq_len, normalise_window):
sequence_length = seq_len + 1
result = []
for index in range(len(data) - sequence_length):
result.append(data[index: index + sequence_length])

if normalise_window:
result = normalise_windows(result)

result = np.array(result)


row = round(0.85 * result.shape[0])
train = result[:int(row), :]
np.random.shuffle(train)
x_train = train[:, :-1]
y_train = train[:, -1]
x_test = result[int(row):, :-1]
y_test = result[int(row):, -1]

x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

return [x_train, y_train, x_test, y_test]

for i, filename in enumerate(os.listdir('/directory')):
try:
print"starting file :",filename
data = pd.read_csv('directory/{0}'.format(filename), index_col=0, header=0)
data['open'].replace('nan',np.nan)
data.dropna()
new = data['open'].tolist()


x_train[i], y_train[i], x_test[i], y_test[i] = format_data(new, seq_len, True)
except:
pass

for i in range(len(x_train)):
print"starting fit :",i,"of length",len(x_train)

try:
model.fit(
x_train[i],
y_train[i],
batch_size=batch,
epochs=epochs,
validation_split=0.05)
except:
pass

我怎样才能实现我想要做的事情?

最佳答案

尝试删除 for block 并尝试插入此代码

model.fit(
x_train,
y_train,
batch_size=batch,
epochs=epochs,
validation_split=0.05)

希望这对您有帮助。我猜损失和 val_loss 为“nan”,因为您将单个值传递给模型,尝试传递张量本身(希望您能在损失 n val_loss 中获得一些值)。

关于python - 如何循环遍历Keras fit函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50375207/

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