Beispiel #1
0
def main():
	#Load .xls .xlsx file
	file_name = raw_input('File name: ')
	work_book = load_workbook(file_name)

	#Load sheet
	print (work_book.get_sheet_names())
	sheet_name = raw_input('\nSheet name: ')
	work_sheet = work_book.get_sheet_by_name(sheet_name)

	#Select workspace in sheet
	begin_position = raw_input('Begin position: ')
	end_position = raw_input('End position: ')
	workspace = work_sheet[begin_position : end_position] #Workspace(begin, end)

	#Load data
	training_set = []
	for row in workspace:
		class_name = row[0].value # first column is class name

		for item in row[1:]: # skip first column
			inputs = np.array([item.value])
			training_set.append((inputs, class_name))

	#Create SOM ANN
	som_ann = SOM((1, 4, 4))

	#Train network
	print 'Wait, I\'m traning...'
	som_ann.train([i[0] for i in training_set], 200, -1, 'quadratic')

	somplot(som_ann, training_set)
Beispiel #2
0
    def som(self, m, n, dim, iterations):
        # step 4
        trainData = numpy.zeros((len(self.contrastPoints), 1))

        for i in range(len(self.contrastPoints)):
            trainData[i] = [self.contrastPoints[i]]

        som = SOM(m, n, dim, iterations)
        som.train(trainData)    
        mapped = som.map_vects(trainData)
        d0 = 0
        d1 = 0
        d2 = 0

        for d, m in zip(trainData, mapped):
            mapv = m[0] + m[1]
            
            if (d0 == 0 and mapv == 0):
                d0 = d[0] if d0 < d[0] else d0

            if (d1 == 0  and mapv == 1):    
                d1 = d[0] if d1 < d[0] else d1
                
            if (d2 == 0  and mapv == 2): 
                d2 = d[0] if d2 < d[0] else d2
                
        least = 0 if d0 < d1 and d0 < d2 else (1 if d1 < d0 and d1 < d2 else (2 if d2 < d1 and d2 < d0 else 0))
        most = 0 if d0 > d1 and d0 > d2 else (1 if d1 > d0 and d1 > d2 else (2 if d2 > d1 and d2 > d0 else 0))
        mean = 0 if d0 > d1 and d0 < d2 else (1 if d1 > d0 and d1 < d2 else (2 if d2 > d1 and d2 < d0 else 0))
        print(d0, d1, d2)
        print(least, most, mean)
        self.mappingDict = {'least':  least, 'mean': mean, 'most': most}  
        self.blocksMapping = mapped
Beispiel #3
0
def call_som(m, n, dim, input_data, weight_after_insertion=None, alpha=None, sigma=None):
    # get the shape of input data inorder to calc iternumber in SOM
    iter_no = input_data.shape[0]*10
    som = SOM(m, n, dim, weight_after_insertion, n_iterations= iter_no)
    trained_weight = som.train(input_data)
    mapped = som.map_vects(input_data)
    result = np.array(mapped)
    return trained_weight, result
Beispiel #4
0
 def on_clicked_train(self, widget):
     print("train")
     som = SOM(self.net_size_x_sb.get_value_as_int(), self.net_size_y_sb.get_value_as_int(), self.standar_diviison_sb.get_value() / 10, self.learning_rate_sb.get_value() / 10, self.training_times_sb.get_value_as_int(), self.status_flag)
     som.net_init()
     thread = threading.Thread(target=som.training(self.training_set[0], self.draw_function))
     thread.daemon = True
     thread.start()
     # som.printnet()
     self.draw_function(som.get_net())
     print("end")
Beispiel #5
0
def call_som(m,
             n,
             dim,
             input_data,
             weight_after_insertion=None,
             alpha=None,
             sigma=None):
    som = SOM(m, n, dim, weight_after_insertion)
    trained_weight = som.train(input_data)
    mapped = som.map_vects(input_data)
    result = np.array(mapped)
    return trained_weight, result
