Python 3.x 在单个数据集上生成结果时出现Tensorflow错误

Python 3.x 在单个数据集上生成结果时出现Tensorflow错误,python-3.x,tensorflow,Python 3.x,Tensorflow,我正在使用tensorflow尝试我的第一个神经网络,但无法为单个输入样本生成结果。我创建了一个最小的示例,其中我向它提供了多个y=a*x+b输入(用于改变a,b),并尝试返回结果,但失败了。注意,我不关心这里的准确性,我是作为一个POC来做的。一些参数如下: N是x网格点的数量。每个输入行的长度2*N (N表示x,N表示y) M是我给出的训练行数 2是我期望的输出数(a和b) 因此,我的训练数据是x\u-train大小(m,2*n)和y\u-train大小(m,2)。似乎我构建的模型还可以

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

  • N
    是x网格点的数量。每个输入行的长度
    2*N
    N
    表示x,
    N
    表示y)
  • M
    是我给出的训练行数
  • 2
    是我期望的输出数(
    a
    b
因此,我的训练数据是
x\u-train
大小
(m,2*n)
y\u-train
大小
(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,)

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

ab2=model.predict(rys)

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

成功了

#!/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()