You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Numpy Tile函数使用异常:神经网络训练数据基数不匹配报错

问题

我正在构建一个实现两数相加的神经网络,相关代码如下:

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
import numpy as np

num_train = 100000

X_train = np.random.rand(num_train, 2)
y_train = X_train[:, 0] + X_train[:, 1]

norm = tf.keras.layers.Normalization(axis=-1)
norm.adapt(X_train)
Xn = norm(X_train)

Xt = np.tile(Xn, (1000, 1))
yt = np.tile(y_train, (1000, 1))

model = Sequential(
        [
            Dense(10, activation='relu'),
            Dense(1, activation ='relu')
            ]       
        )

model.compile(loss = 'mse', optimizer='adam')
batch_size = 32
epochs = 100
model.fit(Xt, yt, batch_size=batch_size, epochs=epochs, verbose = 0)

test_input = np.array([[1, 2]])
predicted_sum = model.predict(test_input)
print(predicted_sum)

未添加数据平铺(tile)代码时,代码可运行但预测结果不准;添加后运行出现如下报错:

line 114, in check_data_cardinality
    raise ValueError(msg)
ValueError: Data cardinality is ambiguous. Make sure all arrays contain the same number of samples.'x' sizes: 10000000
0
'y' sizes: 1000

按计算,yt经np.tile处理后样本量应与Xt一致为10000000,但实际仍为1000,请问这是什么原因?

原因与解决方法
  • 核心原因:y_train是一维数组(形状为(100000,)),使用np.tile(y_train, (1000, 1))时,numpy会将其视为行向量进行平铺,最终得到的yt形状是(1000, 100000),样本数为1000;而Xt的形状是(10000000, 2),样本数为10000000,两者样本数量不匹配,导致报错。

  • 解决步骤:

    1. 先将y_train转换为二维数组(形状(100000, 1)),再进行平铺操作,保证平铺后的维度和Xt对齐。
    2. 测试输入需要经过同样的归一化处理,否则预测结果会偏离预期。
    3. 回归任务中输出层改用linear激活函数,避免relu截断接近0的输出影响精度。

修改后的代码如下:

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
import numpy as np

num_train = 100000

X_train = np.random.rand(num_train, 2)
# 将y_train转为二维数组
y_train = X_train[:, 0] + X_train[:, 1]
y_train = y_train.reshape(-1, 1)

norm = tf.keras.layers.Normalization(axis=-1)
norm.adapt(X_train)
Xn = norm(X_train)

Xt = np.tile(Xn, (1000, 1))
# 对二维的y_train进行平铺,得到形状(10000000, 1)的yt
yt = np.tile(y_train, (1000, 1))

model = Sequential(
        [
            Dense(10, activation='relu'),
            Dense(1, activation='linear')  # 回归任务用linear激活更合适
            ]       
        )

model.compile(loss='mse', optimizer='adam')
batch_size = 32
epochs = 100
model.fit(Xt, yt, batch_size=batch_size, epochs=epochs, verbose=1)

# 测试输入需要经过同样的归一化处理
test_input = np.array([[1, 2]])
test_input_norm = norm(test_input)
predicted_sum = model.predict(test_input_norm)
print(predicted_sum)

内容的提问来源于stack exchange,提问作者harry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:41:15