trained_model, optimizer, criterion, loss_hist, loss_val_hist = train_classifier.run_train( n_epochs=n_epochs, lr=learning_rate, batch_size=batch_size) pre.save_results(loss_hist, loss_val_hist, f'{model_name}') #get parameters get the paramteres fro the last time forwads was called #since at the end of traning we check the accuracy of the train data, the pasarmeter have a size of all the train data detected_patterns = model.get_detected_patterns1() for idx in range(10): plt.figure(1, figsize=(20, 10)) for p in range(trained_model.n_patterns1): pattern = detected_patterns[idx][p].reshape( detected_patterns.shape[2], detected_patterns.shape[3]) patern_np = pattern.detach().numpy().reshape(8, 8) plt.subplot(2, 3, 1 + p) plt.imshow(patern_np, cmap='hot', interpolation='none') pre.save_plt_as_image(plt, f'patterns1_{idx}') detected_patterns = model.get_detected_patterns2() for idx in range(10): plt.figure(1, figsize=(20, 20)) for p in range(trained_model.n_patterns2): pattern = detected_patterns[idx][p].reshape( detected_patterns.shape[2], detected_patterns.shape[3]) patern_np = pattern.detach().numpy().reshape(4, 4) plt.subplot(4, 4, 1 + p) plt.imshow(patern_np, cmap='hot', interpolation='none') pre.save_plt_as_image(plt, f'patterns2_{idx}') if args.s_model: m_exporter = ModelExporter('digits') m_exporter.save_nn_model(trained_model, optimizer, 0, n_classes,
plt.figure(1, figsize=(20, 10)) for idx in range(30): image = X_test[idx].detach().numpy().reshape(16, 16) image2 = X_test_encoded[idx].detach().numpy().reshape( int(math.sqrt(n_features_encoded)), int(math.sqrt(n_features_encoded))) image3 = X_test_decoded[idx].detach().numpy().reshape(16, 16) # Call signature: subplot(nrows, ncols, index, **kwargs) plt.subplot(10, 9, 1 + idx * 3) plt.imshow(image, cmap='hot', interpolation='none') plt.subplot(10, 9, 2 + idx * 3) plt.imshow(image2, cmap='winter', interpolation='none') plt.subplot(10, 9, 3 + idx * 3) plt.imshow(image3, cmap='winter', interpolation='none') pre.save_plt_as_image(plt, f'encoded_{n_features_encoded}') #for idx in range(30): # plt.figure(1, figsize=(10, 5)) # plt.subplot(1, 3, 1) # X_test_np = X_test[idx].detach().numpy().reshape(16, 16) # plt.imshow(X_test_np, cmap='hot', interpolation='none') # # plt.subplot(1, 3, 2) # test_num_np = X_test_encoded[idx].detach().numpy().reshape(int(math.sqrt(n_features_encoded)), int(math.sqrt(n_features_encoded))) # plt.imshow(test_num_np, cmap='hot', interpolation='none') # # plt.subplot(1, 3, 3) # X_test_decoded_np = X_test_decoded[idx].detach().numpy().reshape(16, 16) # plt.imshow(X_test_decoded_np, cmap='hot', interpolation='none') #
model = m_importer.load_nn_model(model_name, 0, 10, 100) image = model.reshape_data(torch.tensor(img_array, device=device, dtype=dtype)) y_test = torch.tensor(int(args.num), device=device, dtype=torch.long) y_pred = model(image).argmax(1) accuracy_soft = (y_pred == y_test).float().mean() print(f'number predicted {y_pred.item()}') pre = Preprocessing('digits') detected_patterns = model.get_detected_patterns1() plt.figure(1, figsize=(20, 10)) for p in range(model.n_patterns1): pattern = detected_patterns[0][p].reshape(detected_patterns.shape[2], detected_patterns.shape[3]) patern_np = pattern.detach().numpy().reshape(8, 8) plt.subplot(2, 3, 1 + p) plt.imshow(patern_np, cmap='hot', interpolation='none') pre.save_plt_as_image(plt, f'written_number {args.num}_patterns') detected_patterns = model.get_detected_patterns2() plt.figure(1, figsize=(20, 20)) for p in range(model.n_patterns2): pattern = detected_patterns[0][p].reshape(detected_patterns.shape[2], detected_patterns.shape[3]) patern_np = pattern.detach().numpy().reshape(4, 4) plt.subplot(4, 4, 1 + p) plt.imshow(patern_np, cmap='hot', interpolation='none') pre.save_plt_as_image(plt, f'written_number {args.num}_patterns2')
print(t, loss.item()) print(time.time() - start) y_pred = model(X_train).argmax(1) accuracy_soft = (y_pred == y_train.long()).float().mean() print(f'training accuracy: {accuracy_soft}') print(f'optimal iteration: {acc_val.index(max(acc_val))}') title = f'train_acc_val_acc_vs_iter_{n_iter}' plt.figure(1) plt.plot(acc_train) plt.plot(acc_val) plt.title = title pre.save_plt_as_image(plt, title) plt.close() title = f'train_loss_val_loss_vs_iter_{n_iter}' plt.figure(2) plt.plot(loss_hist) plt.plot(loss_val_hist) plt.title = title pre.save_plt_as_image(plt, title) #plt.show() if args.s_model: name = f'custom_{n_features}.pt' pre.save_model(model, name=name) #torch.save(model, name)