keras:model.predict返回形状错误的数组

问题描述 投票:0回答:1

我对model.predict的输出形状有疑问。

让我们考虑以下简单的虚拟模型:

import numpy as np
import pandas as pd
import tensorflow as tf
import string
from tensorflow.keras import layers

###Creating pseudo data
a = pd.DataFrame(np.random.randn(1000, 10), columns = [i for i in string.ascii_lowercase[:10]])

### Splitting data between train and test set
train = a.sample(frac=0.75, random_state=0)
test = a.drop(train.index)

### Creating features columns
f = []
for c in a.columns:
    if c == 'j':
        continue
    x = tf.feature_column.numeric_column(c)
    f.append(x)

l = layers.DenseFeatures(f)

### Creating model
model = tf.keras.models.Sequential()
model.add(l)  
model.add(tf.keras.layers.Dense(
    units = 64,
    activation = 'relu',
    name = 'hidden1'
    )
)
model.add(tf.keras.layers.Dense(
    units = 128,
    activation = 'relu',
    name = 'hidden2'
    )
)


### Prepping training data for input in the model
features = {name : np.array(value) for name, value in train.items()}
label = np.array(features.pop('j'))

lr = 0.001
batch_size = 40
epochs = 40

model.compile(optimizer = tf.keras.optimizers.RMSprop(lr = lr),
    loss = 'mean_squared_error',
    metrics = [tf.keras.metrics.MeanSquaredError()])

model.fit(x=features, y=label, batch_size = batch_size, epochs = epochs, shuffle = True)

features_predict = {name : np.array(value) for name, value in test.items()}
label_predict = np.array(features_predict.pop('j'))

p = model.predict(x=features_predict)

现在是否检查p:

p.shape

它返回(250, 128)(显然,我希望可以预测j的值并且形状为(250,1)

我在做什么错?

tensorflow keras
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.