在使用keras进行机器学习的练习中,下面代码期望是永远不会结束,但是在实际中执行几步后程序就会退出,有大神知道怎么解决吗?代码如下:
import os
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
while True:
x = np.linspace(-2, 6, 20000)
np.random.shuffle(x)
y = 0.5 * x*x + 2*x +3+np.random.randn(20000,)*0.01
model = Sequential()
model.add(Dense(64, activation='relu',input_shape=(1,)))
model.add(Dense(1,activation='relu'))
model.compile(loss='mae', optimizer='sgd')#绝对值均差,公式为(|y_pred-y_true|).mean()
model.fit(x, y, epochs=10, batch_size=40)
x_test=np.linspace(-2, 6, 200)
y_test = 0.5 * x_test*x_test + 2*x_test +3
cost_eval = model.evaluate(x_test, y_test)
model.summary()
y_prediction = model.predict(x_test)
if y_prediction[-1]<10:
print('????')
break
print('????')
将y_prediction打印出来就发现了,y_prediction最后值全部变为了0
x_test和y_test的值固定,那么就是model一直在变化
多加几个print语句,逐个分析