关于使用Tensorflow的函数逼近神经网络输出层单元数的问题

Question about the number of units in the output layer for a function approximation neural network using Tensorflow

我正在尝试实现一个密集神经网络来近似给定函数 y=0.9x^3 + 2x^2 + 12。我创建了 50,000 个样本,其中包括 x 和相应的 y 值。 第一个隐藏层必须有 12 个单元,第二个隐藏层必须有 8 个单元,最后一个必须有 4 个单元。这是我到目前为止为实现此结构所做的工作:

model = Sequential()
model.add (Dense(12, input_shape = (50000,), activation = 'relu'))
model.add (Dense(8, activation = 'relu'))
model.add (Dense(4, activation = 'relu'))
model.add (Dense(50000, activation = 'linear')) 

我为输出层指定了 50,000 个单位,因为我的每个样本都有 50,000 个 y 值 - 这对于输出层的单位数是否正确?

您正在计算一个函数,其中一个 X 作为输入,一个 Y 作为输出:

y=0.9x^3 + 2x^2 + 12

所以输入形状和输出形状都应该是 1。我认为你犯了一个错误,每个样本都有 50000 个 y 值。你的功能是不可能的。你有 50000 个样本,每个样本有 1 个 X 和 1 个 Y。

例如:

x = tf.random.uniform((50000,),dtype=tf.float32, minval=0, maxval=100)#random x values
y = tf.multiply(0.9,tf.pow(x,3)) + tf.multiply(2,tf.pow(x,2)) + 12   #y= 0.9x^3 + 2x^2 + 12

那么,对你提议的模型的修改应该是这样的:

model = tf.keras.models.Sequential()
model.add (Dense(12, input_shape = (1,), activation = 'relu'))
model.add (Dense(8, activation = 'relu'))
model.add (Dense(4, activation = 'relu'))
model.add (Dense(1, activation = 'linear'))