def fit_hopfield(params): # get params n_label = params.get('n_label', None) n_sample = params['n_sample'] fit_mode = params['fit_mode'] # load dataset dataset = load_alphabet() # target_name target_names = params.get('target_names', None) if target_names is None: target_names = dataset.target_names[:n_label] # transform data dataset.data = binarize(dataset.data, binary_values=(-1,1)) # create train data X, y = create_train_data(data=dataset.data, target=dataset.target, target_names=target_names, n_sample=n_sample) print_train_data(X, y, target_names) # fit hopfield hf = Hopfield(mode=fit_mode) hf.fit(X, y, watch_weight=False) # set params params['img_shape'] = dataset.image_shape return hf, X, y, target_names, params
def test_load_alphabet(): dataset = load_alphabet() # number of labels assert len(np.unique(dataset.target)) == len(dataset.target_names) # number of data assert len(dataset.data) == len(dataset.target)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import matplotlib.pyplot as plt from data import load_alphabet dataset = load_alphabet() images = [] for t in dataset.target_names: images.append(dataset.images[dataset.target == t][0]) for i, (label, img) in enumerate(zip(dataset.target_names, images)): plt.subplot(2, len(dataset.target_names) / 2, i + 1) plt.imshow(img, cmap="gray") plt.title(label.upper()) plt.axis("off") plt.savefig("alphabet_images.png")