Beispiel #6
0
def solve_tsp(filename: str):
    # Parse problem from txt file
    problem = parse_tsp_problem(filename)
    cases = problem[:, 1:]

    # Initialize self organizing map
    som = SOM(cases=cases)
    # Train
    som.train()

    # Use Som weights to generate a solution for tsp problem
    solution = create_tsp_solution(som.cases, som.weights)
    visualize_tsp(solution, som.weights)
Beispiel #7
0
 def __init__(self, dataset, nodes, sigma_0, learn_rate_0, total_iterations,
              scaler, sigma_timeconst, learn_t, plot_interval):
     SOM.__init__(self,
                  dataset,
                  nodes,
                  sigma_0=sigma_0,
                  learn_rate_0=learn_rate_0,
                  total_iterations=total_iterations,
                  sigma_timeconst=sigma_timeconst,
                  learn_timeconst=learn_t,
                  plot_interval=plot_interval,
                  graphics=TSPGraphics())
     self.scaler = scaler
def SortingSelfOrganizingMap(Feature_List):
    # Train a SS_LINES x SS_COLS self-organizing map using NUM_ITERATIONS iterations
    som = SOM(SS_LINES, SS_COLS, len(Feature_List[0]), NUM_ITERATIONS)
    som.train(Feature_List)
    mapped = som.map_vects(Feature_List)

    # Grayscale landscape
    sorting_landscape = np.zeros((SS_LINES, SS_COLS), np.float16)

    for i in range(len(mapped)):
        lin = mapped[i][0]
        col = mapped[i][1]
        sorting_landscape[lin][col] = Feature_List[i][0]

    io.imsave('sorted_landscape.png', sorting_landscape)
 def __init__(self, train_x, train_y, test_x, test_y, nodes, sigma_0,
              learn_rate_0, total_iterations, learn_rate_timeconst, scaler,
              sigma_timeconst, plot_interval):
     SOM.__init__(self,
                  train_x,
                  nodes,
                  sigma_0,
                  learn_rate_0,
                  sigma_timeconst,
                  learn_rate_timeconst,
                  total_iterations,
                  plot_interval=plot_interval,
                  graphics=MNISTGraphics())
     self.scaler = scaler
     self.train_y = train_y
     self.test_x = test_x
     self.test_y = test_y
Beispiel #10
0
def som_plot(img, som_dim):
    # Method for creating a small Kohonen Self-Organizing Map (SOM) of the image and
    # plotting the SOM on the screen. RGB values of the colors are printed on the screen.

    # Select small amount of random pixels from the image.
    n_pixels = 500

    colors = shuffle(img.reshape((img.shape[0] * img.shape[1], 3)))[:n_pixels]

    print('\n')
    print('=' * 80)
    print('Self-Organized Map of {} randomly picked colors:'.format(som_dim *
                                                                    som_dim))

    # Train the SOM model with small amount of iterations.
    som = SOM(som_dim, som_dim, 3, 10)
    som.train(colors)

    #Get output grid from the SOM. This is plotted as color palette.
    image_grid = som.get_centroids()
    plt.figure(figsize=(5, 5))
    plt.imshow(image_grid)
    plt.title('Color map')
    plt.show()

    # Create RGB palette values from the image_grid.
    grid = np.array(image_grid)
    grid *= 255
    grid = grid.astype(np.int)
    rgb_df = pd.DataFrame(np.zeros((som_dim, som_dim)))
    hex_df = pd.DataFrame(np.zeros((som_dim, som_dim)))

    for i in range(som_dim):
        for j in range(som_dim):
            rgb_df.iloc[i, j] = str(grid[i, j])
            hex_color = '#{:02x}{:02x}{:02x}'.format(grid[i, j, 0],
                                                     grid[i, j, 1], grid[i, j,
                                                                         2])
            hex_df.iloc[i, j] = hex_color

    print('RGB values:')
    print(rgb_df)
    print('\n')
    print('HEX color values:')
    print(hex_df)
Beispiel #11
0
def max_dist(som):
    """calculate maximum distance between code vectors"""
    max_distance = 0
    for q in som.nodes:
        for i in som.nodes:
            d = SOM.vector_distance(q.weights, i.weights)
            if d > max_distance:
                max_distance = d
    return max_distance
