簡體   English   中英

為什么 TensorFlow 不從我的 numpy 陣列中學習,但與其他 numpy ZA3CBC3F9D0CE2F2C1554E1B67 一起使用? (三次回歸)

[英]Why doesn't TensorFlow learn from my numpy array but works with other numpy arrays? (Cubic regression)

這是我的數據生成代碼:

x = []
for i in range(-500, 500):
  x.append(i)

y = []
for i in range(-500, 500):
  y.append(i**3)

x = np.array(x)
y = np.array(y)

我可以 plot 它和一切都很好,但是當我在我的 model 中使用這個數據集時:

#Build model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(4, activation="relu", input_shape = [1]),
    tf.keras.layers.Dense(16, activation="relu"),
    tf.keras.layers.Dense(1)
])

model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-1),
    loss='mean_squared_error',
    metrics=['mean_squared_error']
)

model.fit(x, y, epochs=1000)

結果它給出了奇數:

Epoch 1000/1000
32/32 [==============================] - 0s 2ms/step - loss: 646000595173376.0000 - mean_squared_error: 646000595173376.0000

但如果我使用硬編碼數據集:

x = np.array([-5,-4.8,-4.6,-4.4,-4.2,-4,-3.8,-3.6,-3.4,-3.2,-3,-2.8,-2.6,-2.4,-2.2,-2,-1.8,-1.6,-1.4,-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8,1,1.2,1.4,1.6,1.8,2,2.2,2.4,2.6,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5],  dtype=float)
y = np.array([-45,-40.57424,-36.44992,-32.61648,-29.06336,-25.78,-22.75584,-19.98032,-17.44288,-15.13296,-13.04,-11.15344,-9.46272,-7.95728,-6.62656,-5.46,-4.44704,-3.57712,-2.83968,-2.22416,-1.72,-1.31664,-1.00352,-0.77008,-0.60576,-0.5,-0.44224,-0.42192,-0.42848,-0.45136,-0.48,-0.50384,-0.51232,-0.49488,-0.44096,-0.34,-0.18144,0.04528,0.350720000000001,0.745439999999999,1.24,1.84496,2.57088,3.42832,4.42784,5.58,6.89536,8.38448,10.05792,11.92624,14],  dtype=float)

它給出了非常好的結果,降低到非常低的損失值。 這里有什么問題?

您可以根據數據集類型和值范圍更改編譯參數以獲得所需的 output。

為此,請將“Adam”優化器更改為“SGD”或“RMSprop”,它們是線性值的良好優化器。 還將損失 function 更改為MeanSquaredLogarithmicError()因為您的數據輸入具有異常值。

固定代碼:

model.compile(
    optimizer=tf.keras.optimizers.SGD(learning_rate=1e-1),
    loss=tf.keras.losses.MeanSquaredLogarithmicError(),
    metrics=['MeanSquaredLogarithmicError'])

model.fit(x, y, epochs=10)

Output:

Epoch 1/10
32/32 [==============================] - 5s 6ms/step - loss: 21.2284 - mean_squared_logarithmic_error: 21.2284
Epoch 2/10
32/32 [==============================] - 0s 5ms/step - loss: 11.6544 - mean_squared_logarithmic_error: 11.6544
Epoch 3/10
32/32 [==============================] - 0s 5ms/step - loss: 9.2233 - mean_squared_logarithmic_error: 9.2233
Epoch 4/10
32/32 [==============================] - 0s 6ms/step - loss: 7.8573 - mean_squared_logarithmic_error: 7.8573
Epoch 5/10
32/32 [==============================] - 0s 4ms/step - loss: 6.9383 - mean_squared_logarithmic_error: 6.9383
Epoch 6/10
32/32 [==============================] - 0s 6ms/step - loss: 6.2736 - mean_squared_logarithmic_error: 6.2736
Epoch 7/10
32/32 [==============================] - 0s 8ms/step - loss: 5.7597 - mean_squared_logarithmic_error: 5.7597
Epoch 8/10
32/32 [==============================] - 0s 6ms/step - loss: 5.3538 - mean_squared_logarithmic_error: 5.3538
Epoch 9/10
32/32 [==============================] - 0s 4ms/step - loss: 5.0170 - mean_squared_logarithmic_error: 5.0170
Epoch 10/10
32/32 [==============================] - 0s 4ms/step - loss: 4.7353 - mean_squared_logarithmic_error: 4.7353
<keras.callbacks.History at 0x7f69501378d0>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM