在单个数据集上生成结果的张量流错误



我正在用张量流尝试我的第一个神经网络,但无法为单个输入样本生成结果。我创建了一个最小的例子,我正在向它提供多个y = a * x + b输入(对于不同的ab(并试图得到一个结果,但它失败了。请注意,我在这里不关心准确性,我是作为 POC 这样做的。一些参数如下:

  • N是 x 网格点的数量。每个输入行的长度为2*N(N表示 x,N表示 y(。
  • M是我给出的训练行数。
  • 2是我期望的输出数量(ab(。

因此,我的训练数据x_train大小(m, 2*n),大小(m, 2)y_train。似乎我可以正常构建模型,但我无法为它提供大小为(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()

事实证明,解决方案非常简单。您只需将输入数据作为大小为 1 的批次传入即可。改变:

ab2 = model.predict(rys)

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

成功了!

最新更新