在Keras中如何预测一个值列表?

huangapple go评论80阅读模式
英文:

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:

&gt; ValueError: Input 0 of layer &quot;sequential_51&quot; 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:

&gt;  Input shape:  (2, 1023, 256) Label shape:  (2, 10) 1/1
&gt; [==============================] - 0s 83ms/step Prediction shape:  
&gt; (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(&quot;Input shape: &quot;, inputs.shape)
print(&quot;Label shape: &quot;,labels.shape)




model = Sequential()


model.add(Dense(256, input_shape=(1023, 256), activation=&#39;relu&#39;))


model.add(Dense(128, activation=&#39;relu&#39;))

model.add(Flatten())
model.add(Dense(10, activation=&#39;linear&#39;))

model.compile(optimizer=&#39;adam&#39;, loss=&#39;mean_squared_error&#39;)

model.summary()
model.fit(inputs, labels, epochs=10, batch_size=1, verbose=1)


predictions = model.predict(test_data, batch_size=1)

print(predictions.shape)

huangapple
  • 本文由 发表于 2023年6月15日 16:22:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定