Beispiel #12
0
def max_dist(som):
    """calculate maximum distance between code vectors"""
    max_distance = 0
    for q in som.nodes:
        for i in som.nodes:
            d = SOM.vector_distance(q.weights, i.weights)
            if d > max_distance:
                max_distance = d
    return max_distance
Beispiel #13
0
def main():
	'''
	Create and train a SOM with the XOR problem

	SOM settings
	------------
	The SOM used in this test works in a 2D space with a 
	map's weights of 2x2

	Train settings
	---------------
	The SOM will be trained with the XOR problem, this training
	will run in 2000 iterations. In the weights adjustment function, a 
	zero neighborhood radius, that mean that only the winner neuron's 
	weights will be updated.
	'''
	som_ann = SOM((2, 2, 2))

	training_set = [
		np.array([1, 0]),
		np.array([0, 1]),
		np.array([1, 1]),
		np.array([0, 0]),
	]

	#Train network
	converged, epochs = som_ann.train(training_set, 2000, -1, 'zero')
	if not converged:
		print 'SOM didn\'t converge with the minimum learning rate\n'
	else:
		print 'SOM converged in {0} epochs\n'.format(epochs)

	#Mapping values
	for entry in training_set:
		class_name = som_ann.map(entry)
		print 'The object {0} belongs to class: {1}'.format(entry, class_name)

	#Get the full mapped classes
	print ''
	elements = som_ann.get_mapped_classes()
	print elements
Beispiel #14
0
def main():
    img_list = glob.glob(join(in_path, '*.jpg'))
    vectors = np.empty((0, 64 * 48 * 3), int)
    for i, _file in enumerate(img_list):
        print(_file)
        try:
            np_img, raw = read_img(_file)
        except OSError as e:
            print(e.strerror)
            continue
        vectors = np.append(vectors, np_img, axis=0)
        #if i > 100:
        #    break

    N = 200
    som = SOM(vectors, N=N, seed=10)

    # learn
    som.learn(vectors)
    with open('som.pickle', mode='wb') as f:
        pickle.dump(som, f)
    print(som.W.shape)
Beispiel #15
0
def SelfOrganizingMap(Feature_List):
    som = SOM(SOM_LINES, SOM_COLS, len(Feature_List[0]), SOM_ITERATIONS)
    som.train(Feature_List)

    image_grid = som.get_centroids()
    mapped = som.map_vects(Feature_List)

    return mapped, image_grid
Beispiel #16
0
def main():
	# FIT THE LDA
	lda = TagsLDA(skip=10)
	theta, beta = lda.train(ntopics=30, niter=300, seed=42)

	# TRAIN THE SOM
	dist_func = lambda p, q: divergence(p, q)
	som = SOM(grid_shape=(20, 20), ndims=lda.nterms, dist_func=dist_func)		# len(dictionary) = len(dictionary.token2id)
	som.train(lda.beta, nepochs=10)
	
	# PLOT
	labels = [lda.topic_representatives(i, topn=3, show_scores=False) for i in range(lda.ntopics)]
	areas = np.array([30*2*np.pi*score**2 for score in lda.topic_significances()])
	areas_top, top_indices = zip(*sorted([(a, i) for i, a in enumerate(areas)], reverse=True)[:10])
	locations = som.get_locations(vecs=lda.beta[top_indices, :], labels=labels)

	# print(locations)
	labels, data = zip(*locations.items())
	x, y = zip(*data)
	plt.scatter(x, y, s=areas_top, alpha=0.5)
	for l, x, y in zip(labels, x, y):
		plt.annotate(l, xy=(x, y), textcoords='offset points', xytext=(0, 20), horizontalalignment='center')
	plt.show()		
def SelfOrganizingMap(feature_list):
    no_features = len(feature_list[0])
    print('Clustering', no_features, 'features')
    som = SOM(SOM_LINES, SOM_COLS, no_features, SOM_ITERATIONS, alpha=ALPHA, sigma=SIGMA)
    som.train(feature_list)

    image_grid = som.get_centroids()
    mapped = som.map_vects(feature_list)

    return mapped, image_grid
