首页 > 解决方案 > TensorFlow 错误在单个数据集上产生结果

问题描述

我正在使用 tensorflow 尝试我的第一个 NN,但无法为单个输入样本生成结果。我创建了一个最小的示例,我向它提供了多个y = a * x + b输入(用于改变a, b)并尝试返回结果,但它失败了。请注意,我不关心这里的准确性,我是作为 POC 来做的。一些参数如下:

因此,我的训练数据是x_trainsize(m, 2*n)y_trainsize (m, 2)。看来我可以构建模型,但我无法为其提供单个大小输入(1, 2*n)并获得(1, 2)所需大小的结果。相反,我收到以下错误:

Traceback (most recent call last):
  File "xdriver.py", line 92, in <module>
    main()
  File "xdriver.py", line 89, in main
    ab2 = model.predict(rys) # This fails
  File "/apps/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 909, in predict
    use_multiprocessing=use_multiprocessing)
  File "/apps/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 462, in predict
    steps=steps, callbacks=callbacks, **kwargs)
  File "/apps/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 396, in _model_iteration
    distribution_strategy=strategy)
  File "/apps/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_v2.py", line 594, in _process_inputs
    steps=steps)
  File "/apps/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py", line 2472, in _standardize_user_data
    exception_prefix='input')
  File "/apps/anaconda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 574, in standardize_input_data
    str(data_shape))
ValueError: Error when checking input: expected dense_input to have shape (20,) but got array with shape (1,)

下面是我正在使用的代码,这是我能够开发以重现此代码的最小示例(以及解释我的过程的文档)。谁能评估我做错了什么以及要改变什么?

#!/usr/bin/env python3

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

#################
### CONSTANTS ###
#################
ARANGE = (-5.0, 5.0) # Possible values for m in training data
BRANGE = (0.0, 10.0) # Possible values for b in training data
X_MIN = 1.0 
X_MAX = 9.0 
N = 10 # Number of grid points
M = 2 # Number of {(x,y)} sets to train on


def gen_ab(arange, brange):
    """ mrange, brange are tuples of floats """
    a = (arange[1] - arange[0])*np.random.rand() + arange[0]
    b = (brange[1] - brange[0])*np.random.rand() + brange[0]

    return (a, b)

def build_model(x_data, y_data):
    """ Build the model using input / output training data
    Args:
        x_data (np array): Size (m, n*2) grid of input training data.
        y_data (np array): Size (m, 2) grid of output training data.
    Returns:
        model (Sequential model)
    """
    model = keras.Sequential()
    model.add(layers.Dense(64, activation='relu', input_dim=len(x_data[0])))
    model.add(layers.Dense(len(y_data[0])))

    optimizer = tf.keras.optimizers.RMSprop(0.001)
    model.compile(loss='mse', optimizer=optimizer, metrics=['mae', 'mse'])

    return model


def gen_data(xs, arange, brange, m):
    """ Generate training data for lines of y = m*x + b
    Args:
        xs (list): Grid points (size N1)
        arange (tuple): Range to use for a (a_min, a_max)
        brange (tuple): Range to use for b (b_min, b_max)
        m (int): Number of y grids to generate
    Returns:
        x_data (np array): Size (m, n*2) grid of input training data.
        y_data (np array): Size (m, 2) grid of output training data.
    """
    n = len(xs)
    x_data = np.zeros((m, 2*n))
    y_data = np.zeros((m, 2))
    for ix in range(m):
        (a, b) = gen_ab(arange, brange)
        ys = a*xs + b*np.ones(xs.size)
        x_data[ix, :] = np.concatenate((xs, ys))
        y_data[ix, :] = [a, b]

    return (x_data, y_data)

def main():
    """ Main routin """
    # Generate the x axis grid to be used for all training sets
    xs = np.linspace(X_MIN, X_MAX, N)

    # Generate the training data
    # x_train has M rows (M is the number of training samples)
    # x_train has 2*N columns (first N columns are x, second N columns are y)
    # y_train has M rows, each of which has two columns (a, b) for y = ax + b
    (x_train, y_train) = gen_data(xs, ARANGE, BRANGE, M)

    model = build_model(x_train, y_train)
    model.fit(x_train, y_train, epochs=10, batch_size=32)
    model.summary()

    ####################
    ### Test example ###
    ####################
    (a, b) = gen_ab(ARANGE, BRANGE)
    ys = a*xs + b*np.ones(xs.size)
    rys = np.concatenate((xs, ys))
    ab1 = model.predict(x_train) # This succeeds
    print(a, b)
    print(ab1)
    ab2 = model.predict(rys) # This fails

if __name__ == "__main__":
    main()

标签: python-3.xtensorflow

解决方案


解决这个问题的方法非常简单。您只需将输入数据作为一批大小的数据传入。改变:

ab2 = model.predict(rys)

ab2 = model.predict(np.array([rys]))

让它工作!


推荐阅读