我一直在尝试将神经网络调整为一个简单的函数:球体的质量。我尝试过不同的架构,例如,一个隐藏层和两个隐藏层,每个隐藏层总是有 128 个神经元,并训练它们 5000 个 epoch。代码是通常的代码。以防万一,我发布其中一个
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])
,keras.layers.Dense(128, activation="relu")
,keras.layers.Dense(1, activation="relu")])
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
history = model.fit(x, y, validation_split=0.2, epochs=5000)
结果显示在图表中。
我怀疑我在某个地方犯了错误,因为我已经看到深度学习能够以更少的时期匹配复杂的函数。我将不胜感激任何解决此问题并与深度学习功能完美契合的提示。
为了清楚起见,我发布了图表的代码。
rs =[x for x in range(20)]
def masas_circulo(x):
masas_circulos =[]
rs =[r for r in range(x)]
for r in rs:
masas_circulos.append(model.predict([r])[0][0])
return masas_circulos
masas_circulos = masas_circulo(20)
masas_circulos
esferas = [4/3*np.pi*r**3 for r in range(20)]
import matplotlib.pyplot as plt
plt.plot(rs,masas_circulos,label="DL")
plt.plot(rs,esferas,label="Real");
plt.title("Mass of an sphere.\nDL (1hl,128 n,5000 e) vs ground_truth")
plt.xlabel("Radius")
plt.ylabel("Sphere")
plt.legend();