def drowPicture(History): pyplot.plot(History.history['loss']) pyplot.plot(History.history['val_loss']) pyplot.plot(History.history['acc']) pyplot.plot(History.history['val_acc']) pyplot.title('model train vs validation loss') pyplot.ylabel('loss') pyplot.xlabel('epoch') pyplot.legend(['train', 'validation'], loc='upper right') pyplot.show()
return (train_metrics, test_metrics) # 不带约束的 # model = Model(FEATURE_NAMES, hidden_units=10) # model.build_train_op(0.01, unconstrained=True) # results = training_helper(model, train_df, test_df, 100, num_iterations_per_loop=44, num_loops=500) # print("Train Error", results[0]["last_error_rate"]) # print("Train Violation", results[0]["last_max_constraint_violation"]) model = Model(FEATURE_NAMES, hidden_units=10) model.build_train_op(0.01, unconstrained=False) results = training_helper(model, train_df, test_df, 100, num_iterations_per_loop=44, num_loops=500) print("Train Error", results[0]["last_error_rate"]) print("Train Violation", results[0]["last_max_constraint_violation"]) """画图""" plt.title("Error Rate vs Epoch") plt.plot(range(100, len(results[0]["all_errors"])), results[0]["all_errors"][100:], color="green") plt.xlabel("Epoch") plt.show() plt.title("Violation vs Epoch") plt.plot(range(100, len(results[0]["all_violations"])), results[0]["all_violations"][100:], color="blue") plt.xlabel("Epoch") plt.show()
return X, y # define model model = Sequential() model.add(LSTM(10, input_shape=(1, 1))) # activation是激活函数的选择,linear是线性函数 model.add(Dense(1, activation='linear')) # compile model, loss是损失函数的取值方式,mse是mean_squared_error,代表均方误差, # optimizer是优化控制器的选择,AdamOptimizer通过使用动量(参数的移动平均数)来改善传统梯度下降 model.compile(loss='mse', optimizer='adam') # fit model X, y = get_train() valX, valY = get_val() # validation_data是要验证的测试集,shuffle代表是否混淆打乱数据 # 不合格的原因是epochs=100,训练周期不足 history = model.fit(X, y, epochs=100, validation_data=(valX, valY), shuffle=False) # plot train and validation loss # loss是训练集的损失函数值,val_loss是验证数据集的损失值 pyplot.plot(history.history['loss']) pyplot.plot(history.history['val_loss']) pyplot.title('model train vs validation loss') pyplot.ylabel('loss') pyplot.xlabel('epoch') pyplot.legend(['train', 'validation'], loc='upper right') pyplot.show()