Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

python - Why my Tensorflow model can't predict a simple quadratic equation of degree 2?

I tried to create a model in TensorFlow to predict a simple y = x^2 function. But my model behaves completely wrong. How can I fix it?

MODEL
model = Sequential([Dense(units=1, input_shape=[1], use_bias=True),
                   Activation('relu')])

model.compile(optimizer='Adam', loss='mean_squared_logarithmic_error',
              metrics=['mae','accuracy'])
VARIABLE X
xs1 = np.array(list(range(-100,100,1)), dtype=float)
VARIABLE Y
ys = xs1*xs1
FIT THE MODEL
model.fit(xs1, ys, batch_size=30, epochs=500)
PREDICT
y_predict = model.predict(xs1)
SHOW PREDICTION AND REAL VALUES
plt.scatter(x, y_predict)
plt.scatter(xs1, ys)

PLOT: Prediction is completely wrong. The model arrives to what seems a linear equation when it should be a quadratic

RESULTS OF PREDICTIONS
array([[426.38446  ],
       [422.14005  ],
       [417.89563  ],
       [413.6512   ],
       [409.4068   ],
       [405.16238  ],
       [400.91797  ],
       [396.67355  ],
       [392.42914  ],
       [388.18472  ],
       [383.9403   ],
       [379.6959   ],
       [375.45148  ],
       [371.20706  ],
       [366.96265  ],
       [362.71823  ],
       [358.47382  ],
       [354.2294   ],
       [349.985    ],
       [345.74057  ],
       [341.49612  ],
       [337.2517   ],
       [333.0073   ],
       [328.76288  ],
       [324.51846  ],
       [320.27405  ],
       [316.02963  ],
       [311.78522  ],
       [307.5408   ],
       [303.2964   ],
       [299.05197  ],
       [294.80756  ],
       [290.56314  ],
       [286.31873  ],
       [282.0743   ],
       [277.8299   ],
       [273.58548  ],
       [269.34106  ],
       [265.09665  ],
       [260.85223  ],
       [256.60782  ],
       [252.3634   ],
       [248.11899  ],
       [243.87457  ],
       [239.63016  ],
       [235.38573  ],
       [231.14131  ],
       [226.8969   ],
       [222.65248  ],
       [218.40807  ],
       [214.16365  ],
       [209.91924  ],
       [205.67482  ],
       [201.4304   ],
       [197.18599  ],
       [192.94157  ],
       [188.69716  ],
       [184.45274  ],
       [180.20833  ],
       [175.96391  ],
       [171.71948  ],
       [167.47507  ],
       [163.23065  ],
       [158.98624  ],
       [154.74182  ],
       [150.4974   ],
       [146.25299  ],
       [142.00858  ],
       [137.76416  ],
       [133.51974  ],
       [129.27533  ],
       [125.030914 ],
       [120.7865   ],
       [116.542076 ],
       [112.29766  ],
       [108.053246 ],
       [103.80883  ],
       [ 99.564415 ],
       [ 95.32     ],
       [ 91.075584 ],
       [ 86.83116  ],
       [ 82.58675  ],
       [ 78.34233  ],
       [ 74.097916 ],
       [ 69.8535   ],
       [ 65.609085 ],
       [ 61.36467  ],
       [ 57.12025  ],
       [ 52.875835 ],
       [ 48.63142  ],
       [ 44.387    ],
       [ 40.142586 ],
       [ 35.89817  ],
       [ 31.653757 ],
       [ 27.40934  ],
       [ 23.164923 ],
       [ 18.920507 ],
       [ 14.67609  ],
       [ 10.431675 ],
       [  6.1872582],
       [  1.942842 ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ],
       [  0.       ]], dtype=float32)
question from:https://stackoverflow.com/questions/65644703/why-my-tensorflow-model-cant-predict-a-simple-quadratic-equation-of-degree-2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't predict a non-linear function with a linear one. Try adding another layer or more units

model = Sequential([Dense(units=8, input_shape=[1], use_bias=True),
                    Activation('relu'),
                    Dense(units=1, activation='relu')])

model.compile(optimizer='Adam', loss='mean_squared_logarithmic_error',
              metrics=['mae','accuracy'])

Also why are you using that loss function, wouldn't mse work fine?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...