英文:
How to predict a list of values in keras?
问题
我想使用keras训练一个神经网络。输入数据是多个形状为(1023, 256)的数组,标签是每个输入的10个值的列表。
我期望输出一个包含10个值的列表。
我简化了我的代码:
```python
input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1023, 256)
inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))
print(inputs.shape)
print(labels.shape)
model = Sequential()
model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)
predictions = model.predict(test_data)
预测结果应该类似于:
[ 8.06436593, 9.44189802, 10.44503002, 11.95312031, 14.43248845,
3.63374982, 4.72397662, 5.54989524, 15.2114616 , 7.70812219]
输出:
> ValueError: 第0层“sequential_51”的输入与该层不兼容:期望形状=(None, 1023, 256),找到形状=(None, 256)
更新:
input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1, 1023, 256)
inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))
print("输入形状:", inputs.shape)
print("标签形状:", labels.shape)
model = Sequential()
model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=0)
predictions = model.predict(test_data)
print(predictions.shape)
输出:
> 输入形状: (2, 1023, 256) 标签形状: (2, 10) 1/1
> [==============================] - 0s 83ms/step 预测形状:
> (1, 1023, 10)
<details>
<summary>英文:</summary>
I would like to train a neural network with keras. The input data are multiple arrays with shape (1023, 256) and the labels are a list of 10 values for each input.
I expect a list with 10 Values as output.
I simplified my code:
input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1023, 256)
inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))
print(inputs.shape)
print(labels.shape)
model = Sequential()
model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)
predictions = model.predict(test_data)
The prediction should look something like this:
[ 8.06436593, 9.44189802, 10.44503002, 11.95312031, 14.43248845,
3.63374982, 4.72397662, 5.54989524, 15.2114616 , 7.70812219]
Output:
> ValueError: Input 0 of layer "sequential_51" is incompatible with the layer: expected shape=(None, 1023, 256), found shape=(None, 256)
Update:
input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1, 1023, 256)
inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))
print("Input shape: ", inputs.shape)
print("Label shape: ",labels.shape)
model = Sequential()
model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=0)
predictions = model.predict(test_data)
print(predictions.shape)
Output:
> Input shape: (2, 1023, 256) Label shape: (2, 10) 1/1
> [==============================] - 0s 83ms/step Prediction shape:
> (1, 1023, 10)
</details>
# 答案1
**得分**: 0
你需要在你的测试数据中添加批次维度,因为Keras始终将第一个轴视为批次。所以测试数据的形状必须是 (1, 1023, 256) 而不是 (1023, 256)。
```python
test_data = np.expand_dims(test_data, axis=0)
predictions = model.predict(test_data)
我通过在最后一个(Dense 10)之前添加了Flatten来修改了你的模型层,以获得一维数据:
input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1, 1023, 256)
inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))
print("Input shape: ", inputs.shape)
print("Label shape: ", labels.shape)
model = Sequential()
model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Flatten())
model.add(Dense(10, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)
predictions = model.predict(test_data, batch_size=1)
print(predictions.shape)
请注意,上述代码中的双引号是正确的中文引号,但在程序中应使用英文引号。
英文:
You need to add the batch dimension in your test data, since Keras will consider always the first axis as the batches. So test data must have the shape (1, 1023, 256) instead of (1023, 256).
test_data = np.expand_dims(test_data, axis=0)
predictions = model.predict(test_data)
I edited you model layers by adding Flatten before the last one (Dense 10) to get 1-dimensional data:
input1 = np.random.rand(1023, 256)
input2 = np.random.rand(1023, 256)
label1 = np.random.rand(10)
label2 = np.random.rand(10)
test_data = np.random.rand(1, 1023, 256)
inputs = np.stack((input1, input2))
labels = np.stack((label1, label2))
print("Input shape: ", inputs.shape)
print("Label shape: ",labels.shape)
model = Sequential()
model.add(Dense(256, input_shape=(1023, 256), activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Flatten())
model.add(Dense(10, activation='linear'))
model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)
predictions = model.predict(test_data, batch_size=1)
print(predictions.shape)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论