由TensorFlow2训练的简单线性回归模型的池性能

时间:2020-11-12 14:32:51

标签: tensorflow linear-regression tensorflow2.0

我的模型像fig2 = figure(x_range=x, y_range=[0, 1], plot_width=1500, plot_height=600) line1_2 = fig2.line(x, churn_40d_12m_all, color='blue', line_width=2) circle1_2 = fig2.circle(x, churn_40d_12m_all, color='blue') line2_2 = fig2.line(x, churn_40d_12m_b2b, color='brown', line_width=2) circle2_2 = fig2.circle(x, churn_40d_12m_b2b, color='brown') line3_2 = fig2.line(x, churn_40d_12m_b2c, color='green', line_width=2) circle3_2 = fig2.circle(x, churn_40d_12m_b2c, color='green') line4_2 = fig2.line(x, churn_40d_6m, color='red', line_width=2) circle4_2 = fig2.circle(x, churn_40d_6m, color='red') fig2.xaxis.axis_label = 'Month' fig2.xaxis.major_label_orientation= math.pi/2 fig2.xaxis.axis_label_text_font_size = "20px" fig2.yaxis.axis_label_text_font_size = "20px" fig2.yaxis.axis_label = 'Values' fig2.yaxis[0].formatter = NumeralTickFormatter(format="0.0%") fig2.add_tools(hover) fig2.add_layout(Legend(), 'right') legend2 = Legend(items=[ ("Line1", [line1_2, circle1_2]), ("Line2", [line2_2, circle2_2]), ("Line3", [line3_2, circle3_2]), ("Line4", [line4_2, circle4_2]), ], location="top_right") fig2.add_layout(legend2, 'right') legend2.click_policy = 'hide' legend2.title="MultilineChart1" legend2.title_text_font_size = "20px" legend2.title_text_font_style = "bold" legend2.label_text_font_size = "15px" tab2 = Panel(child=fig2, title="MultiLineChart1") 一样简单,但是我无法以简单的方式获得正确的结果。我不知道发生了什么。

hover = HoverTool(tooltips=[
    ('Values', '@y{0.00 %}'),
    ('Line', 'name of the current line')
])

当验证损失很大时,我的模型总是停止:

Epoch 901/2000 5/5 [=============================]-0s 3ms / step- 损失:14767.1357-损失:166.8979

而且我得到的训练后参数总是不正确:

y = 2*x + 200 + error

[
]

请帮助我弄清楚我的代码出了什么问题。

我使用tensorflow-v2.3.0

1 个答案:

答案 0 :(得分:0)

我明白了,主要问题是EarlyStopping太早停止了我的训练过程!另一个问题是学习率太小。

所以当我更改两个参数设置时,我得到了正确的结果:

import numpy as np
from tensorflow import keras
x = np.arange(100)
error = np.random.rand(100,1).ravel()
y = 2*x + 200 + error

opt = keras.optimizers.Adam(lr=0.8)  # <--- bigger lr
model = keras.Sequential([keras.layers.Dense(1, input_shape=[1])])
model.compile(optimizer=opt, loss='mse', metrics=['mae'])
early_stopping_callback = keras.callbacks.EarlyStopping(
        patience=100,  # <--- longer patience to training
        monitor='val_loss',
        mode='min',
        restore_best_weights=True)
history = model.fit(x, y, epochs=2000, batch_size=16, verbose=1,
                    validation_split=0.2, callbacks=[early_stopping_callback])