Хотелось бы спросить, можно ли обучить данную сеть на данных из файла Excel.
При изучении принципов работы карт Кохонена, мне не удалось понять как использовать данные в виде многомерных векторов. И также мне хотелось бы узнать как можно реализовать данную задачу с помощью Python.
В качестве базы данных используется файл Excel на 711 строк с данными в виде действительных чисел, в столбцах указанны группы на которые эти данные разделяются.
Насколько я понял, для работы с такого рода файлами используется библиотека Pandas, но как именно мне следует преобразовать файл, мне не довелось понять.
Буду благодарен за помощь.
import time
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
class SOMNetwork():
def __init__(self, input_dim, dim=10, sigma=None, learning_rate=0.1,
tay2=1000, dtype=tf.float32):
if not sigma:
sigma = dim / 2
self.dtype = dtype
self.dim = tf.constant(dim, dtype=tf.int64)
self.learning_rate = tf.constant(learning_rate, dtype=dtype, name='learning_rate')
self.sigma = tf.constant(sigma, dtype=dtype, name='sigma')
self.tay1 = tf.constant(1000/np.log(sigma), dtype=dtype, name='tay1')
self.minsigma = tf.constant(sigma * np.exp(-1000/(1000/np.log(sigma))), dtype=dtype, name='min_sigma')
self.tay2 = tf.constant(tay2, dtype=dtype, name='tay2')
#input vector
self.x = tf.placeholder(shape=[input_dim], dtype=dtype, name='input')
#iteration number
self.n = tf.placeholder(dtype=dtype, name='iteration')
#variables
self.w = tf.Variable(tf.random_uniform([dim*dim, input_dim], minval=-1, maxval=1, dtype=dtype),
dtype=dtype, name='weights')
#helper
self.positions = tf.where(tf.fill([dim, dim], True))
def feed(self, input):
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
win_index = sess.run(self.__competition(), feed_dict={self.x: input})
win_index_2d = np.array([win_index//self.dim.eval(), win_index-win_index//self.dim.eval()*self.dim.eval()])
return win_index_2d
def training_op(self):
win_index = self.__competition('train_')
with tf.name_scope('cooperation') as scope:
coop_dist = tf.sqrt(tf.reduce_sum(tf.square(tf.cast(self.positions -
[win_index//self.dim, win_index-win_index//self.dim*self.dim],
dtype=self.dtype)), axis=1))
sigma = tf.cond(self.n > 1000, lambda: self.minsigma, lambda: self.sigma * tf.exp(-self.n/self.tay1))
sigma_summary = tf.summary.scalar('Sigma', sigma)
tnh = tf.exp(-tf.square(coop_dist) / (2 * tf.square(sigma))) # topological neighbourhood
with tf.name_scope('adaptation') as scope:
lr = self.learning_rate * tf.exp(-self.n/self.tay2)
minlr = tf.constant(0.01, dtype=self.dtype, name='min_learning_rate')
lr = tf.cond(lr <= minlr, lambda: minlr, lambda: lr)
lr_summary = tf.summary.scalar('Learning rate', lr)
delta = tf.transpose(lr * tnh * tf.transpose(self.x - self.w))
training_op = tf.assign(self.w, self.w + delta)
return training_op, lr_summary, sigma_summary
def __competition(self, info=''):
with tf.name_scope(info+'competition') as scope:
distance = tf.sqrt(tf.reduce_sum(tf.square(self.x - self.w), axis=1))
return tf.argmin(distance, axis=0)