Beispiel #18
0
def load_som(net_name):
    """
    loads som from directory net_name
    """
    global som

    if not os.path.exists(net_name):
        print("No such som")
        return

    weightages = np.load(net_name + output_weightages + ".npy")
    locations = np.load(net_name + output_locations + ".npy")
    with open(net_name + output_properties_file) as input:
        properties = json.load(input)
    som = SOM(properties["m"], properties["n"], properties["dim"],
              properties["n_iterations"], properties["alpha"],
              properties["sigma"], properties["trained_iterations"],
              weightages, locations, properties["trained"])
Beispiel #19
0
def main(in_file, out_file, x, y, epochs, ref=None, test=False, verbose=0):
    if test:
        df = pd.DataFrame(in_file, columns=range(in_file.shape[1]))
    else:
        df = pd.read_table(in_file, sep='\t', low_memory=True, index_col=0)

    s = df.shape[0]
    df.dropna(axis=0, how='any', inplace=True)
    sn = df.shape[0]
    if s != sn:
        logger.warning('%d rows dropped due to missing values' % (s - sn))

    s = df.shape[1]
    df = df.select_dtypes(include=[np.number])
    sn = df.shape[1]
    if s != sn:
        logger.warning('%d columns dropped due to non-numeric data type' % (s - sn))

    basedir = os.path.dirname(os.path.abspath(__file__))
    som = SOM(x, y)
    if ref == 'IRCI':
        som = som.load('/SOM.pkl')
        embedding = som.winner_neurons(df.values)
    else:
        som.fit(df.values, epochs, verbose=verbose)
        embedding = som.winner_neurons(df.values)
        if ref == 'Create':
            som.save(basedir + '/SOM.pkl')

    emb_df = pd.DataFrame({'ID': df.index})
    emb_df['X'] = embedding[:, 1]
    emb_df['Y'] = embedding[:, 0]
    if test:
        return emb_df
    else:
        emb_df.to_csv(out_file, index=False, sep='\t')
Beispiel #20
0
def main():
    # Training inputs for RGBcolors
    credits_data = pd.read_csv("testdata/credit_train.csv", header=None)
    columns = credits_data.columns
    train_data = credits_data[columns[:-1]].copy().values
    label_data = credits_data[columns[-1]].copy().values
    train_data_scale = preprocessing.scale(train_data)
    # colors = np.array(
    #     [[0., 0., 0.],
    #      [0., 0., 1.],
    #      [0., 0., 0.5],
    #      [0.125, 0.529, 1.0],
    #      [0.33, 0.4, 0.67],
    #      [0.6, 0.5, 1.0],
    #      [0., 1., 0.],
    #      [1., 0., 0.],
    #      [0., 1., 1.],
    #      [1., 0., 1.],
    #      [1., 1., 0.],
    #      [1., 1., 1.],
    #      [.33, .33, .33],
    #      [.5, .5, .5],
    #      [.66, .66, .66]])
    # color_names = \
    #     ['black', 'blue', 'darkblue', 'skyblue',
    #      'greyblue', 'lilac', 'green', 'red',
    #      'cyan', 'violet', 'yellow', 'white',
    #      'darkgrey', 'mediumgrey', 'lightgrey']

    # Train a 20x30 SOM with 400 iterations
    som = SOM(1, 2, len(columns) - 1, 400)
    som.train(train_data_scale)

    # Get output grid
    image_grid = som.get_centroids()

    # Map colours to their closest neurons
    mapped = som.map_vects(train_data_scale)

    # Plot
    plt.imshow(image_grid)
    plt.title('Color SOM')
    for i, m in enumerate(mapped):
        plt.text(m[1],
                 m[0],
                 label_data[i],
                 ha='center',
                 va='center',
                 bbox=dict(facecolor='white', alpha=0.5, lw=0))
    plt.show()
