'{:.0E}'.format(lam)) my_path = Path('..', 'model_output', 'part_2', filename + '_train.json') train_path = Path(__file__).parent.resolve().joinpath(my_path) # Make output directory if doesn't exist. output_dir = train_path.parent.resolve() if not Path(output_dir).exists(): Path(output_dir).mkdir() with open(train_path, 'w') as f: json.dump(learned_model, f, indent=4) # If gradient didn't explode, get predictions on validation set. if learned_model['exploding'] is False: print('Calculating predictions on validation set...') # Grab weights to input to prediction method. weights = learned_model['weights'] predictions = model.predict_validation(weights) # Output for predictions and SSE for validation set. dev_filename = filename + '_dev.json' dev_path = train_path.with_name(dev_filename) with open(dev_path, 'w') as f: json.dump(predictions, f, indent=4) print('Predictions complete.\n') print('Part 2 complete.\n')
from models.linear_model import LinearModel pp = pprint.PrettyPrinter() model = LinearModel(train='data/PA1_train.pkl', validation='data/PA1_dev.pkl', test='data/PA1_test.pkl', target='price', rate=1e-05, lam=0.001, eps=0.5, normalize=True) names = model.weight_labels learned_model = model.train_model(50000) val_predictions = model.predict_validation( learned_model['weights'])['predictions'] test_predictions = model.predict_test( (learned_model['weights']))['predictions'] prediction_output = pathlib.Path('model_output/predictions.pkl') prediction_file = pathlib.Path('model_output/predictions.txt') pred_output_path = pathlib.Path(__file__).parent.resolve().joinpath( prediction_output) pred_file_path = pathlib.Path(__file__).parent.resolve().joinpath( prediction_file) # Save predictions with open(pred_output_path, 'wb') as fp: pickle.dump(test_predictions, fp, pickle.HIGHEST_PROTOCOL)