def test(model, test_data, noise, normalize): """ test function """ count = 0 for img, _ in test_data: img = img.view(-1, 784) noise_img = add_noise(img, noise=noise, normalize=normalize) out = model(noise_img.to(device)) out = out / out.max().item() out = out.detach().cpu() img = torch.chunk(img, args.batch_size_test) noise_img = torch.chunk(noise_img, args.batch_size_test) out = torch.chunk(out, args.batch_size_test) # store training progress in tensorboard if requested if args.tensorboard: writer.add_image( 'test_model', image_to_tensor( plot_to_image( create_plot_grid( img[0].view(28, 28), noise_img[0].view(28, 28), np.clip(out[0].view(28, 28), 0., 1.), names=['raw', 'noise %s' % noise, 'denoised']))), count) count = count + 1
def process_graphic_error_hopfield(vowel,hopfield_net,title,its = 100): x = [0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80] y = [0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for i in range(0,its): for j in range(0,len(y)): new_p = add_noise(vowel,x[j],1,-1) retrive_p = hopfield_net.retrieve(new_p) y[j] += get_accurracy(vowel,retrive_p) y = [i/100 for i in y] plt.plot(x,y) plt.title(title) plt.xlabel("Noise") plt.ylabel("Acurracy %") plt.show()
def test(model, test_data, noise, normalize): """ test function """ count = 0 for img, _ in test_data: noise_img = add_noise(img, noise=noise, normalize=normalize) out = model(noise_img.to(device)) out = out / out.max().item() out = out.detach().cpu() img = torch.chunk(img, args.batch_size_test) noise_img = torch.chunk(noise_img, args.batch_size_test) out = torch.chunk(out, args.batch_size_test) # store training progress in tensorboard if requested if args.tensorboard: writer.add_image( 'test_model', image_to_tensor( plot_to_image( create_plot_grid( img[0].view(28, 28), noise_img[0].view(28, 28), np.clip(out[0].view(28, 28), 0., 1.), names=['raw', 'noise %s' % noise, 'denoised']))), count) # display summary if count == 0: dims = (1400, 600) summary = Image.new('L', (dims[0], 5 * dims[1]), 255) i = 0 while i < 5: tmp_img = plot_to_image( create_plot_grid(img[i].view(28, 28), noise_img[i].view(28, 28), out[i].view(28, 28), names=None)) summary.paste(tmp_img, (0, i * dims[1])) i += 1 plt.imshow(summary, cmap=plt.cm.gray) plt.show() count = count + 1
def train(model, data_train, optim, loss_fn, epochs, noise, norm): """ train function """ loss_list_result = [] for epoch in range(epochs): loss = 0 for inputs, _ in data_train: # reshape inputs inputs = inputs.view(-1, 784) # add noise noise_img = add_noise(inputs, noise=noise, normalize=norm) # reset gradients optim.zero_grad() # move model (ie. net) to appropiate device model.to(device) # compute reconstructions outputs = model(noise_img.to(device)) # compute a training reconstruction loss train_loss = loss_fn(outputs.to(device), inputs.to(device)) # compute accumulated gradients train_loss.backward() # update parameters based on current gradients optim.step() #add the batch training loss to epoch loss loss += train_loss.item() # compute the epoch training loss loss = loss / len(data_train) # and add it to results list loss_list_result.append(loss) # print info every epoch if args.verbose and not args.suppress: print('Epoch {} of {}, loss={:.3}'.format(epoch + 1, epochs, loss)) else: # print info every 10th epoch if epoch % 10 == 0 and not args.suppress: print('Epoch {} of {}, loss={:.3}'.format( epoch + 1, epochs, loss)) # store training progress in tensorboard if requested if args.tensorboard: writer.add_scalar('train/Epoch loss', loss, epoch) writer.flush() return loss_list_result
def process_graphic_error_steinbuch(vowel,steinbuch_net,title,target,its = 100): x = [0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80] y = [0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] t = np.array([0,0,0,0,0]) t[target] = 1 t = t.reshape(-1,1) for i in range(0,its): for j in range(0,len(y)): new_p = add_noise(vowel,x[j],1,0) retrieve_p = steinbuch_net.retrieve(new_p) y[j] += get_accurracy(t,retrieve_p) y = [i/100 for i in y] plt.plot(x,y) plt.title(title) plt.xlabel("Noise") plt.ylabel("Acurracy %") plt.show()