def run(data, musics):

    som = SOM(30, 30)  # initialize the SOM
    som.fit(data, 20000)  # fit the SOM for 2000 epochs

    #targets = len(data) * [0]   # create some dummy target values
    # vizualizando as paradas, ver se pego o que eu precido
    # som.plot_point_map(data, targets, ['class 1', 'class 2'], filename='./results/som.png')
    # som.plot_class_density(data, targets, 0, filename='./results/class_0.png', names=['a', 'b', 'c'], mode)
    # som.plot_density_map(data, filename='som1.png')

    #preparando para clusterizar, denovo
    winners = som.winner_map(data)
    #plt.imshow(winners, interpolation='nearest', extent=(0.5,10.5,0.5,10.5))
    #plt.colorbar()
    #plt.show()

    points = []
    for i in range(0, len(winners)):
        for j in range(0, len(winners[0])):
            points.append([i, j, winners[i][j]])

    # create dendrogram para imprimir
    #dendrogram = sch.dendrogram(sch.linkage(points, method='ward'))

    # create clusters
    hc = AgglomerativeClustering(n_clusters=6,
                                 affinity='euclidean',
                                 linkage='ward')
    # save clusters for chart
    y_hc = hc.fit_predict(points)

    for j in range(0, len(y_hc)):
        point = points[j]
        winners[point[0], point[1]] = y_hc[j]

    #vizualizando a qualidade da clusterizacao
    #plt.matshow(winners)
    #plt.show()

    for music in musics:
        winner = som.winner(music.toObject()['valor'])
        music.setGroupKon(winners[winner[0]][winner[1]])
Beispiel #22
0
def tensorSOM():
    import numpy as np
    from som import SOM
    import tensorflow as tf

    sess = tf.InteractiveSession()
    map_size = 30

    s = SOM(input_shape=(num_features, ),
            map_size_n=map_size,
            num_expected_iterations=epochs,
            session=sess)

    sess.run(tf.initialize_all_variables())

    #Training inputs
    for i in range(10):
        print('Epoch: {0}'.format(i))
        rnd_ind = np.random.randint(0, len(X))
        s.train(X[rnd_ind, :])

    print(np.reshape(s.get_weights(), [map_size, map_size, num_features]))
    print(np.array(y) * s.get_weights())
Beispiel #23
0
    def som3(self, m, n, dim, iterations):
        # step 4
        trainData = numpy.zeros((len(self.contrastPoints), 3))

        for i in range(len(self.contrastPoints)):
            trainData[i] = [self.diagonalContrast[i], self.verticalContrast[i], self.horizontalContrast[i]]

        som = SOM(m, n, dim, iterations)
        som.train(trainData)    
        mapped = som.map_vects(trainData)
        d0 = 0
        d1 = 0
        d2 = 0

        for d, m in zip(trainData, mapped):
            mapv = m[0] + m[1]
            
            if (d0 == 0 and mapv == 0):
                d0 = d[0] if d0 < d[0] else d0

            if (d1 == 0  and mapv == 1):    
                d1 = d[0] if d1 < d[0] else d1
                
            if (d2 == 0  and mapv == 2): 
                d2 = d[0] if d2 < d[0] else d2
                
        arr = numpy.array([d0, d1, d2])
        most = numpy.argmax(arr)
        numpy.delete(arr, most, 0)
        least = numpy.argmin(arr)
        numpy.delete(arr, least, 0)
        mean = 0 if (arr[most] == d1 or arr[most] == d2) and (arr[least] == d1 or arr[least] == d2) else (1 if (arr[most] == d0 or arr[most] == d2) and (arr[least] == d0 or arr[least] == d2) else 2)
       
        self.mappingDict = {'least':  least, 'mean': mean, 'most': most}  
        self.blocksMapping = mapped
        self.neuronsMap = som.get_centroids()
 def test_across_root(self):
     new = '/home/user/firefox/bookmarks'
     original = '/var/log/gnome'
     distance = SOM.path_distance(original, new)
     self.assertEqual(distance, 7)
Beispiel #25
0
                   [0., 1., 0.], [1., 0., 0.], [0., 1., 1.], [1., 0., 1.],
                   [1., 1., 0.], [1., 1., 1.], [.33, .33, .33], [.5, .5, .5],
                   [.66, .66, .66]])
