首页 > 解决方案 > 如何在 Keras API 中将数组列表作为输入

问题描述

好吧,我是机器学习的新手,对 Keras 也是如此。我正在尝试创建一个模型,可以将其作为输入传递给数组的数组列表(2 个数组中的 6400 个数组的列表)。这是我的代码的问题:

XFIT = np.array([x_train, XX_train])
YFIT = np.array([y_train, yy_train])
Inputs = keras.layers.Input(shape=(6400, 2))
hidden1 = keras.layers.Dense(units=100, activation="sigmoid")(Inputs)
hidden2 = keras.layers.Dense(units=100, activation='relu')(hidden1)
predictions = keras.layers.Dense(units=3, activation='softmax')(hidden2)

model = keras.Model(inputs=Inputs, outputs=predictions)

没有错误;但是,输入层 (Inputs) 迫使我传递 (6400, 2) 形状,因为每个数组(x_train 和 XX_train)内部都有 6400 个数组。结果,随着时代的发展,是这样的:

Train on 2 samples
Epoch 1/5

2/2 [==============================] - 1s 353ms/sample - loss: 1.1966 - accuracy: 0.2488
Epoch 2/5

2/2 [==============================] - 0s 9ms/sample - loss: 1.1303 - accuracy: 0.2544
Epoch 3/5

2/2 [==============================] - 0s 9ms/sample - loss: 1.0982 - accuracy: 0.3745
Epoch 4/5

2/2 [==============================] - 0s 9ms/sample - loss: 1.0854 - accuracy: 0.3745
Epoch 5/5

2/2 [==============================] - 0s 9ms/sample - loss: 1.0835 - accuracy: 0.3745

Process finished with exit code 0

由于输入形状,我在每个时期不能训练超过两次。如何更改此输入?我尝试过其他形状,但它们让我出错。

x_train, XX_train 看起来像这样

[[[0.505834 0.795461]
  [0.843175 0.975741]
  [0.22349  0.035036]
  ...
  [0.884796 0.867509]
  [0.396942 0.659936]
  [0.873194 0.05454 ]]

 [[0.95968  0.281957]
  [0.137547 0.390005]
  [0.635382 0.901555]
  ...
  [0.887062 0.486206]
  [0.49827  0.949123]
  [0.034411 0.983711]]]

谢谢你,如果我犯了任何错误,请原谅我,第一次在 Keras 和第一次在 StackOverFlow :D

标签: pythontensorflowmachine-learningkerasdeep-learning

解决方案


你快到了。问题在于:

XFIT = np.array([x_train, XX_train])
YFIT = np.array([y_train, yy_train])

让我们看一个例子:

import numpy as np

x_train = np.random.random((6400, 2))
y_train = np.random.randint(2, size=(6400,1))

xx_train = np.array([x_train, x_train])
yy_train = np.array([y_train, y_train])

print(xx_train.shape)
(2, 6400, 2)

print(yy_train.shape)
(2, 6400, 1)

在数组中,我们有 2 个批次,每个批次有 6400 个样本。这意味着当我们调用 时model.fit,它只有 2 个批次可以训练。相反,我们可以做的:

xx_train = np.vstack([x_train, x_train])
yy_train = np.vstack([y_train, y_train])

print(xx_train.shape)
(12800, 2)

print(yy_train.shape)
(12800, 1)

现在,我们已经正确地加入了两个样本,现在可以训练了。

Inputs = Input(shape=(2, ))
hidden1 = Dense(units=100, activation="sigmoid")(Inputs)
hidden2 = Dense(units=100, activation='relu')(hidden1)
predictions = Dense(units=1, activation='sigmoid')(hidden2)

model = Model([Inputs], outputs=predictions)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(xx_train, yy_train, batch_size=10, epochs=5)

Train on 12800 samples
Epoch 1/5
12800/12800 [==============================] - 3s 216us/sample - loss: 0.6978 - acc: 0.5047
Epoch 2/5
12800/12800 [==============================] - 2s 186us/sample - loss: 0.6952 - acc: 0.5018
Epoch 3/5
12800/12800 [==============================] - 3s 196us/sample - loss: 0.6942 - acc: 0.4962
Epoch 4/5
12800/12800 [==============================] - 3s 217us/sample - loss: 0.6938 - acc: 0.4898
Epoch 5/5
12800/12800 [==============================] - 3s 217us/sample - loss: 0.6933 - acc: 0.5002

推荐阅读