color_names = \
    ['black', 'blue', 'darkblue', 'skyblue',
     'greyblue', 'lilac', 'green', 'red',
     'cyan', 'violet', 'yellow', 'white',
     'darkgrey', 'mediumgrey', 'lightgrey']

data = list()
for i in range(colors.shape[0]):
    data.append(torch.FloatTensor(colors[i, :]))

#Train a 20x30 SOM with 100 iterations
n_iter = 100
som = SOM(m, n, 3, n_iter)
for iter_no in range(n_iter):
    #Train with each vector one by one
    for i in range(len(data)):
        som(data[i], iter_no)

#Store a centroid grid for easy retrieval later on
centroid_grid = [[] for i in range(m)]
weights = som.get_weights()
locations = som.get_locations()
for i, loc in enumerate(locations):
    centroid_grid[loc[0]].append(weights[i].numpy())

#Get output grid
image_grid = centroid_grid
Beispiel #26
0
from som import SOM
s = SOM()

# teste do método distância euclidiana
d = s.distancia([2, 1, 1], [4, 2, 3])
print(d, ' = 3')

posM = s.melhorNeuronio([17, 13, 22])
print(posM)
print(s.matrizNeuronios[posM[0]][posM[1]])

r = s.melhorReposta([17, 13, 22])
print(r)
Beispiel #27
0
            train_data = datasets.FashionMNIST(DATA_DIR,
                                               train=True,
                                               download=True,
                                               transform=transform)
            train_loader = DataLoader(train_data,
                                      batch_size=batch_size,
                                      shuffle=True)
        else:
            print('Please set specify dataset. --mnist, --fashion_mnist')
            exit(0)

    train_data.train_data = train_data.train_data[:5000]
    train_data.train_labels = train_data.train_labels[:5000]

    print('Building Model...')
    som = SOM(input_size=28 * 28 * 1, out_size=(row, col))
    if os.path.exists('%s/som.pth' % MODEL_DIR):
        som.load_state_dict(torch.load('%s/som.pth' % MODEL_DIR))
        print('Model Loaded!')
    else:
        print('Create Model!')
    som = som.to(device)

    if train == True:
        losses = list()
        for epoch in range(total_epoch):
            running_loss = 0
            start_time = time.time()
            for idx, (X, Y) in enumerate(train_loader):
                X = X.view(-1, 28 * 28 * 1).to(device)  # flatten
                loss = som.self_organizing(X, epoch, total_epoch)  # train som
Beispiel #28
0
def main():
    # generate some random data with 36 features
    data1 = np.random.normal(loc=-.25, scale=0.5, size=(500, 36))
    data2 = np.random.normal(loc=.25, scale=0.5, size=(500, 36))
    data = np.vstack((data1, data2))

    som = SOM(10, 10)  # initialize the SOM
    som.fit(data, 10000, save_e=True, interval=100
            )  # fit the SOM for 10000 epochs, save the error every 100 steps
    som.plot_error_history(
        filename='images/som_error.png')  # plot the training error history

    targets = np.array(500 * [0] +
                       500 * [1])  # create some dummy target values

    # now visualize the learned representation with the class labels
    som.plot_point_map(data,
                       targets, ['Class 0', 'Class 1'],
                       filename='images/som.png')
    som.plot_class_density(data,
                           targets,
                           t=0,
                           name='Class 0',
                           filename='images/class_0.png')
    som.plot_distance_map(filename='images/distance_map.png'
                          )  # plot the distance map after training
Beispiel #29
0
colors = np.append(colors, np.array([[0, 0, 0]]), axis=0)
colors = np.append(colors, np.array([[1, 1, 1]]), axis=0)
for i in range(10):
    colors = np.append(colors, np.array([[0, 0, random.random()]]), axis=0)
    colors = np.append(colors, np.array([[0, random.random(), 0]]), axis=0)
    colors = np.append(colors, np.array([[random.random(), 0, 0]]), axis=0)
    colors = np.append(colors, np.array([[1, 1, random.random()]]), axis=0)
    colors = np.append(colors, np.array([[1, random.random(), 1]]), axis=0)
    colors = np.append(colors, np.array([[random.random(), 1, 1]]), axis=0)
    colors = np.append(colors, np.array([[0, random.random(), random.random()]]), axis=0)
    colors = np.append(colors, np.array([[random.random(), random.random(), 0]]), axis=0)
    colors = np.append(colors, np.array([[1, random.random(), random.random()]]), axis=0)
    colors = np.append(colors, np.array([[random.random(), random.random(), 1]]), axis=0)
    colors = np.append(colors, np.array([[random.random(), random.random(), random.random()]]), axis=0)
data = torch.Tensor(colors)

row = 40
col = 40
total_epoch = 1000

som = SOM(3, (row, col))
for iter_no in range(total_epoch):
    som.self_organizing(data, iter_no, total_epoch)

weight = som.weight.reshape(3, row, col).numpy()
weight = np.transpose(weight, (1, 2, 0,))

plt.title('Color SOM')
plt.imshow(weight)
plt.show()
Beispiel #30
0
    #Plot
    plt.imshow(image_grid)
    plt.title('Color SOM')
    for i, map_ in enumerate(mapped):
        plt.text(map_[1],
                 map_[0],
                 color_names[i],
                 ha='center',
                 va='center',
                 bbox=dict(facecolor='white', alpha=0.5, lw=0))


#Train a 20x30 SOM with 100 iterations
n_iter = 100
som = SOM(m, n, 3, n_iter)
for iter_no in range(n_iter):
    #Train with each vector one by one
    som(data_t, iter_no)

mapped = som.map_vects(torch.Tensor(colors))


def infer(som, color_rgb):
    required = som.get_bmu_loc(torch.Tensor(color_rgb))

    dists = som.pdist(torch.stack([required for i in range(len(mapped))]),
                      torch.stack(mapped))

    min_dist, min_dist_index = torch.min(dists, 0)
Beispiel #31
0
def normalized_distance(n1, n2, max_dist):
    return SOM.vector_distance(n1.weights, n2.weights) * MAX_NORMALIZED_DISTANCE / max_dist
 def test_same(self):
     original = '/home/user/test.txt'
     new = '/home/user/test.txt'
     distance = SOM.path_distance(original, new)
     self.assertEqual(distance, 0)
Beispiel #33
0
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with sompy. If not, see <http://www.gnu.org/licenses/>.
"""

import random

from som import SOM
from distance_functions import euclidean
from damping_functions import exponentional_damping
from neighbourhood_functions import mexican_hat_with_sigma
from fill_functions import random_fill

som = SOM(n = 4, dimensions = 5, fill = random_fill)

samples = [[random.gauss(m, 0.1) for e in range(0, 5)] 
    for m in range(0, 20) 
    for sample in range(0,10)]

# training
for index in range(0, len(samples)):

    # learn_rate as a factor of how much the weight vectors are 
    # pulled to the target vector. 
    learn_rate = exponentional_damping(\
        start = 1.0, end = 0.1,\
        t = float(index), tmax = len(samples)) 

    # sigma influences neighbourhood radius
 def test_down_file(self):
     new = '/home/user/doc.pdf'
     original = '/home/user'
     distance = SOM.path_distance(original, new)
     self.assertEqual(distance, 1)
 def test_down_folder(self):
     new = '/home/user/typography/doc.pdf'
     original = '/home/user'
     distance = SOM.path_distance(original, new)
     self.assertEqual(distance, 2)
Beispiel #36
0
#For plotting the images
from matplotlib import pyplot as plt
import numpy as np
from som import SOM

colors = np.array(
     [[0., 0., 1.],
      [0., 0., 0.95],
      [0., 0.05, 1.],
      [0., 1., 0.],
      [0., 0.95, 0.],
      [0., 1, 0.05],
      [1., 0., 0.],
      [1., 0.05, 0.],
      [1., 0., 0.05],
      [1., 1., 0.]])

som = SOM(4, 4, 3)
som.train(colors)

plt.imshow(som.centroid_grid)
plt.show()